4 ReplayData::ReplayData()
12 header.randomSeed = time(NULL);
13 header.version = VERSION;
14 header.release = RELEASE;
19 ReplayData::~ReplayData()
21 if (replayMode == REPLAY_MODE::RECORD)
25 int size = fwrite(&header, sizeof(ReplayDataHeader), 1, fp);
28 printf("Error saving replay data: %s\n", strerror(errno));
33 if (replayMode != REPLAY_MODE::NONE)
39 void ReplayData::printReplayInformation()
41 tm *timeinfo = localtime(&header.randomSeed);
42 printf("Recorded on : %s", asctime(timeinfo));
43 printf("Map : %s\n", header.map);
44 printf("Score : %d\n", header.score);
45 printf("Time : %s\n", Math::formatTime(header.time));
48 void ReplayData::swapHeaderEndians()
51 header.randomSeed = SDL_SwapLE32(header.randomSeed);
52 header.version = SDL_SwapLE32(header.version);
53 header.release = SDL_SwapLE32(header.release);
54 header.skill = SDL_SwapLE32(header.skill);
55 header.score = SDL_SwapLE32(header.score);
59 void ReplayData::setMode(REPLAY_MODE::TYPE replayMode)
61 this->replayMode = replayMode;
63 if (replayMode == REPLAY_MODE::PLAYBACK)
65 fp = fopen(filename, "rb");
69 printf("ERROR: Replay file '%s' could not be loaded.\n", filename);
70 replayMode = REPLAY_MODE::NONE;
74 if (fread(&header, sizeof(ReplayDataHeader), 1, fp) != 1)
76 printf("ERROR: Replay file '%s' is corrupt\n", filename);
77 replayMode = REPLAY_MODE::NONE;
84 printf("\n==== REPLAY HEADER DATA ====\n");
85 printReplayInformation();
86 if ((header.version != VERSION) && (header.release != RELEASE))
88 printf("\nWARNING: Replay is from a different version (%f %d) and may not play back correctly\n", header.version, header.release);
90 printf("Press F5 to toggle Fast Playback\n");
93 else if (replayMode == REPLAY_MODE::RECORD)
95 fp = fopen(filename, "wba");
99 printf("ERROR: Replay file '%s' could not be opened for writing.\n", filename);
100 replayMode = REPLAY_MODE::NONE;
106 int size = fwrite(&header, sizeof(ReplayDataHeader), 1, fp);
109 printf("Error writing replay data header: %s\n", strerror(errno));
110 replayMode = REPLAY_MODE::NONE;
120 void ReplayData::reset()
124 for (int i = 0 ; i < DATA_LENGTH ; i++)
126 for (int j = 0 ; j < CONTROL::MAX ; j++)
132 for (int i = 0 ; i < CONTROL::MAX ; i++)
138 void ReplayData::read(int *c)
140 if (replayMode != REPLAY_MODE::PLAYBACK)
147 for (int i = 0 ; i < CONTROL::MAX ; i++)
155 for (int i = 0 ; i < CONTROL::MAX ; i++)
157 c[i] = data[index][i];
161 c[CONTROL::PAUSE] = 0;
164 void ReplayData::set(int *c)
166 if (replayMode != REPLAY_MODE::RECORD)
171 for (int i = 0 ; i < CONTROL::MAX ; i++)
173 data[index][i] = c[i];
177 void ReplayData::commit()
179 if ((endOfReplay) || (replayMode == REPLAY_MODE::NONE))
186 if (index < DATA_LENGTH)
188 if (replayMode == REPLAY_MODE::PLAYBACK)
190 if (data[index][0] == -1)
192 printf("===== END OF REPLAY =====\n");
200 for (int i = 0 ; i < CONTROL::MAX ; i++)
207 if (index >= DATA_LENGTH)
209 if (replayMode == REPLAY_MODE::PLAYBACK)
213 else if (replayMode == REPLAY_MODE::RECORD)
222 void ReplayData::load()
229 debug(("ReplayData::load()\n"));
231 int size = fread(data, 1, DATA_LENGTH * CONTROL::MAX, fp);
233 if (size != DATA_LENGTH * CONTROL::MAX)
235 printf("Error reading replay data\n");
239 debug(("ReplayData::load() - Done (%d)\n", size));
242 void ReplayData::save()
244 debug(("ReplayData::save()\n"));
246 int size = fwrite(data, 1, DATA_LENGTH * CONTROL::MAX, fp);
248 if (size != DATA_LENGTH * CONTROL::MAX)
250 printf("Error saving replay data\n");
254 debug(("ReplayData::save() - Done (%d)\n", size));