6
我正在設計應該展示圖像上打開簡歷的程序。我注意到基本的SDL應用程序非常糟糕的概念 - 它由循環和延遲組成。防止SDL程序消耗額外的資源
while(true) {
while(event_is_in_buffer(event)) {
process_event(event);
}
do_some_other_stuff();
do_some_delay(100); //Program is stuck here, unable to respond to user input
}
這使得程序即使在後臺(或者如果不需要重新渲染)也能執行和渲染。如果我使用更長的延遲時間,則消耗的資源會減少,但在處理事件(如鼠標點擊)之前,我必須等待更長的時間。
我想要的是讓程序等待事件,就像WinApi一樣,或者像套接字請求一樣。 這可能嗎?
概念我想:
bool go=true;
while(get_event(event)&&go) { //Program gets stuck here if no events happen
switch(event.type){
case QUIT: go=false;
}
}
謝謝,這是我需要的。是否有一些技巧來設置等待時間限制(就像使用套接字一樣)? –
是的,你必須使用SDL_WaitEventTimeout(SDL_Event * event,int timeout),看到這個頁面: http://wiki.libsdl.org/moin.cgi/SDL_WaitEventTimeout –