Progress on loading game.
This commit is contained in:
parent
4307992cbc
commit
e912de2a35
54
game.c
54
game.c
@ -1,4 +1,5 @@
|
||||
#include <ncurses.h>
|
||||
#include <stdlib.h>
|
||||
#include "game.h"
|
||||
#include "grid.h"
|
||||
#include "vect.h"
|
||||
@ -16,6 +17,10 @@ static void toggleStepMode();
|
||||
|
||||
static void toggleCell();
|
||||
|
||||
int min (int a, int b);
|
||||
|
||||
int max (int a, int b);
|
||||
|
||||
void initGame()
|
||||
{
|
||||
// init
|
||||
@ -128,14 +133,16 @@ void saveGame(char* name)
|
||||
unsigned int width, height;
|
||||
getDimensions(grid, &width, &height);
|
||||
|
||||
FILE* file = fopen(name, "w");
|
||||
|
||||
FILE* file = NULL;
|
||||
file = fopen(name, "w");
|
||||
|
||||
fprintf(file, "%c%c", width, height);
|
||||
if (file) {
|
||||
for (int y = 0; y < height; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
fprintf(file, "%i", getPixel(grid, x, y));
|
||||
}
|
||||
fprintf(file, "\n");
|
||||
}
|
||||
fclose(file);
|
||||
}
|
||||
@ -143,6 +150,37 @@ void saveGame(char* name)
|
||||
|
||||
void loadGame(char* name)
|
||||
{
|
||||
FILE* file = NULL;
|
||||
file = fopen(name, "r");
|
||||
if (!file) return;
|
||||
|
||||
if (!grid) initGame();
|
||||
else clearGrid(grid);
|
||||
|
||||
unsigned int width, height;
|
||||
bool* pixels;
|
||||
int count = 0;
|
||||
char c;
|
||||
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');
|
||||
}
|
||||
count++;
|
||||
}
|
||||
|
||||
unsigned int grid_width, grid_height;
|
||||
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++) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
void endGame()
|
||||
@ -167,3 +205,15 @@ static void toggleCell()
|
||||
else setPixel(grid, cursor.x, cursor.y, 1);
|
||||
|
||||
}
|
||||
|
||||
int min(int a, int b)
|
||||
{
|
||||
if (a <= b) return a;
|
||||
else return b;
|
||||
}
|
||||
|
||||
int max(int a, int b)
|
||||
{
|
||||
if (a >= b) return a;
|
||||
else return b;
|
||||
}
|
||||
|
3
main.c
3
main.c
@ -18,6 +18,7 @@ int main()
|
||||
startLog("log.txt");
|
||||
|
||||
initGame();
|
||||
loadGame("test.txt");
|
||||
|
||||
float t = 0;
|
||||
while (isRunning()) {
|
||||
@ -35,7 +36,7 @@ int main()
|
||||
updateGame();
|
||||
|
||||
usleep(pow(10,6)*(FRAME_TIME-t));
|
||||
float t = (float) (clock()-start_t) / (float) CLOCKS_PER_SEC;
|
||||
t = (float) (clock()-start_t) / (float) CLOCKS_PER_SEC;
|
||||
}
|
||||
|
||||
saveGame("test.txt");
|
||||
|
Loading…
Reference in New Issue
Block a user