Prevent SDL from using additional resources

I am developing a program that should demonstrate an open CV in images. I noticed a very poor concept of the underlying SDL application - it consists of a loop and a 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 forces the program to execute and display, even if it is in the background (or if re-rendering is not needed in the first place). If I use a longer delay, I get less resources consumed, but I have to wait longer than events like a mouse click will be processed.
I want the program to wait for events like WinApi or something like socket requests. Is it 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; } } 
+6
source share
1 answer

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

 { SDL_Event event; while ( SDL_WaitEvent(&event) ) { switch (event.type) { ... ... } } } 
+8
source

All Articles