-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsave.c
87 lines (78 loc) · 1.91 KB
/
save.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdbool.h>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <stdlib.h>
#include "structs.h"
#include "methods.h"
#include "draw.h"
#include "save.h"
char* getPath(char *title){
char *path, *dirname, *ext;
path = (char *)malloc(63*sizeof(char));
dirname = (char *)malloc(7*sizeof(char));
ext = (char *)malloc(5*sizeof(char));
strcpy(dirname,"games/");
strcpy(ext,".isl");
path = strcat(dirname,title);
path = strcat(path,".isl");
return path;
}
void Save(char *title) {
if( strcmp(title,"") == 0 ) return;
FILE * f;
int i,w,h;
w = game.width;
h = game.height;
Move *temp;
f = fopen(getPath(title),"wb");
fwrite(&game.numplayers,sizeof(int),1,f);
fwrite(&game.width,sizeof(int),1,f);
fwrite(&game.height,sizeof(int),1,f);
fwrite(&game.turn,sizeof(int),1,f);
fwrite(&game.step,sizeof(int),1,f);
for(i=0;i<w;i++)
{
fwrite(game.board[i],sizeof(Cell),h,f);
}
fwrite(game.players,sizeof(Player),game.numplayers,f);
temp = game.history;
// l'utilisateur ne peut sauvegarder que si history != NULL
while(temp->prev != NULL) temp = temp -> prev ;
while(temp != game.history->next){
fwrite(temp,sizeof(int),7,f);
temp = temp -> next;
}
fclose(f);
}
void Load(char * name){
FILE * f;
int r,t[7],w,h,i;
f = fopen(name,"rb");
fread(&game.numplayers,sizeof(int),1,f);
fread(&game.width,sizeof(int),1,f);
fread(&game.height,sizeof(int),1,f);
NewGame();
fread(&game.turn,sizeof(int),1,f);
fread(&game.step,sizeof(int),1,f);
w = game.width;
h = game.height;
for(i=0;i<w;i++)
{
fread(game.board[i],sizeof(Cell),h,f);
}
fread(game.players,sizeof(Player),game.numplayers,f);
r = fread(t,sizeof(int),7,f);
while(r == 7){
AddHistory();
game.history->pid = t[0];
game.history->fx = t[1];
game.history->fy = t[2];
game.history->tx = t[3];
game.history->ty = t[4];
game.history->dx = t[5];
game.history->dy = t[6];
r = fread(t,sizeof(int),7,f);
}
fclose(f);
}