Frame rate limiting is an important technique in game development to ensure consistent performance and reduce unnecessary resource consumption. High-resolution timers, like SDL_GetPerformanceCounter()
, can be used to implement precise frame rate limiting. Here's how you can do it:
The idea is to calculate how long each frame should take (target frame time) and then wait if the frame finishes early. For example, if we want 60 FPS, each frame should take approximately 1/60 seconds or about 16.67Â milliseconds.
Here's a basic implementation using SDL's high-resolution timer:
#include <SDL.h>
#include <iostream>
const int TARGET_FPS{60};
const double TARGET_FRAME_TIME
{1.0 / TARGET_FPS};
void simulateGameLogic() {
// Simulate some game logic
SDL_Delay(5); // Simulate 5ms of work
}
void render() {
// Simulate rendering
SDL_Delay(3); // Simulate 3ms of rendering
}
int main(int argc, char** argv) {
SDL_Init(SDL_INIT_TIMER);
Uint64 performanceFrequency{
SDL_GetPerformanceFrequency()};
Uint64 frameStart;
double elapsedSeconds;
for (int frame{0}; frame < 600; ++frame) {
frameStart = SDL_GetPerformanceCounter();
simulateGameLogic();
render();
elapsedSeconds = (
SDL_GetPerformanceCounter() -
frameStart) /
static_cast<double>(performanceFrequency);
if (elapsedSeconds < TARGET_FRAME_TIME) {
SDL_Delay(
static_cast<Uint32>((TARGET_FRAME_TIME -
elapsedSeconds) * 1000));
}
if (frame % 60 == 0) {
std::cout << "FPS: "
<< 1.0 / ((SDL_GetPerformanceCounter() -
frameStart) /
static_cast<double>(
performanceFrequency))
<< '\n';
}
}
SDL_Quit();
return 0;
}
FPS: 60.0213
FPS: 59.9881
FPS: 59.9902
FPS: 60.0156
FPS: 59.9923
...
SDL_GetPerformanceCounter()
.SDL_Delay()
for simplicity, but it's not always precise. For more accuracy, you could use a busy-wait loop, though this consumes more CPU:while (
(SDL_GetPerformanceCounter() - frameStart) /
static_cast<double>(performanceFrequency)
< TARGET_FRAME_TIME) {
// Busy wait
}
Remember, while frame rate limiting can save power and provide consistent performance, it's not always necessary. Many games run as fast as they can and use delta time for smooth animation. The best approach depends on your specific requirements.
Answers to questions are automatically generated and may not have been reviewed.
Learn to measure time intervals with high accuracy in your games