2017-12-29 26 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を返します。つまり、イベント構造を意味のあるデータで埋めることはできませんでした。

第2に、イベント処理を図面と組み合わせないでください。反復の間に発生したすべてのイベントを取得し、一度描画します。

#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

どうもありがとう:

基本的にはのようなものでなければなりません! :) –

関連する問題