forked from eugene-kirzhanov/flipper-zero-2048-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_controller.c
87 lines (66 loc) · 2.43 KB
/
game_controller.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#define __game_controller_c
#include "game_controller.h"
#include <furi.h>
#include <gui/gui.h>
#include <input/input.h>
#include <storage/storage.h>
#include "app_shared.h"
static bool game_state_load_callback(GameState* state);
static bool game_state_save_callback(GameState* gamectrl);
void game_controlller_init(GameController* gamectrl) {
game_state_init(&gamectrl->state);
game_state_load(&gamectrl->state, game_state_load_callback);
gamectrl->ui_state = gamectrl->state.is_over ? UIStateGameOver : UIStateInProgress;
}
void game_controller_save_state(GameController* gamectrl) {
game_state_dump(&gamectrl->state, game_state_save_callback);
}
void game_controller_show_menu(GameController* gamectrl) {
gamectrl->ui_state = UIStateMenu;
gamectrl->selected_menu_item = 0;
}
void game_controller_close_menu(GameController* gamectrl) {
gamectrl->ui_state = UIStateInProgress;
}
bool game_state_load_callback(GameState* state) {
Storage* storage = furi_record_open(RECORD_STORAGE);
bool is_loaded = false;
File* file = storage_file_alloc(storage);
uint16_t bytes_readed = 0;
if(storage_file_open(file, SAVE_FILENAME, FSAM_READ, FSOM_OPEN_EXISTING)) {
bytes_readed = storage_file_read(file, state, sizeof(GameState));
is_loaded = true;
}
storage_file_close(file);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
if(is_loaded) {
is_loaded = bytes_readed == sizeof(GameState);
if(is_loaded) {
FURI_LOG_I(LOG_TAG, "Game state loaded");
} else {
FURI_LOG_W(LOG_TAG, "Failed to load game. The storage file contains invalid data.");
}
} else {
FURI_LOG_I(LOG_TAG, "Failed to load game. The storage file does not exist.");
}
return is_loaded;
}
bool game_state_save_callback(GameState* gamectrl) {
Storage* storage = furi_record_open(RECORD_STORAGE);
bool is_saved = false;
File* file = storage_file_alloc(storage);
if(storage_file_open(file, SAVE_FILENAME, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
storage_file_write(file, gamectrl, sizeof(GameState));
is_saved = true;
}
storage_file_close(file);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
if(is_saved) {
FURI_LOG_I(LOG_TAG, "Game state saved");
} else {
FURI_LOG_E(LOG_TAG, "Failed to save game state. The storage file can not be opened.");
}
return is_saved;
}