1
/***************************************************************************
2
* KBlocks, a falling blocks game for KDE *
3
* Copyright (C) 2010 Zhongjie Cai <squall.leonhart.cai@gmail.com> *
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"
14
KBlocksGameRecorder::KBlocksGameRecorder()
19
KBlocksGameRecorder::~KBlocksGameRecorder()
24
void KBlocksGameRecorder::append(int index, int type, int value)
26
_game_record_data tmpLastData;
27
tmpLastData.index = index;
28
tmpLastData.type = type;
29
tmpLastData.value = value;
30
tmpLastData.time = getMillisecOfNow();
31
mGameRecord.push_back(tmpLastData);
34
void KBlocksGameRecorder::save(const char * fileName, bool isBinaryMode)
36
FILE * pFile = fopen(fileName, "w");
48
void KBlocksGameRecorder::saveText(FILE * pFile)
51
timeLong oldTime = mGameRecord.front().time;
52
list<_game_record_data>::iterator it;
53
for(it = mGameRecord.begin(); it != mGameRecord.end(); it++)
55
tmpTime = (int)(it->time - oldTime);
57
fprintf(pFile, "%d %s %d %d\n", tmpTime, KBlocksRecordText[it->type], it->index, it->value);
61
void KBlocksGameRecorder::saveBinary(FILE * pFile)
64
timeLong oldTime = mGameRecord.front().time;
65
list<_game_record_data>::iterator it;
66
for(it = mGameRecord.begin(); it != mGameRecord.end(); it++)
68
tmpTime = (int)(it->time - oldTime);
74
writeByte(pFile, 255);
75
writeByte(pFile, RecordDataType_Skipped);
76
writeByte(pFile, it->index);
77
writeByte(pFile, it->value);
81
writeByte(pFile, tmpTime);
82
writeByte(pFile, it->type);
83
writeByte(pFile, it->index);
84
writeByte(pFile, it->value);
88
void KBlocksGameRecorder::writeByte(FILE * pFile, int value)
90
int tmpByte = (value & 0xFF);
91
fputc(tmpByte, pFile);
94
timeLong KBlocksGameRecorder::getMillisecOfNow()
98
gettimeofday(&tmpCurTime, NULL);
100
timeLong tmpMilliTime = (timeLong)tmpCurTime.tv_usec / 1000;
101
tmpMilliTime += (timeLong)tmpCurTime.tv_sec * 1000;