2 Copyright (C) 2004-2010 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 if (!fgets(line, 1024, fp)) {
127 graphics.showErrorAndExit("Unexpected end of file reading save data", "");
130 sscanf(line, "%*c %[^\"] %*c %*c %[^\"] %*c %d %d", string[0], string[1], ¶m[0], ¶m[1]);
134 data->set(string[0], string[1], param[0], param[1]);
136 debug(("Read %s %s %d %d\n", data->key, data->value, data->current, data->target));
138 if ((data->current == -1) && (data->target == -1))
144 gameData.addCompletedObjective(data);
150 debug(("Sanity Check #1 > 10000!\n"));
159 int numberOfLines = 0;
161 Persistant *persistant;
162 PersistData *persistData;
166 if (!fgets(line, 1024, fp)) {
168 graphics.showErrorAndExit("Unexpected end of file reading save data", "");
171 sscanf(line, "%[^\n\r]", string[0]);
172 strlcpy(stageName, string[0], sizeof stageName);
174 if (strcmp(stageName, "@EOF@") == 0)
179 if (!fgets(line, 1024, fp)) {
181 graphics.showErrorAndExit("Unexpected end of file reading save data", "");
183 sscanf(line, "%d", &numberOfLines);
185 debug(("Read %s with %d lines.\n", stageName, numberOfLines));
187 persistant = map.createPersistant(stageName);
189 for (int i = 0 ; i < numberOfLines ; i++)
191 persistData = new PersistData();
193 if (!fgets(line, 1024, fp)) {
195 graphics.showErrorAndExit("Unexpected end of file reading save data", "");
198 strlcpy(persistData->data, line, sizeof persistData->data);
200 //debug(("Read %d: %s", i, persistData->data));
202 persistant->addLine(persistData->data);
206 if (sanity == 100000)
208 debug(("Sanity Check #2 > 100000!\n"));
217 debug(("Loaded Successfully\n"));
226 return game.autoSaveSlot;
229 if (!engine.loadWidgets("data/saveWidgets"))
230 graphics.showErrorAndExit(ERR_FILE, "data/saveWidgets");
232 int slot[6], quitYes, quitNo;
233 slot[0] = slot[1] = slot[2] = slot[3] = slot[4] = slot[5] = 0;
234 quitYes = quitNo = 0;
236 engine.setWidgetVariable("slot1", &slot[0]);
237 engine.setWidgetVariable("slot2", &slot[1]);
238 engine.setWidgetVariable("slot3", &slot[2]);
239 engine.setWidgetVariable("slot4", &slot[3]);
240 engine.setWidgetVariable("slot5", &slot[4]);
241 engine.setWidgetVariable("slot6", &slot[5]);
243 engine.setWidgetVariable("contyes", &quitYes);
244 engine.setWidgetVariable("contno", &quitNo);
249 for (int i = 0 ; i < 5 ; i++)
251 snprintf(widgetName, sizeof widgetName, "slot%d", i + 1);
252 strlcpy(engine.getWidgetByName(widgetName)->label, engine.saveSlot[i], sizeof engine.getWidgetByName(widgetName)->label);
255 engine.highlightWidget("slot1");
261 engine.showWidgetGroup("gameSlots", true);
262 engine.showWidgetGroup("continueconf", false);
264 graphics.setFontSize(4);
265 SDL_Surface *title = graphics.quickSprite("savetitle", graphics.getString("Save Game", true));
269 graphics.updateScreen();
270 SDL_FillRect(graphics.screen, NULL, graphics.black);
274 menuSound = engine.processWidgets();
277 audio.playMenuSound(menuSound);
279 graphics.blit(title, 320, 100, graphics.screen, true);
285 engine.showWidgetGroup("gameSlots", false);
286 engine.showWidgetGroup("continueconf", true);
287 engine.highlightWidget("contno");
299 engine.showWidgetGroup("gameSlots", true);
300 engine.showWidgetGroup("continueconf", false);
301 engine.highlightWidget("slot1");
306 for (int i = 0 ; i < 5 ; i++)
314 if ((slot[0]) || (slot[1]) || (slot[2]) || (slot[3]) || (slot[4]) || (slot[5]))
322 SDL_FillRect(graphics.screen, NULL, graphics.black);
323 graphics.updateScreen();
333 SDL_FillRect(graphics.screen, NULL, graphics.black);
334 graphics.updateScreen();
337 int slot = confirmSave();
342 graphics.setFontSize(1);
343 graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
344 snprintf(message, sizeof message, _("Saving Game to Save Slot #%d. Please Wait..."), slot + 1);
345 graphics.drawString(message, 320, 220, true, graphics.screen);
346 graphics.updateScreen();
348 char filename[PATH_MAX];
352 snprintf(filename, sizeof filename, "%ssave%d.dat", engine.userHomeDirectory, slot);
354 fp = fopen(filename, "wb");
358 graphics.showErrorAndExit("File write error whilst saving game", "");
361 if (fwrite(&game, sizeof(Game), 1, fp) != 1)
364 graphics.showErrorAndExit("File write error whilst saving game", strerror(errno));
369 snprintf(filename, sizeof filename, "%spersistant%d.dat", engine.userHomeDirectory, slot);
371 fp = fopen(filename, "wt");
375 graphics.showErrorAndExit("File write error whilst saving game", "");
378 createPersistantMapData();
380 Data *data = (Data*)gameData.dataList.getHead();
382 while (data->next != NULL)
384 data = (Data*)data->next;
386 fprintf(fp, "\"%s\" \"%s\" %d %d\n", data->key, data->value, data->current, data->target);
389 fprintf(fp, "\"@EOF@\" \"@EOF@\" -1 -1\n");
391 Persistant *persistant = (Persistant*)map.persistantList.getHead();
392 PersistData *persistData;
394 while (persistant->next != NULL)
396 persistant = (Persistant*)persistant->next;
398 if (strcmp(persistant->stageName, "@none@") == 0)
403 fprintf(fp, "%s\n", persistant->stageName);
404 fprintf(fp, "%d\n", persistant->numberOfLines);
406 persistData = (PersistData*)persistant->dataList.getHead();
408 while (persistData->next != NULL)
410 persistData = (PersistData*)persistData->next;
412 fprintf(fp, "%s", persistData->data);
416 fprintf(fp, "@EOF@\n");
424 graphics.drawString(_("Save Complete"), 320, 260, true, graphics.screen);
425 graphics.updateScreen();