2017-12-29 5 views
0

내 선 그리기는 정상적으로 작동하지만 선이 깜박입니다. 루프의 FPS를 변경하려고했지만 RenderPresent() 코드를 다른 라인에 추가하거나 라인 렌더링을위한 새로운 코드를 추가하면됩니다. 나는 많은 가능성을 시도했지만 아무 것도 효과가 없었습니다.SDL2 C++에서 그리기 선이 깜박입니다. 내 게임 루프를 수정하는 방법?

라인 깜박임을 멈추게하려면 어떻게해야합니까? 자세한 내용을 원하면 의견을 보내주십시오.

// Global variables 
SDL_Event event;    // Event object 
int mouse_x = 0;    // Actual Mouse Coordinate X 
int mouse_y = 0;    // Actual Mouse Coordinate Y 
int mouse_last_x = 0;  // The coordinate X by last click 
int mouse_last_y = 0;  // The coordinate Y by last click 
bool dragged = false;  // Boolean after I clicked on some place 
bool running = true;   // Makes the game loop run 

void GameWin::loop()   //Game Loop 
{ 
    while (running) 
    { 
     SDL_GetMouseState(&mouse_x, &mouse_y); // Get Mouse Coordninates 
     SDL_PollEvent(&event); 

     SDL_SetRenderDrawColor(GameWin::renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); // Draw The Background Black 

     update(); 
     render(); 

     switch (event.type) 
     { 
     case SDL_QUIT: 
      running = false; 
      break; 
     case SDL_MOUSEBUTTONDOWN: // Mouse Click Event 
      if (event.button.button == SDL_BUTTON_LEFT) 
      { 
       mouse_last_x = mouse_x; // After Left Click Save my Mouse Coordingates 
       mouse_last_y = mouse_y; 
       dragged = true;   
      } 
      break; 
     case SDL_MOUSEMOTION: 
      if (dragged) 
      { 
       SDL_SetRenderDrawColor(GameWin::renderer, 137, 255, 85, SDL_ALPHA_OPAQUE); // Set The Color Line To Green 
       SDL_RenderDrawLine(GameWin::renderer, mouse_last_x, mouse_last_y, mouse_x, mouse_y); // Draw The Line 
       SDL_RenderPresent(GameWin::renderer); // Render Line 
      } 
      break; 
     case SDL_MOUSEBUTTONUP: 
      if (dragged) 
      { 
       dragged= false; 
       mouse_last_x = mouse_x; 
       mouse_last_y = mouse_y; 
      } 
      break; 
     default: 
      break; 
     } 
    } 
} 

// My render method 
void GameWin::render() 
{ 
    SDL_RenderClear(GameWin::renderer); 
    SDL_RenderPresent(GameWin::renderer); 
} 

답변

1

우선 이벤트가 전혀 발생하지 않았는지 확인하십시오. SDL_PollEvent으로 전화 한 다음 이벤트를 처리하십시오. 대기열에 이벤트가 있다는 보장이 없기 때문에 괜찮을 수도 있습니다. SDL_PollEvent은 대기열이 비어 있으면 0을 반환합니다. 이는 이벤트 구조에 의미있는 데이터를 채우지 않았 음을 의미합니다.

둘째, 이벤트 처리를 드로잉과 결합하지 마십시오. 반복 사이에 발생한 모든 이벤트를 가져온 다음 한 번 그립니다.

#include "SDL.h" 

// Global variables 
SDL_Event event;    // Event object 
int mouse_x = 0;    // Actual Mouse Coordinate X 
int mouse_y = 0;    // Actual Mouse Coordinate Y 
int mouse_last_x = 0;  // The coordinate X by last click 
int mouse_last_y = 0;  // The coordinate Y by last click 
bool dragged = false;  // Boolean after I clicked on some place 
bool running = true;   // Makes the game loop run 

int main(int argc, char **argv) { 
    SDL_Init(SDL_INIT_VIDEO); 
    SDL_Window *w = NULL; 
    SDL_Renderer *renderer = NULL; 
    SDL_CreateWindowAndRenderer(640, 480, 0, &w, &renderer); 

    while (running) 
    { 
     SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); 
     SDL_RenderClear(renderer); 

     SDL_GetMouseState(&mouse_x, &mouse_y); // Get Mouse Coordninates 
     while(SDL_PollEvent(&event)) { 
      switch (event.type) 
      { 
      case SDL_QUIT: 
       running = false; 
       break; 
      case SDL_MOUSEBUTTONDOWN: // Mouse Click Event 
       if (event.button.button == SDL_BUTTON_LEFT) 
       { 
        mouse_last_x = mouse_x; // After Left Click Save my Mouse Coordingates 
        mouse_last_y = mouse_y; 
        dragged = true;   
       } 
       break; 
      case SDL_MOUSEBUTTONUP: 
       if (dragged) 
       { 
        dragged= false; 
        mouse_last_x = mouse_x; 
        mouse_last_y = mouse_y; 
       } 
       break; 
      default: 
       break; 
      } 
     } 

     if(!running) break; 

     if (dragged) 
     { 
      SDL_SetRenderDrawColor(renderer, 137, 255, 85, SDL_ALPHA_OPAQUE); // Set The Color Line To Green 
      SDL_RenderDrawLine(renderer, mouse_last_x, mouse_last_y, mouse_x, mouse_y); // Draw The Line 
     } 

     SDL_RenderPresent(renderer); 
    } 
} 
+0

고마워 :

는 기본적으로 뭔가 같이해야합니다! :) –

관련 문제