Fps shoots up when sdl2 window is not on top

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?

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

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.