Prevent SDL program from consuming extra resources

戏子无情 提交于 2019-12-12 10:43:00

问题


I'm designing program that should demonstrate open CV on images. I've noticed very bad concept of basic SDL application - it consists of loop and delay.

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
}

This makes the program execute and render even if it is on background (or if re-rendering is not necessary in the first place). If I use longer delay, I get less consumed resources but I must wait longer before events, like mouse click, are processed.
What I want is to make program wait for events, like WinApi does or like socket requests do. Is that possible?
Concept I want:

bool go=true;
while(get_event(event)&&go) {  //Program gets stuck here if no events happen
    switch(event.type){
       case QUIT: go=false;
    }
}

回答1:


You can use SDL_WaitEvent(SDL_Event *event) to wait for an event in the SDL. It will use less resources than the polling loop design you currently have. See the example in this doc:

{
    SDL_Event event;

    while ( SDL_WaitEvent(&event) ) {
        switch (event.type) {
                ...
                ...
        }
    }
}


来源:https://stackoverflow.com/questions/14666812/prevent-sdl-program-from-consuming-extra-resources

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!