2017-10-29 1 views
-2

SDL_Rect에 이상한 오류가 발생했습니다. 때마다 나는이 사각형, SDL_RenderFillRect 전화 :SDL_Rect가 지정보다 큽니다

SDL_Rect rect = {100, 100, 100, 100}; 

나는 항상 전체 800 × 600 창을 덮고있는 사각형 끝.

#include "SDL.h" 
#include <iostream> 
#include <cstdlib> 
#include <ctime> 
#include <thread> 
#include <string> 

class Window 
{ 
public: 
    Window(std::string title, int width, int height); 
    void runEvents(SDL_Event event); 
    void drawRect(int x, int y, int h, int w); 
    bool closed = false; 
    SDL_Renderer *_renderer; 
    SDL_Window *_window; 

private: 
    int colour = 0; 
}; 

Window::Window(std::string title, int width, int height) 
{ 
    _window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, 0); 
    _renderer = SDL_CreateRenderer(_window, -1, 0); 
} 

// this function deals with rects (it's supposed to draw a rect when the mouse button is down): 
void Window::runEvents(SDL_Event event) 
{ 
    SDL_Rect rect = { 100, 100, 100, 100 }; 
    switch(event.type) 
    { 
    case SDL_QUIT: 
     closed = true; 
     break; 
    case SDL_KEYDOWN: 
     switch(event.key.keysym.sym) 
     { 
     case SDLK_ESCAPE: 
      closed = true; 
      break; 
     case SDLK_KP_1: 
      // set colour to blue 
      SDL_SetRenderDrawColor(_renderer, 0, 51, 204, 255); 
      std::cout << "Colour set to blue!" << std::endl; 
      colour = 1; 
      break; 
     case SDLK_KP_2: 
      // set colour to red 
      SDL_SetRenderDrawColor(_renderer, 255, 0, 0, 255); 
      std::cout << "Colour set to red!" << std::endl; 
      colour = 2; 
      break; 
     case SDLK_KP_3: 
      // set colour to green 
      SDL_SetRenderDrawColor(_renderer, 51, 204, 51, 255); 
      std::cout << "Colour set to green!" << std::endl; 
      colour = 3; 
      break; 
     case SDLK_KP_4: 
      SDL_SetRenderDrawColor(_renderer, 153, 0, 153, 255); 
      std::cout << "Colour set to purple!" << std::endl; 
      colour = 4; 
      break; 
     case SDLK_KP_0: 
      SDL_SetRenderDrawColor(_renderer, 255, 255, 255, 255); 
      std::cout << "Colour set to white!" << std::endl; 
      colour = 0; 
      break; 
     } 
     break; 
     //this draws the rectangle: 
    case SDL_MOUSEBUTTONDOWN: 
     SDL_SetRenderDrawColor(_renderer, 255, 0, 0, 255); 
     drawRect(rect.h, rect.y, rect.h, rect.w); 
     break; 
     //this removes it 
    case SDL_MOUSEBUTTONUP: 
     switch(colour) 
     { 
     case 1: 
      SDL_SetRenderDrawColor(_renderer, 0, 51, 204, 255); 
      break; 
     case 2: 
      SDL_SetRenderDrawColor(_renderer, 255, 0, 0, 255); 
      break; 
     case 3: 
      SDL_SetRenderDrawColor(_renderer, 51, 204, 51, 255); 
      break; 
     case 4: 
      SDL_SetRenderDrawColor(_renderer, 153, 0, 153, 255); 
      break; 
     case 0: 
      SDL_SetRenderDrawColor(_renderer, 255, 255, 255, 255); 
      break; 
     } 
     drawRect(rect.h, rect.y, rect.h, rect.w); 
     break; 
    } 
} 

//this function is supposed to draw the rect 
void Window::drawRect(int x, int y, int h, int w) 
{ 
    SDL_Rect rect = { x, y, w, h }; 
    SDL_RenderFillRect(_renderer, &rect); 
} 

bool running = true; 

void mouseCheck() 
{ 
    while(running) 
    { 
     int x; 
     int y; 
     SDL_GetMouseState(&x, &y); 
     printf("Mouse is at x: %i and y: %i\n", x, y); 
     SDL_Delay(5000); 
    } 
} 

int main(int argc, char *argv[]) 
{ 
    SDL_Init(SDL_INIT_EVERYTHING); 

    Window window("Hello World", 800, 600); 
    SDL_Event event; 

    SDL_SetRenderDrawColor(window._renderer, 255, 255, 255, 255); 

    std::thread mousePosition(mouseCheck); 
    mousePosition.detach(); 

    while(!window.closed) 
    { 
     SDL_RenderClear(window._renderer); 
     SDL_RenderPresent(window._renderer); 
     if(SDL_PollEvent(&event)) 
     { 
      window.runEvents(event); 

     } 
    } 
    return 0; 
} 

답변

1

