From c40e2a770a1a1c039c54eacb80f9f6ab27ea7b65 Mon Sep 17 00:00:00 2001 From: alistair Date: Mon, 10 May 2021 00:08:17 +1000 Subject: [PATCH] serializing controlscheme --- src/controlscheme.c | 78 +++++++++++++++++++++++++++++++++++++++++++++ src/controlscheme.h | 1 + src/main.c | 15 +++++++++ 3 files changed, 94 insertions(+) diff --git a/src/controlscheme.c b/src/controlscheme.c index 9a4c129..b483800 100644 --- a/src/controlscheme.c +++ b/src/controlscheme.c @@ -1,10 +1,28 @@ #include "controlscheme.h" #include +#include struct InputMap input_map; +#define INPUT_MAP_ID 83921864 +#define INPUT_MAP_VERSION 0 +#define CONTROLS_FNAME "controls.bin" + +struct InputMap_Ser { + unsigned int id; + unsigned int ver; + struct InputMap input_map; +}; + +int load_controls(void); + void get_input_map(void) { + + if (load_controls()) { + return; + } + input_map.player_down = SDL_SCANCODE_S; input_map.player_up = SDL_SCANCODE_W; input_map.player_right = SDL_SCANCODE_D; @@ -16,3 +34,63 @@ void get_input_map(void) { input_map.quit = SDL_SCANCODE_Q; } +struct Buffer { + size_t len; + char *data; +}; + +struct Buffer read_whole_file(char const *file_name) { + struct stat sb; + struct Buffer b = {}; + + if (stat (file_name, & sb) != 0) { + return b; + } + + char *buffer = malloc(sb.st_size + 1); + + FILE *f = fopen(file_name, "r"); + size_t bytes_read = fread(buffer, sizeof (unsigned char), sb.st_size, f); + if (bytes_read != sb.st_size) { + fclose(f); + free(buffer); + return b; + } + + fclose(f); + + b.data = buffer; + b.len = bytes_read; + return b; +} + +int load_controls(void) { + struct Buffer b = read_whole_file(CONTROLS_FNAME); + + if (b.len != sizeof (struct InputMap_Ser)) { + return 0; + } + + struct InputMap_Ser *im = (struct InputMap_Ser *)b.data; + + if (im->id != INPUT_MAP_ID) + return 0; + + if (im->ver != INPUT_MAP_VERSION) + return 0; + + memcpy(&input_map, &im->input_map, sizeof(struct InputMap)); + return 1; +} + +void save_controls(void) { + FILE *f = fopen(CONTROLS_FNAME, "w"); + + struct InputMap_Ser im; + im.id = INPUT_MAP_ID; + im.ver = INPUT_MAP_VERSION; + im.input_map = input_map; + + fwrite(&im, sizeof(unsigned char), sizeof(struct InputMap_Ser), f); + fclose(f); +} diff --git a/src/controlscheme.h b/src/controlscheme.h index 62657d3..faf55bf 100644 --- a/src/controlscheme.h +++ b/src/controlscheme.h @@ -25,6 +25,7 @@ struct InputMap { extern struct InputMap input_map; void get_input_map(void); +void save_controls(void); #endif diff --git a/src/main.c b/src/main.c index ad41a71..19d1612 100644 --- a/src/main.c +++ b/src/main.c @@ -72,6 +72,8 @@ int physics_loop(void *ptr) { } } + + int game(void) { //SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "2" ); @@ -161,3 +163,16 @@ int main (int argc, char** argv) { return 0; } + + +/* + * TODO: + * - Allow setting and saving/loading differnet control schemes + * - Allow starting a specific level + * - Allow adjusging level lengths? + * - Ensure next_level doesn't leak memory + * - fix that weird jitter + * - make the end of level look sane + * - restart level + */ +