Loading game works. Breaks if dimension > 255 or so.

This commit is contained in:
Sheldon Lee 2020-07-06 00:59:39 +01:00
parent 6a23dd6667
commit c7617943f3
2 changed files with 17 additions and 10 deletions

24
game.c
View File

@ -104,6 +104,12 @@ void handleInput(char ch)
case 'i': case 'i':
toggleCell(); toggleCell();
break; break;
case 'e':
loadGame("test.txt");
break;
case 'w':
saveGame("test.txt");
break;
default: default:
if (ch != -1) do_step = true; if (ch != -1) do_step = true;
} }
@ -161,14 +167,14 @@ void loadGame(char* name)
bool* pixels; bool* pixels;
int count = 0; int count = 0;
char c; 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) { while((c = fgetc(file)) != EOF) {
if (count == 0) width = (int)c; pixels[count] = (c == '1');
else if (count == 1) height = (int)c;
else {
unsigned int size = width * height;
pixels = (bool*)malloc(sizeof(char)*size);
pixels[count-2] = (c == '0');
}
count++; count++;
} }
@ -176,9 +182,11 @@ void loadGame(char* name)
getDimensions(grid, &grid_width, &grid_height); getDimensions(grid, &grid_width, &grid_height);
for (int y =0; y < min(grid_height, height); y++) { for (int y =0; y < min(grid_height, height); y++) {
for (int x =0; x < min(grid_width, width); x++) { 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); fclose(file);
} }

3
main.c
View File

@ -18,7 +18,6 @@ int main()
startLog("log.txt"); startLog("log.txt");
initGame(); initGame();
loadGame("test.txt");
float t = 0; float t = 0;
while (isRunning()) { while (isRunning()) {
@ -39,7 +38,7 @@ int main()
t = (float) (clock()-start_t) / (float) CLOCKS_PER_SEC; t = (float) (clock()-start_t) / (float) CLOCKS_PER_SEC;
} }
saveGame("test.txt"); logLine("oof");
endGame(); endGame();
endLog(); endLog();