summary refs log tree commit diff
path: root/src/main.c
diff options
context:
space:
mode:
authorzlago2024-10-26 20:18:41 +0200
committerzlago2024-10-26 20:21:59 +0200
commitd44d411e0eb3800ed883374b29c2863e1a863735 (patch)
tree05721dbc8bc1df747179fcb62120a009f23d86a3 /src/main.c
parentb12606899c98d7fc7a120c2b79797b5c45283ad2 (diff)
move save file code to a separate file
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c48
1 files changed, 20 insertions, 28 deletions
diff --git a/src/main.c b/src/main.c
index 7afd152..9c40876 100644
--- a/src/main.c
+++ b/src/main.c
@@ -10,6 +10,7 @@
 #include "input.h"
 #include "loader.h"
 #include "util.h"
+#include "save.h"
 
 #include "tilemap.h"
 
@@ -222,6 +223,8 @@ void game_render(SDL_Texture *framebuffer, int x, int y) {
 	}
 }
 
+int fade = 255;
+
 int game_load_level(char *level) {
 	struct blob blob = res_get_map(level);
 	next_tilemap = tilemap_load(blob.data, blob.size);
@@ -236,6 +239,7 @@ int game_load_level(char *level) {
 			memcpy(&entities, &next_entities, sizeof (entities));
 
 			game_state = STATE_FADE_OUT;
+			fade = 255;
 			int x = (entities.player[0].x / 16) - (WINDOW_WIDTH / 2);
 			int y = (entities.player[0].y / 16) - (WINDOW_HEIGHT / 2);
 			if (x < 0) {x = 0;} else if (x + WINDOW_WIDTH > tilemap->width * 8) {x = tilemap->width * 8 - WINDOW_WIDTH;}
@@ -417,31 +421,7 @@ int main(int const argc, char *const *const argv) {
 			player_property(next_entities.player, "y", "64");
 			game_load_level("untitled");
 		} else {
-			size_t filesize, len;
-			char *filedata = util_loadFile(file, &filesize); // hack: we leak a tiny bit of memory here
-			fclose(file);
-			char *level = filedata;
-			len = strnlen(filedata, filesize);
-			if (len == filesize) {
-				fputs("invalid save format\n", stderr);
-				goto end;
-			}
-			filedata += len + 1;
-			len = strnlen(filedata, filesize);
-			if (len == filesize) {
-				fputs("invalid save format\n", stderr);
-				goto end;
-			}
-			player_property(next_entities.player, "x", filedata);
-			filedata += len + 1;
-			len = strnlen(filedata, filesize);
-			if (len == filesize) {
-				fputs("invalid save format\n", stderr);
-				goto end;
-			}
-			player_property(next_entities.player, "y", filedata);
-			level = realloc(level, strlen(level) + 1);
-			game_load_level(level);
+			game_load(file);
 		}
 		#if 0
 		struct blob blob = res_get_map("untitled");
@@ -490,11 +470,11 @@ int main(int const argc, char *const *const argv) {
 	return 0;
 }
 
-int x = 0, y = 0, fade = 255;
+int x = 0, y = 0;
 
 void main_loop(void) {
 #else
-	int x = 0, y = 0, fade = 255;
+	int x = 0, y = 0;
 	while (1) {
 #endif
 		input_pressed = input_held;
@@ -649,7 +629,19 @@ void main_loop(void) {
 			case STATE_PLAYING:
 				//SDL_RenderCopy(renderer, tilemap->wang_tileset, &(SDL_Rect) {0, 0, 128, 90}, &(SDL_Rect) {0, 0, 128, 90});
 				if (game_update()) {
-					goto end; // TODO: game over state
+					//goto end; // TODO: game over state
+					FILE *file = fopen(save_file_name, "rb");
+					if (file == NULL) {
+						if (errno != ENOENT) {
+							perror(save_file_name);
+							goto end;
+						}
+						player_property(next_entities.player, "x", "40");
+						player_property(next_entities.player, "y", "64");
+						game_load_level("untitled");
+					} else {
+						game_load(file);
+					}
 				}
 				
 				x = (entities.player[0].x / 16) - (WINDOW_WIDTH / 2);