-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_operations.c
188 lines (175 loc) · 5.07 KB
/
file_operations.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*funções de leitura e escrita em arquivos*/
#include "file_operations.h"
#include "lists.h"
//Carrega o arquivo texto do cenário inicial
FILE* load_level(int level){
FILE *level_file = NULL;
char buffer[10];
sprintf(buffer, "fase%d.txt", level);
if((level_file = fopen(buffer, "rb")) == NULL)
printf("erro ao abrir arquivo de fase\n");
return level_file;
}
//Gera a matriz do mapa a partir do arquivo de texto
void make_map(FILE *level, chtype (*p)[MAX_X]){
char buffer;
rewind(level);
//Atualiza a matriz, ignorando caracteres fora do padrão
for(int i = 0; i < MAX_Y; i++){
for(int j = 0; j < MAX_X; j++){
if(fread(&buffer, sizeof(char), 1, level) != 1){
printw("erro na leitura do arquivo\n");
}
p[i][j] = (chtype)buffer;
}
//pula \n
if(fseek(level, sizeof(char), SEEK_CUR) != 0){
printw("erro ao ler arquivo\n");
}
}
fclose(level);
}
//le o arquivo binario com a lista de sprites e recria as lista para dar continuidade ao jogo
void load_state(struct sprite_list *sprite_list){
FILE *game_state_file;
if((game_state_file = fopen("estado.bin", "rb")) == NULL){
printw("erro ao abrir arquivo de scores\n");
}
else{
SPRITE current;
//le o score e o level no inicio do arquivo
fread(&(game_state.score), sizeof(int), 1, game_state_file);
fread(&(game_state.level), sizeof(int), 1, game_state_file);
//le as listas de sprites de cada tipo em sprite_list
for (int i = 0; i < sizeof(*sprite_list)/sizeof(SPRITE*); i++) {
do{
fread(¤t, sizeof(SPRITE), 1, game_state_file);
switch(current.representation){
case CH_SPACE:
push(&(sprite_list->spaces), current);
break;
case CH_WALL:
push(&(sprite_list->walls), current);
break;
case CH_FRUIT:
push(&(sprite_list->fruits), current);
break;
case CH_GHOST:
push(&(sprite_list->ghosts), current);
break;
case CH_SHOT:
push(&(sprite_list->shot), current);
break;
case CH_NEST:
push(&(sprite_list->nest), current);
break;
case CH_MR_DO:
push(&(sprite_list->mr_do), current);
break;
}
} while(current.next != NULL);
}
fclose(game_state_file);
}
}
//Salva o estado atual do jogo em um arquivo binário
void save_state(struct sprite_list sl){
//salva lista de sprites
FILE *game_state_file;
game_state_file = fopen("estado.bin", "wb+");
if (game_state_file) {
//salva o score o level no começo do arquivo
fwrite(&(game_state.score), sizeof(int), 1, game_state_file);
fwrite(&(game_state.level), sizeof(int), 1, game_state_file);
SPRITE *list[] = {sl.spaces, sl.walls, sl.fruits, sl.ghosts, sl.nest, sl.mr_do, sl.shot};
//salva todas as listas no arquivo binário
for (int i = 0; i < sizeof(list)/sizeof(SPRITE*); i++) {
SPRITE *current = list[i];
while(current != NULL){
fwrite(current, sizeof(SPRITE), 1, game_state_file);
current = current->next;
}
}
fclose(game_state_file);
}
else{
printw("erro ao escrever no arquivo\n");
}
}
//salva o mapa atual em um arquivo texto
void save_map(WINDOW *game_window){
chtype buffer[MAX_Y][MAX_X];
for(int i = 0; i < MAX_Y; i++){
for(int j = 0; j < MAX_X; j++){
buffer[i][j] = mvwinch(game_window, i, j);
switch(buffer[i][j]){
case CH_WALL:
buffer[i][j] = 'p';
break;
case CH_SHOT:
buffer[i][j] = 't';
break;
case CH_GHOST:
buffer[i][j] = 'i';
break;
case CH_FRUIT:
buffer[i][j] = 'f';
break;
case CH_MR_DO:
buffer[i][j] = 'd';
break;
case CH_NEST:
buffer[i][j] = 'n';
break;
case CH_SPACE:
buffer[i][j] = 'v';
break;
}
}
}
FILE *cont_map;
cont_map = fopen("continuar.txt", "wb+");
if (cont_map) {
for (int i=0; i < MAX_Y; i++){
for(int j = 0; j < MAX_X; j++){
fprintf(cont_map, "%c", (char)buffer[i][j]);
}
fprintf(cont_map, "%s", "\n");
}
fclose(cont_map);
}
else{
printw("erro ao abrir arquivo\n");
}
}
void save_score(struct score *hi_score){
FILE *hi_score_file;
hi_score_file = fopen("highscores.bin","wb+");
if (hi_score_file){
for(int i = TOP_SCORES; i >= 0 ; i--){
fwrite(&hi_score[i], sizeof(struct score), 1, hi_score_file);
}
fclose(hi_score_file);
}
else{
printw("erro ao abrir arquivo\n");
}
}
void load_score(struct score *hi_score){
FILE *hi_score_file;
hi_score_file = fopen("highscores.bin","rb");
if (!hi_score_file){
for(int i = 0; i < TOP_SCORES; i++){
hi_score[i].name[0] = '\0';
hi_score[i].score = 0;
}
}else{
for(int i = 0; i < TOP_SCORES; i++){
if(!fread(&hi_score[i], sizeof(struct score), 1, hi_score_file)){
hi_score[i].name[0] = '\0';
hi_score[i].score = 0;
}
}
fclose(hi_score_file);
}
}