/* =========================================================================== blockattack - Block Attack - Rise of the Blocks Copyright (C) 2005-2012 Poul Sander 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, see http://www.gnu.org/licenses/ Source information and contacts persons can be found at http://blockattack.sf.net =========================================================================== */ /* Handles replay */ #include #include "replay.h" Replay::Replay() { nrOfFrames=0; isLoaded = false; } Replay::Replay(const Replay& r) { bps = r.bps; finalPack = r.finalPack; nrOfFrames = r.nrOfFrames; isLoaded = r.isLoaded; theResult = r.theResult; strncpy(name,r.name,sizeof(name)); } Uint32 Replay::getNumberOfFrames() { return nrOfFrames; } void Replay::setFrameSecTo(Uint32 miliseconds, boardPackage bp) { if (isLoaded) return; int framesToSet = (miliseconds*FRAMESPERSEC)/1000; for (int i=nrOfFrames; i(&version),sizeof(Uint32)); //Fileversion Uint8 nrOfReplays = 1; saveFile.write(reinterpret_cast(&nrOfReplays),sizeof(Uint8)); //nrOfReplaysIn File saveFile.write(reinterpret_cast(&nrOfFrames),sizeof(Uint32)); //Nr of frames in file for (int i=0; i(&bp),sizeof(bp)); } saveFile.write(reinterpret_cast(&finalPack),sizeof(finalPack)); saveFile.write(reinterpret_cast(&theResult),sizeof(theResult)); saveFile.write(reinterpret_cast(&name),sizeof(name)); saveFile.close(); return true; } else { return false; } } bool Replay::saveReplay(string filename,Replay p2) { //Saving as fileversion 3 cout << "Saving as version 3 save file (2 players)" << endl; ofstream saveFile; saveFile.open(filename.c_str(),ios::binary|ios::trunc); if (saveFile) { Uint32 version = 3; boardPackage bp; saveFile.write(reinterpret_cast(&version),sizeof(Uint32)); //Fileversion Uint8 nrOfReplays = 2; saveFile.write(reinterpret_cast(&nrOfReplays),sizeof(Uint8)); //nrOfReplaysIn File saveFile.write(reinterpret_cast(&nrOfFrames),sizeof(Uint32)); //Nr of frames in file for (int i=0; i(&bp),sizeof(bp)); } saveFile.write(reinterpret_cast(&finalPack),sizeof(finalPack)); saveFile.write(reinterpret_cast(&theResult),sizeof(theResult)); saveFile.write(reinterpret_cast(&name),sizeof(name)); ///Player 2 starts here!!!!!!!!!!!!!!!!!!!!!! saveFile.write(reinterpret_cast(&p2.nrOfFrames),sizeof(Uint32)); //Nr of frames in file for (int i=0; (i(&bp),sizeof(bp)); } saveFile.write(reinterpret_cast(&p2.finalPack),sizeof(finalPack)); saveFile.write(reinterpret_cast(&p2.theResult),sizeof(theResult)); saveFile.write(reinterpret_cast(&p2.name),sizeof(name)); saveFile.close(); return true; } else { return false; } } bool Replay::loadReplay(string filename) { isLoaded = true; ifstream loadFile; loadFile.open(filename.c_str(),ios::binary); if (loadFile) { Uint32 version; loadFile.read(reinterpret_cast(&version),sizeof(Uint32)); switch (version) { case 3: cout << "Loading a version 3 save game" << endl; Uint8 nrOfPlayers; boardPackage bp; loadFile.read(reinterpret_cast(&nrOfPlayers),sizeof(Uint8)); loadFile.read(reinterpret_cast(&nrOfFrames),sizeof(Uint32)); bps.clear(); for (int i=0; (i(&bp),sizeof(bp)); bps.push_back(bp); } loadFile.read(reinterpret_cast(&finalPack),sizeof(finalPack)); loadFile.read(reinterpret_cast(&theResult),sizeof(theResult)); loadFile.read(reinterpret_cast(&name),sizeof(name)); break; default: cout << "Unknown version" << endl; return false; }; loadFile.close(); cout << "Loaded 1 player" << endl; } else { cout << "File not found or couldn't open: " << filename << endl; return false; } } bool Replay::loadReplay2(string filename) { isLoaded = true; ifstream loadFile; loadFile.open(filename.c_str(),ios::binary); if (loadFile) { Uint32 version; loadFile.read(reinterpret_cast(&version),sizeof(Uint32)); switch (version) { case 3: Uint8 nrOfPlayers; boardPackage bp; loadFile.read(reinterpret_cast(&nrOfPlayers),sizeof(Uint8)); if (nrOfPlayers<2) { loadFile.close(); cout << "Only one player in replay" << endl; return false; } cout << "loading player 2" << endl; loadFile.read(reinterpret_cast(&nrOfFrames),sizeof(Uint32)); for (int i=0; (i(&bp),sizeof(bp)); //bps.push_back(bp); We have already read player 1 with another function } loadFile.read(reinterpret_cast(&finalPack),sizeof(finalPack)); loadFile.read(reinterpret_cast(&theResult),sizeof(theResult)); loadFile.read(reinterpret_cast(&name),sizeof(name)); loadFile.read(reinterpret_cast(&nrOfFrames),sizeof(Uint32)); bps.reserve(nrOfFrames); for (int i=0; (i(&bp),sizeof(bp)); bps.push_back(bp); } loadFile.read(reinterpret_cast(&finalPack),sizeof(finalPack)); loadFile.read(reinterpret_cast(&theResult),sizeof(theResult)); loadFile.read(reinterpret_cast(&name),sizeof(name)); break; default: cout << "Unknown version: " << version << endl; return false; }; loadFile.close(); } else { cout << "File not found or couldn't open: " << filename << endl; return false; } }