A very simple, yet versatile, game loop in C++ with EnTT
9 min readFeb 1, 2023
This is the 5th devlog of Warmonger Dynasty, a 4X turn-based strategy game made in C++/SDL. If you want to read the previous entries, take a look at my reading list.
When writing a game from scratch, the first step is to write the game loop. This is the part where you define what code should be run every frame, what should be run before and after the loop, and how/when you exit the loop (and therefore, the game).
Game Loop 101: Starting with the basics
A typical SDL application has usually 3 parts:
- initializion: setting up the SDL, loading assets, etc…
- event loop: process SDL events, update your game state, rendering
- teardown: free assets, teardown SDL, etc…
It may look like this (error handling is omitted for clarity):
int main(void) {
// initialization
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window *win = SDL_CreateWindow(
"my game",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOW_POS_UNDEFINED,
800, 600,
SDL_WINDOW_SHOWN
);
SDL_Renderer *renderer = SDL_CreateRenderer(
win,
-1,
SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE,
);
// event loop
bool running = true;
while…