SDL timers offer several important advantages over frame counting for game events. Let's explore why you might choose one over the other.
Frame-based timing can be problematic because frame rates often fluctuate. Consider this frame-counting approach:
#include <SDL.h>
#include <iostream>
int main(int argc, char** argv) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* Window = SDL_CreateWindow(
"Frame Timer", 100, 100, 800, 600, 0);
int FrameCount{0};
// Spawn every 60 frames
const int SpawnInterval{60};
while (true) {
// Process events...
SDL_PumpEvents();
FrameCount++;
if (FrameCount % SpawnInterval == 0) {
std::cout << "Spawning enemy at frame "
<< FrameCount << '\n';
}
// Render scene...
// Simulate varying frame times
SDL_Delay(16);
}
SDL_DestroyWindow(Window);
SDL_Quit();
return 0;
}
Spawning enemy at frame 60
Spawning enemy at frame 120
Spawning enemy at frame 180
Spawning enemy at frame 240
If your game runs at 60 FPS, enemies spawn every second. But if it drops to 30 FPS, enemies spawn every 2 seconds! With SDL timers, spawning remains consistent regardless of frame rate:
#include <SDL.h>
#include <iostream>
Uint32 SpawnEnemy(Uint32 Interval, void*) {
std::cout << "Spawning enemy at time "
<< SDL_GetTicks() << "ms\n";
return Interval;
}
int main(int argc, char** argv) {
SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO);
SDL_Window* Window = SDL_CreateWindow(
"SDL Timer", 100, 100, 800, 600, 0);
SDL_AddTimer(1000, SpawnEnemy, nullptr);
while (true) {
// Process events...
SDL_PumpEvents();
// Render scene...
// Varying frame times don't affect spawn rate
SDL_Delay(16);
}
SDL_DestroyWindow(Window);
SDL_Quit();
return 0;
}
Spawning enemy at time 1090ms
Spawning enemy at time 2090ms
Spawning enemy at time 3090ms
Spawning enemy at time 4090ms
Frame counting requires checking conditions every frame, even for events that happen rarely. SDL timers only consume resources when they actually trigger, making them more efficient for:
SDL timers run on a separate thread, meaning they:
The downside is you need to be careful about thread safety when modifying shared data in callbacks.
Use SDL timers for:
Use frame counting for:
Remember, you can also combine both approaches where appropriate!
Answers to questions are automatically generated and may not have been reviewed.
Learn how to use callbacks with SDL_AddTimer()
to provide functions that are executed on time-based intervals