여러 문제 :

  • 지금 당신의 주요 무승부 루프는 다음과 같습니다

    1. Draw 
    2. Clear 
    3. Present 
    

    당신이 원하는 :

    1. (re)Set clear color 
    2. Clear 
    3. Draw 
    4. Present 
    
  • 여기

    내 프로그램의
  • mouseCheck() 스레드로 시도하고있는 내용을 잘 모르지만 다른 스레드에서 GUI 관련 SDL 기능을 호출하지 마십시오. 다른 스레드의 해당 정보가 필요하면 스레드 안전성 원자/큐/메시지를 사용하여 통신하십시오.

  • rect.x 대신 첫 번째 매개 변수로 rect.h을 사용하여 두 곳에서 drawRect()을 호출하려고합니다.

  • runEvents()처럼 입력 처리 및 렌더링을 인터리빙하지 않는 것이 좋습니다. 이벤트 대기열 (while(SDL_PollEvent()))을 배출하는 동안 시스템 상태 (shouldDrawRect & colour)를 업데이트 한 다음 새 상태 (Window::drawScene())의 프레임을 그립니다.

  • 프레임 당 하나의 이벤트 만 처리 중입니다. 각 프레임의 이벤트 큐를 while(SDL_PollEvent())을 통해 배제하여 이벤트가 백업 및 삭제되지 않도록하십시오. 모두 함께

는 :

#include "SDL.h" 
#include <iostream> 
#include <cstdlib> 
#include <ctime> 
#include <thread> 
#include <string> 

class Window 
{ 
public: 
    Window(std::string title, int width, int height); 
    void runEvents(SDL_Event event); 
    void drawScene(); 
    void drawRect(int x, int y, int h, int w); 
    bool closed = false; 
    SDL_Renderer *_renderer; 
    SDL_Window *_window; 

private: 
    bool shouldDrawRect; 
    int colour = 0; 
}; 

Window::Window(std::string title, int width, int height) 
{ 
    _window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, 0); 
    _renderer = SDL_CreateRenderer(_window, -1, 0); 
    shouldDrawRect = false; 
} 

void Window::drawScene() 
{ 
    if(shouldDrawRect) 
    { 
     switch(colour) 
     { 
     case 0: 
      SDL_SetRenderDrawColor(_renderer, 255, 0, 0, 255); 
      break; 
     case 1: 
      SDL_SetRenderDrawColor(_renderer, 0, 51, 204, 255); 
      break; 
     case 2: 
      SDL_SetRenderDrawColor(_renderer, 255, 0, 0, 255); 
      break; 
     case 3: 
      SDL_SetRenderDrawColor(_renderer, 51, 204, 51, 255); 
      break; 
     case 4: 
      SDL_SetRenderDrawColor(_renderer, 153, 0, 153, 255); 
      break; 
     } 

     SDL_Rect rect = { 100, 100, 100, 100 }; 
     drawRect(rect.x, rect.y, rect.h, rect.w); 
    } 
} 

//this function deals with rects (it's supposed to draw a rect when the mouse button is down): 
void Window::runEvents(SDL_Event event) 
{ 
    switch(event.type) 
    { 
    case SDL_QUIT: 
     closed = true; 
     break; 
    case SDL_KEYDOWN: 
     switch(event.key.keysym.sym) 
     { 
     case SDLK_ESCAPE: 
      closed = true; 
      break; 
     case SDLK_KP_1: 
      // set colour to blue 
      std::cout << "Colour set to blue!" << std::endl; 
      colour = 1; 
      break; 
     case SDLK_KP_2: 
      // set colour to red 
      std::cout << "Colour set to red!" << std::endl; 
      colour = 2; 
      break; 
     case SDLK_KP_3: 
      // set colour to green 
      std::cout << "Colour set to green!" << std::endl; 
      colour = 3; 
      break; 
     case SDLK_KP_4: 
      std::cout << "Colour set to purple!" << std::endl; 
      colour = 4; 
      break; 
     case SDLK_KP_0: 
      std::cout << "Colour set to white!" << std::endl; 
      colour = 0; 
      break; 
     } 
     break; 
     //this draws the rectangle: 
    case SDL_MOUSEBUTTONDOWN: 
     shouldDrawRect = true; 
     break; 
     //this removes it 
    case SDL_MOUSEBUTTONUP: 
     shouldDrawRect = false; 
     break; 
    } 
} 

//this function is supposed to draw the rect 
void Window::drawRect(int x, int y, int h, int w) 
{ 
    SDL_Rect rect = { x, y, w, h }; 
    SDL_RenderFillRect(_renderer, &rect); 
} 

bool running = true; 

int main(int argc, char *argv[]) 
{ 
    SDL_Init(SDL_INIT_EVERYTHING); 

    Window window("Hello World", 800, 600); 
    SDL_Event event; 

    while(!window.closed) 
    { 
     SDL_SetRenderDrawColor(window._renderer, 255, 255, 255, 255); 
     SDL_RenderClear(window._renderer); 

     while(SDL_PollEvent(&event)) 
     { 
      window.runEvents(event); 
     } 
     window.drawScene(); 

     SDL_RenderPresent(window._renderer); 
    } 
    return 0; 
} 
+0

이 아름답게 작품! 고마워요. –