SDL_PollEvent high CPU time

I am going through the lazy foo’s tutorials and up lesson04 when the example code is run the CPU runs at anywhere between 80 an 100%.

Lazy Foo Lesson 4

From trying to understand why, I ran across SDL_WaitEvent(), but there seems to be limitations on that API.

Any example I could find I rand the ode and every example code using SDL_PollEvent ran the CPU to 100%.

So I do not pick up any bad habits how should I handle this issue and what would be the correct fix.

Im not running any of my own code just exampel code from lazyfoo.

The reason the cpu is up around 80 - 100 is likely due to the program running as fast as possible.

You can add the SDL_PRESENT_VSYNC option to the SDL Renderer to limit the refresh rate. The tutorials probably mention this later on if you continue to follow.


Thanks for the tip, but did not work. The more I research the problem the more I see a lot of people with the same issue, but no consistent resolution. Some say use SDL_WaitEvent(), but it is not good to use for game engines, and I see a lot of more random resolutions. This seems like a known issue and no one fix.

I agree, but it probably has no effect because that particular Lazy Foo lesson never creates a renderer and doesn’t call SDL_RenderPresent() in the main loop. Whilst it purports to be for SDL 2.0 it does everything using surfaces in the old SDL 1.2 way. My advice to the OP would be to skip to Lesson 7 which uses a renderer and textures rather than surfaces, I would then expect SDL_PRESENT_VSYNC to be effective in reducing the CPU time.

Your right I didnt look realise it wasnt using the renderer.

I would recommend finishing off the tutorials, a lot of questions may be answered in later parts. If you still need help please message and I will assist in sorting out the issue.

Thanks, I push on but I did notice that this happens on any lesson I tried and the SDL_WaitEvent() did drop the cpu time considerable.

Thanks for the help.