1 /***************************************************************************
2 * KBlocks, a falling blocks game by KDE *
3 * Copyright (C) 2010 Zhongjie Cai <squall.leonhart.cai@gmail.com> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 ***************************************************************************/
10 #include "KBlocksGameRecorder.h"
11 #include "utils.h"
12
KBlocksGameRecorder()13 KBlocksGameRecorder::KBlocksGameRecorder()
14 {
15 mGameRecord.clear();
16 }
17
~KBlocksGameRecorder()18 KBlocksGameRecorder::~KBlocksGameRecorder()
19 {
20 mGameRecord.clear();
21 }
22
append(int index,int type,int value)23 void KBlocksGameRecorder::append(int index, int type, int value)
24 {
25 _game_record_data tmpLastData;
26 tmpLastData.index = index;
27 tmpLastData.type = type;
28 tmpLastData.value = value;
29 tmpLastData.time = Utils::getMillisecOfNow();
30 mGameRecord.push_back(tmpLastData);
31 }
32
save(const char * fileName,bool isBinaryMode)33 void KBlocksGameRecorder::save(const char *fileName, bool isBinaryMode)
34 {
35 FILE *pFile = fopen(fileName, "w");
36 if (isBinaryMode) {
37 saveBinary(pFile);
38 } else {
39 saveText(pFile);
40 }
41 fclose(pFile);
42 }
43
saveText(FILE * pFile)44 void KBlocksGameRecorder::saveText(FILE *pFile)
45 {
46 int tmpTime = 0;
47 timeLong oldTime = mGameRecord.front().time;
48 list<_game_record_data>::iterator it;
49 for (it = mGameRecord.begin(); it != mGameRecord.end(); ++it) {
50 tmpTime = (int)(it->time - oldTime);
51 oldTime = it->time;
52 fprintf(pFile, "%d %s %d %d\n", tmpTime, KBlocksRecordText[it->type], it->index, it->value);
53 }
54 }
55
saveBinary(FILE * pFile)56 void KBlocksGameRecorder::saveBinary(FILE *pFile)
57 {
58 int tmpTime = 0;
59 timeLong oldTime = mGameRecord.front().time;
60 list<_game_record_data>::iterator it;
61 for (it = mGameRecord.begin(); it != mGameRecord.end(); ++it) {
62 tmpTime = (int)(it->time - oldTime);
63 oldTime = it->time;
64 if (tmpTime > 255) {
65 while (tmpTime > 255) {
66 writeByte(pFile, 255);
67 writeByte(pFile, RecordDataType_Skipped);
68 writeByte(pFile, it->index);
69 writeByte(pFile, it->value);
70 tmpTime -= 255;
71 }
72 }
73 writeByte(pFile, tmpTime);
74 writeByte(pFile, it->type);
75 writeByte(pFile, it->index);
76 writeByte(pFile, it->value);
77 }
78 }
79
writeByte(FILE * pFile,int value)80 void KBlocksGameRecorder::writeByte(FILE *pFile, int value)
81 {
82 int tmpByte = (value & 0xFF);
83 fputc(tmpByte, pFile);
84 }
85
86