Skip to content

Commit

Permalink
Implement periodic game state autosave (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
AMDmi3 committed Nov 4, 2020
1 parent 9a447b0 commit 3fa089e
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

#include "game.hh"

static const unsigned int AUTOSAVE_INTERVAL_MS = 5000;

static const std::map<SDL_Keycode, int> teleport_slots = {
{ SDLK_0, 0 },
{ SDLK_1, 1 },
Expand Down Expand Up @@ -69,6 +71,7 @@ int main(int, char*[]) try {
game.LoadState();

unsigned int prev_ticks = SDL_GetTicks();
unsigned int prev_save_ticks = prev_ticks;

// Main loop
while (1) {
Expand Down Expand Up @@ -146,6 +149,11 @@ int main(int, char*[]) try {

renderer.Present();

if (frame_ticks - prev_save_ticks > AUTOSAVE_INTERVAL_MS) {
game.SaveState();
prev_save_ticks = frame_ticks;
}

// Frame limiter
SDL_Delay(5);
}
Expand Down

0 comments on commit 3fa089e

Please sign in to comment.