3
\$\begingroup\$

I am learning sdl2 with C++ from Lazy Foo Productions' tutorials. I am using a 5 year old retina mac book pro and xcode.

When reading this entry on calculating frame rate I noticed that when runing the application it would tend to 60 fps and the cpu and energy impact were very low, 2% and Low respectively, but when placing another window over it the fps would shoot up to 5000 along with cpu and energy impact, to 93% and High respectively.

Why is this happening? Is it because sdl2 just stops rendering all together when it is not on top of other windows? Or is it because the OS stops it from rendering?

\$\endgroup\$
  • \$\begingroup\$ SDL probably tries to render the scene, but everything gets clipped on the GPU \$\endgroup\$ – Bálint Sep 14 '17 at 11:10
1
\$\begingroup\$

This is actually a complicated question to answer. There could be multiple possible explanations, including SDL2, the OS, and your graphics driver.

Either way, it sounds like Vsync is on which is intentionally limiting your frame rate which is limiting the executions of your core loop per second which is limiting your CPU usage. When rendering is turned off nothing restricts your core loop from executing as fast as it can and so your CPU usage spikes up.

If you want to experiment with turning off Vsync you can choose not to pass the SDL_RENDERER_PRESENTVSYNC flag into SDL_CreateRenderer or (if you are using SDL2 with OpenGL) using SDL_GL_SetSwapInterval. If Vsync is still on it might be your graphics driver enforcing it, in which case you would have to configure your driver (this occurs in my case on Windows with SDL2 and the most recent Nvidia drivers for my card), but either way keeping Vsync on is probably good in general.

If you want your CPU usage to not spike whenever rendering is turned off, you could look into trying to inspect window events or check your window status using SDL_GetWindowFlags. Most likely a flag like SDL_WINDOW_SHOWN is on whenever SDL is rendering and a flag like SDL_WINDOW_HIDDEN is on whenever it isn't. Every frame, if the 'is-not-displaying' flag is on, you could do something like SDL_Delay(16) to limit the frame rate at ~60fps when rendering is off.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.