From c7617943f370c4d7994b5439360b2a51a569fcbc Mon Sep 17 00:00:00 2001 From: Sheldon Lee Date: Mon, 6 Jul 2020 00:59:39 +0100 Subject: [PATCH] Loading game works. Breaks if dimension > 255 or so. --- game.c | 24 ++++++++++++++++-------- main.c | 3 +-- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/game.c b/game.c index 28a6455..38a7878 100644 --- a/game.c +++ b/game.c @@ -104,6 +104,12 @@ void handleInput(char ch) case 'i': toggleCell(); break; + case 'e': + loadGame("test.txt"); + break; + case 'w': + saveGame("test.txt"); + break; default: if (ch != -1) do_step = true; } @@ -161,14 +167,14 @@ void loadGame(char* name) bool* pixels; int count = 0; char c; + unsigned int size; + + width = (unsigned int)fgetc(file); height = (unsigned int)fgetc(file); + size = width * height; + pixels = (bool*)malloc(sizeof(char)*size); + while((c = fgetc(file)) != EOF) { - if (count == 0) width = (int)c; - else if (count == 1) height = (int)c; - else { - unsigned int size = width * height; - pixels = (bool*)malloc(sizeof(char)*size); - pixels[count-2] = (c == '0'); - } + pixels[count] = (c == '1'); count++; } @@ -176,9 +182,11 @@ void loadGame(char* name) getDimensions(grid, &grid_width, &grid_height); for (int y =0; y < min(grid_height, height); y++) { for (int x =0; x < min(grid_width, width); x++) { - + int index = y * width + x; + setPixel(grid, x, y, pixels[index]); } } + free(pixels); fclose(file); } diff --git a/main.c b/main.c index 23e2ee5..fe79a0b 100644 --- a/main.c +++ b/main.c @@ -18,7 +18,6 @@ int main() startLog("log.txt"); initGame(); - loadGame("test.txt"); float t = 0; while (isRunning()) { @@ -39,7 +38,7 @@ int main() t = (float) (clock()-start_t) / (float) CLOCKS_PER_SEC; } - saveGame("test.txt"); + logLine("oof"); endGame(); endLog();