2 Copyright (C) 2004 Parallel Realities
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 char filename[PATH_MAX];
32 engine.continueSaveSlot = 0;
37 for (int i = 0 ; i < 5 ; i++)
39 snprintf(filename, sizeof filename, "%ssave%d.dat", engine.userHomeDirectory, i);
41 fp = fopen(filename, "rb");
45 strlcpy(string, "%.2d - %s", sizeof string);
46 snprintf(engine.saveSlot[i], sizeof engine.saveSlot[i], string, (i + 1), _("Empty"));
50 if (fread(&tempGame, sizeof(Game), 1, fp) != 1)
52 strlcpy(string, "%.2d - %s", sizeof string);
53 snprintf(engine.saveSlot[i], sizeof engine.saveSlot[i], string, (i + 1), _("Corrupt Save Data"));
57 snprintf(engine.saveSlot[i], sizeof engine.saveSlot[i], "%.2d - %s (%.2d:%.2d:%.2d)", (i + 1), _(tempGame.stageName), tempGame.totalHours, tempGame.totalMinutes, tempGame.totalSeconds);
60 if (stat(filename, &fileInfo) != -1)
62 if (fileInfo.st_mtime > modTime)
64 modTime = fileInfo.st_mtime;
65 engine.continueSaveSlot = (i + 1);
73 debug(("Continue Save Slot = %d\n", engine.continueSaveSlot));
79 bool loadGame(int slot)
81 debug(("Loading Game #%d...\n", slot));
85 char filename[PATH_MAX];
97 snprintf(filename, sizeof filename, "%ssave%d.dat", engine.userHomeDirectory, slot);
99 fp = fopen(filename, "rb");
106 if (fread(&game, sizeof(Game), 1, fp) != 1)
109 graphics.showErrorAndExit("The save data loaded was not in the format expected", "");
114 snprintf(filename, sizeof filename, "%spersistant%d.dat", engine.userHomeDirectory, slot);
116 fp = fopen(filename, "rb");
125 fgets(line, 1024, fp);
127 sscanf(line, "%*c %[^\"] %*c %*c %[^\"] %*c %d %d", string[0], string[1], ¶m[0], ¶m[1]);
131 data->set(string[0], string[1], param[0], param[1]);
133 debug(("Read %s %s %d %d\n", data->key, data->value, data->current, data->target));
135 if ((data->current == -1) && (data->target == -1))
141 gameData.addCompletedObjective(data);
147 debug(("Sanity Check #1 > 10000!\n"));
156 int numberOfLines = 0;
158 Persistant *persistant;
159 PersistData *persistData;
163 fgets(line, 1024, fp);
165 sscanf(line, "%[^\n\r]", string[0]);
166 strlcpy(stageName, string[0], sizeof stageName);
168 if (strcmp(stageName, "@EOF@") == 0)
173 fgets(line, 1024, fp);
174 sscanf(line, "%d", &numberOfLines);
176 debug(("Read %s with %d lines.\n", stageName, numberOfLines));
178 persistant = map.createPersistant(stageName);
180 for (int i = 0 ; i < numberOfLines ; i++)
182 persistData = new PersistData();
184 fgets(line, 1024, fp);
186 strlcpy(persistData->data, line, sizeof persistData->data);
188 //debug(("Read %d: %s", i, persistData->data));
190 persistant->addLine(persistData->data);
194 if (sanity == 100000)
196 debug(("Sanity Check #2 > 100000!\n"));
205 debug(("Loaded Successfully\n"));
214 return game.autoSaveSlot;
217 if (!engine.loadWidgets("data/saveWidgets"))
218 graphics.showErrorAndExit(ERR_FILE, "data/saveWidgets");
220 int slot[6], quitYes, quitNo;
221 slot[0] = slot[1] = slot[2] = slot[3] = slot[4] = slot[5] = 0;
222 quitYes = quitNo = 0;
224 engine.setWidgetVariable("slot1", &slot[0]);
225 engine.setWidgetVariable("slot2", &slot[1]);
226 engine.setWidgetVariable("slot3", &slot[2]);
227 engine.setWidgetVariable("slot4", &slot[3]);
228 engine.setWidgetVariable("slot5", &slot[4]);
229 engine.setWidgetVariable("slot6", &slot[5]);
231 engine.setWidgetVariable("contyes", &quitYes);
232 engine.setWidgetVariable("contno", &quitNo);
237 for (int i = 0 ; i < 5 ; i++)
239 snprintf(widgetName, sizeof widgetName, "slot%d", i + 1);
240 strlcpy(engine.getWidgetByName(widgetName)->label, engine.saveSlot[i], sizeof engine.getWidgetByName(widgetName)->label);
243 engine.highlightWidget("slot1");
249 engine.showWidgetGroup("gameSlots", true);
250 engine.showWidgetGroup("continueconf", false);
252 graphics.setFontSize(4);
253 SDL_Surface *title = graphics.quickSprite("savetitle", graphics.getString("Save Game", true));
257 graphics.updateScreen();
258 SDL_FillRect(graphics.screen, NULL, graphics.black);
262 menuSound = engine.processWidgets();
265 audio.playMenuSound(menuSound);
267 graphics.blit(title, 320, 100, graphics.screen, true);
273 engine.showWidgetGroup("gameSlots", false);
274 engine.showWidgetGroup("continueconf", true);
275 engine.highlightWidget("contno");
287 engine.showWidgetGroup("gameSlots", true);
288 engine.showWidgetGroup("continueconf", false);
289 engine.highlightWidget("slot1");
294 for (int i = 0 ; i < 5 ; i++)
302 if ((slot[0]) || (slot[1]) || (slot[2]) || (slot[3]) || (slot[4]) || (slot[5]))
310 SDL_FillRect(graphics.screen, NULL, graphics.black);
311 graphics.updateScreen();
321 SDL_FillRect(graphics.screen, NULL, graphics.black);
322 graphics.updateScreen();
325 int slot = confirmSave();
330 graphics.setFontSize(1);
331 graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
332 snprintf(message, sizeof message, _("Saving Game to Save Slot #%d. Please Wait..."), slot + 1);
333 graphics.drawString(message, 320, 220, true, graphics.screen);
334 graphics.updateScreen();
336 char filename[PATH_MAX];
340 snprintf(filename, sizeof filename, "%ssave%d.dat", engine.userHomeDirectory, slot);
342 fp = fopen(filename, "wb");
346 graphics.showErrorAndExit("File write error whilst saving game", "");
349 fwrite(&game, sizeof(Game), 1, fp);
353 snprintf(filename, sizeof filename, "%spersistant%d.dat", engine.userHomeDirectory, slot);
355 fp = fopen(filename, "wt");
359 graphics.showErrorAndExit("File write error whilst saving game", "");
362 createPersistantMapData();
364 Data *data = (Data*)gameData.dataList.getHead();
366 while (data->next != NULL)
368 data = (Data*)data->next;
370 fprintf(fp, "\"%s\" \"%s\" %d %d\n", data->key, data->value, data->current, data->target);
373 fprintf(fp, "\"@EOF@\" \"@EOF@\" -1 -1\n");
375 Persistant *persistant = (Persistant*)map.persistantList.getHead();
376 PersistData *persistData;
378 while (persistant->next != NULL)
380 persistant = (Persistant*)persistant->next;
382 if (strcmp(persistant->stageName, "@none@") == 0)
387 fprintf(fp, "%s\n", persistant->stageName);
388 fprintf(fp, "%d\n", persistant->numberOfLines);
390 persistData = (PersistData*)persistant->dataList.getHead();
392 while (persistData->next != NULL)
394 persistData = (PersistData*)persistData->next;
396 fprintf(fp, "%s", persistData->data);
400 fprintf(fp, "@EOF@\n");
408 graphics.drawString(_("Save Complete"), 320, 260, true, graphics.screen);
409 graphics.updateScreen();