/* Copyright (C) 2005-2007 Tom Beaumont This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ struct HexPuzzle; #include #if SDL_BYTEORDER == SDL_LIL_ENDIAN #define SWAP16(X) (X) #define SWAP32(X) (X) #else #define SWAP16(X) SDL_Swap16(X) #define SWAP32(X) SDL_Swap32(X) #endif class LevelSave { friend struct HexPuzzle; // CHECKME: If char is larger than 8 bits (== 1 byte???) // the code is no longer big endian save? SWAP16/32 is necessary? // Or is a byte always of the same size as char, e.g. 16 bits, so // that int16_t is equally saved on big and little endian systems? char * bestSolution; int32_t bestSolutionLength; int32_t bestScore; #define NUM_LAST_SCORES 19 int32_t lastScores[NUM_LAST_SCORES]; int32_t unlocked; public: LevelSave() { Clear(); } void Clear() { unlocked = 0; bestSolution = 0; bestScore = 0; bestSolutionLength = 0; memset(lastScores, 0, sizeof(lastScores)); } void LoadSave(FILE* f, bool save) { typedef unsigned int _fn(void*, unsigned int, unsigned int, FILE*); _fn * fn = save ? (_fn*)fwrite : (_fn*)fread; // we write little endian data bestSolutionLength = SWAP32(bestSolutionLength); bestScore = SWAP32(bestScore); for (int i=0; inext; delete x; x = nx; } first = 0; } public: General general; SaveState() : first(0) { Clear(); } ~SaveState() { ClearRaw(); } void Clear() { ClearRaw(); ApplyStuff(); } void GetStuff(); void ApplyStuff(); void LoadSave(FILE* f, bool save) { if (save) { GetStuff(); //printf("----\n"); fputc('2', f); general.SwapBytes(); // big==>little endian fwrite(&general, sizeof(general), 1, f); general.SwapBytes(); // revert changes for(X* x=first; x; x=x->next) { int16_t len = strlen(x->name); len = SWAP16(len); fwrite(&len, sizeof(len), 1, f); len = SWAP16(len); fwrite(x->name, 1, len, f); x->LoadSave(f,save); if (x->Completed()) { //printf("% 8d %s\n", x->GetScore(), x->name); x->Dump(); } } } else { ClearRaw(); int v = fgetc(f); if (v=='2') { fread(&general, sizeof(general), 1, f); general.SwapBytes(); v = '1'; } if (v=='1') { while(!feof(f)) { char temp[1000]; int16_t len; fread(&len, sizeof(len), 1, f); len = SWAP16(len); if (feof(f)) break; fread(temp, len, 1, f); temp[len] = 0; first = new X(temp, first); first->LoadSave(f,save); } } ApplyStuff(); } } LevelSave* GetLevel(const char * name, bool create) { const char * l = strstr(name, "Levels"); if (l) name = l; X* x = first; if (x && strcmp(name, x->name)==0) return x; while (x && x->next) { if (strcmp(name, x->next->name)==0) return x->next; x = x->next; } if (create) { X* n = new X(name); if (x) x->next = n; else first = n; return n; } else { return 0; } } };