Module: game
Synopsis
#include <core/game.h>
Main game loop and states.
This module offers a global game structure that contain a state. It is designed to help switching game states and inhibit some of the functions when necessary.
In contrast to popular engines, states are not stacked and only one is used at a time. Switching states mean that the current state will run until next frame and will be closed afterwards.
The main loop use a constant frame rate mechanism based on the screen refresh rate if supported, otherwise it use a default frame rate of 60hz. In any case, the frame rate is capped in the main loop and the elapsed time is measured accordingly to update the game at constant speed no matter the refresh rate is present.
Since only one game must be present at a time, a global game
variable is
available for convenience to not carrying it everywhere.
Globals
Variable | Type |
---|---|
game |
struct game |
Structs
game
Field | Access | Type |
---|---|---|
inhibit | (+) | enum inhibit |
state | (+&?) | struct state * |
state_next | (+&?) | struct state * |
inhibit
Current inhibit flags set, see inhibit for more information.
state
Current state running.
state_next
Optional next state to be changed in next frame loop.
Functions
game_switch
Set state
for the next frame.
The state will only be effective after the next call to game_update.
If argument quick
is set to true, the state is changed immediately and the
current state code should immediately return.
void
game_switch(struct state *state, bool quick)
game_handle
Handle the event ev
into the current state.
void
game_handle(const union event *ev)
game_update
Update the current state with ticks
since last frame.
void
game_update(unsigned int ticks)
game_draw
Draw the current state.
void
game_draw(void)
game_loop
Start a blocking loop that call in order game_handle, game_update and game_draw while keeping a constant framerate.
void
game_loop(void);
game_stop
Destroy both next and current state if any.
Even if you don't use game_loop you should call this function because it will close state resources.
Caution
If you call this function from a state itself, you must return immediately because the state will be finalized immediately.
void
game_quit(void);