I guess the question is straightforward. I'm creating a simple 2D game with a few animation and 30 or 60 fps are more than enough. I'd like to cap the fps to reduce power consumption on my laptop when testing my own game. I can manage that from the nvidia control panel, but I can do that from ingame code? I can see many games provide a fps cap option. How do they achieve that?
Sleeping/calling Sdl_delay doesn't seem a great option and neither is active waiting while checking for passed time. Is there an hardware mechanism I can block to?
I believe that the most common approach is to use an OS timer (via SDL_GetTicks), and keep a timestamp of the last time you rendered the display. Then your main loop just checks the current time vs the stored timestamp, and only renders a new frame when the difference is greater than 1/60 of a second (or what have you).
This will reduce your program's resource usage while still letting the OS get on with whatever else it's doing.
You say active waiting, but I wonder if you mean a busy-wait? Busy-waiting is generally bad, but don't forget that your main loop is just what executes when the OS decides to give your program some processor time. If you just check a stored timestamp vs the current clock at the start of each iteration, and then do nothing unless enough time has passed, control will go back to the OS for a bit, not the start of your next loop, so it's not a true busy-wait.
The original PC's hardware timer was... not great, and I believe that the situation only got worse over time. I understand the desire not to waste resources, but modern OSs are designed with the fear of not fully exploiting resources, so there's only so much you can do.