-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.c
91 lines (73 loc) · 1.77 KB
/
file.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
88
89
90
91
#include <stdlib.h>
#include "define.h"
#include "file.h"
int loadFile(Map* gameMap){
FILE* mapFile = NULL;
int ch = '/';
int x = 0;
int y = 0;
mapFile = fopen(FILE_NAME, "r");
if(mapFile == NULL){
return -1;
}
for(int y = 0; y < NB_BLOC_Y; y++){
for(int x = 0; x < NB_BLOC_X; x++){
fread(&ch, sizeof(char), 1, mapFile);
switch (ch)
{
case '/':
gameMap->map[x][y] = MUR;
break;
case '*':
gameMap->map[x][y] = OBJECTIF;
break;
case 'm':
gameMap->map[x][y] = MARIO;
gameMap->positionM.x = x;
gameMap->positionM.y = y;
break;
case 'c':
gameMap->map[x][y] = CAISSE;
break;
}
}
}
fclose(mapFile);
return 1;
}
int save(Map *map){
FILE* mapFile = NULL;
int ch = '/';
int x = 0;
int y = 0;
mapFile = fopen(FILE_NAME, "w");
if(mapFile == NULL){
return -1;
}
for(int y = 0; y < NB_BLOC_Y; y++){
for(int x = 0; x < NB_BLOC_X; x++){
// fread(&ch, sizeof(char), 1, mapFile);
switch (map->map[x][y])
{
case VIDE:
ch = ' ';
break;
case OBJECTIF:
ch = '*';
break;
case MARIO:
ch = 'm';
break;
case CAISSE:
ch = 'c';
break;
case MUR:
ch = '/';
break;
}
fputc(ch ,mapFile);
}
}
fclose(mapFile);
return 1;
}