32 Game State Management
This tour explores LinuxDOOM
’s game state management in g_game.h
and g_game.c
, covering how new games are started, saves/loads handled, per-tick logic driven, and input events dispatched.
The header linuxdoom-1.10/g_game.h declares the core game lifecycle functions.
linuxdoom-1.10/g_game.h — G_InitNew, G_DeferedInitNew, G_LoadGame and G_SaveGame (lines 36–53) handle new game setup and save/load operations.
linuxdoom-1.10/g_game.h — G_Ticker and G_Responder (lines 68–70) drive the game tick and dispatch input events. Implementations live in
linuxdoom-1.10/g_game.c
.linuxdoom-1.10/g_game.c — G_InitNew configures and starts a new game.
linuxdoom-1.10/g_game.c — Sets all players to PST_REBORN state and marks this as a user-controlled game rather than a demo.
linuxdoom-1.10/g_game.c — G_DoLoadLevel loads and initializes the new game level. Next we’ll walk through load-game functionality in
g_game.c
, coveringG_DoLoadGame
and the unarchiving routines that restore players, world, thinkers, and specials.linuxdoom-1.10/g_game.c — G_DoLoadGame loads the save file and positions save_p past the header.
linuxdoom-1.10/g_game.c — G_InitNew sets up the base level before loading the saved game state.
linuxdoom-1.10/g_game.c — The game state is restored by unarchiving players, world geometry, active entities (thinkers), and special effects in sequence.
P_UnArchivePlayers
reads saved player data from the save file back into memory, restoring each player’s state.
P_UnArchiveWorld
restores each sector’s floor/ceiling geometry and lighting by loading the saved height values, textures, and light levels.
P_UnArchiveThinkers
restores game objects (mobjs
) from the save file by reading their data and reconstructing their memory structures and state pointers.
The next steps examine G_SaveGame
in g_game.c
, which handles saving the game state to disk.
- linuxdoom-1.10/g_game.c — Writes the save header containing description and version.
- linuxdoom-1.10/g_game.c — Writes basic game state variables to the save file. Archives players, world, thinkers and specials, then writes to disk via
M_WriteFile
.
G_Ticker
handles per-tick game logic, while G_Responder
manages input event routing.
- linuxdoom-1.10/g_game.c — In GS_LEVEL state, P_Ticker updates gameplay, ST_Ticker updates status bar, AM_Ticker updates automap, HU_Ticker updates HUD.
- linuxdoom-1.10/g_game.c — The gametic counter helps manage buffered player input commands by determining which set of commands to process in the circular buffer of size BACKUPTICS.
- linuxdoom-1.10/g_game.c — G_Responder checks if input events should be handled by the HUD, status bar, or automap. And that’s our look at
g_game
’s state management.