2016-01-23 87 views
-1

我有一個SDL窗口,按下十字鍵後需要一段時間關閉。我在事件循環中發生了一些事情,所以我認爲這可能與事件有關。我有一個類似的SDL窗口,可以立即關閉,但在事件循環期間它沒有做任何事情,只檢查十字。SDL窗口不能正常關閉

我對事件的代碼循環是這樣的:

while(event.type != SDL_QUIT){ 
    while(SDL_PollEvent(&event) != 0){ 
    if (event.type == SDL_QUIT){ 
     SDL_Quit(); 
     exit(1); 
    } 
    flashingText(data, fontdata, display, text); 
    } 
    SDL_Delay(100); 
} 

flashingText功能有一定的SDL延誤和SDL渲染?

+0

'exit(1)'表示由於功能錯誤導致程序錯誤,程序關閉。當程序關閉時,你應該把'exit(0)'放在上面,因爲用戶按十字,因爲這意味着沒有任何錯誤。 [Here](https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v = vs.85).aspx)是你應該在exit中使用什麼參數的完整列表( )在哪種情況下的功能。 –

回答

0

延遲是因爲flashingText()被稱爲爲事件調查,並在窗口內更鼠標移動,更多的事件被解僱循環和輪詢。考慮這個代碼(它使用上面的事件輪詢);

#include <SDL2/SDL.h> 
#include <iostream> 

int main() 
{  
     SDL_Window* window; 
     SDL_Renderer* renderer; 

     // Initialize SDL. 
     if (SDL_Init(SDL_INIT_VIDEO) < 0) 
       return 1; 

     window = SDL_CreateWindow("SDL_RenderClear", 
         SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 
         512, 512, 
         0); 

     renderer = SDL_CreateRenderer(window, -1, 0); 
     SDL_SetRenderDrawColor(renderer, 255, 128, 128, 255); 
     SDL_RenderClear(renderer); 

    // OP's code starts 
    SDL_Event event; 
    while(event.type != SDL_QUIT){ 
    while(SDL_PollEvent(&event)){ 
     if (event.type == SDL_QUIT){ 
      SDL_Quit(); 
      exit(1); 
     } 
      SDL_RenderPresent(renderer); 
      // to simulate OP's flashingText() 
      SDL_Delay(50);    
    } 
    SDL_Delay(100); 
    } 
    // OP's code ends 

    return 0; 
} 

您會發現窗口區域內的鼠標滾動/移動越多,程序退出的延遲就越多。

希望有所幫助。

乾杯。