Skip to content

Commit 50ca80f

Browse files
committed
Added saving.
1 parent dc3c1a0 commit 50ca80f

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

CMakeSettings.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "x64-Debug (default)",
5+
"generator": "Ninja",
6+
"configurationType": "Debug",
7+
"inheritEnvironments": [ "msvc_x64_x64" ],
8+
"buildRoot": "${projectDir}\\out\\build\\${name}",
9+
"installRoot": "${projectDir}\\out\\install\\${name}",
10+
"cmakeCommandArgs": "",
11+
"buildCommandArgs": "",
12+
"ctestCommandArgs": "",
13+
"variables": []
14+
},
15+
{
16+
"name": "x64-Release",
17+
"generator": "Ninja",
18+
"configurationType": "RelWithDebInfo",
19+
"buildRoot": "${projectDir}\\out\\build\\${name}",
20+
"installRoot": "${projectDir}\\out\\install\\${name}",
21+
"cmakeCommandArgs": "",
22+
"buildCommandArgs": "",
23+
"ctestCommandArgs": "",
24+
"inheritEnvironments": [ "msvc_x64_x64" ],
25+
"variables": []
26+
}
27+
]
28+
}

game.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232

3333
using namespace blit;
3434

35+
struct SaveData {
36+
int score;
37+
};
38+
3539
struct Squirrel {
3640
float yPosition;
3741
float yVelocity;
@@ -61,6 +65,8 @@ uint32_t lastTime = 0;
6165

6266
int localHighscore = 0;
6367

68+
SaveData saveData;
69+
6470
float offset = 0;
6571

6672
int treeNumber = 0;
@@ -196,7 +202,17 @@ void start_game() {
196202
//
197203
void init() {
198204
set_screen_mode(ScreenMode::lores);
199-
screen.sprites = SpriteSheet::load(asset_sprites);
205+
screen.sprites = Surface::load(asset_sprites);
206+
207+
// Attempt to load the first save slot.
208+
if (read_save(saveData)) {
209+
// Loaded sucessfully!
210+
localHighscore = saveData.score;
211+
}
212+
else {
213+
// No save file or it failed to load, set up some defaults.
214+
saveData.score = 0;
215+
}
200216
}
201217

202218
///////////////////////////////////////////////////////////////////////////
@@ -337,6 +353,8 @@ void update(uint32_t time) {
337353
if (player.deadTimer >= DEAD_TIME) {
338354
state = 2;
339355
localHighscore = max(localHighscore, player.score);
356+
saveData.score = localHighscore;
357+
write_save(saveData); // write highscore
340358
}
341359
}
342360
}

metadata.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ splash:
55
file: assets/image.png
66
icon:
77
file: assets/icon.png
8-
version: v1.1.0
8+
version: v1.2.0
99

0 commit comments

Comments
 (0)