1 | /*************************************************************************** |
2 | * KBlocks, a falling blocks game for 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 | |
12 | #include <sys/time.h> |
13 | |
14 | KBlocksGameRecorder::KBlocksGameRecorder() |
15 | { |
16 | mGameRecord.clear(); |
17 | } |
18 | |
19 | KBlocksGameRecorder::~KBlocksGameRecorder() |
20 | { |
21 | mGameRecord.clear(); |
22 | } |
23 | |
24 | void KBlocksGameRecorder::append(int index, int type, int value) |
25 | { |
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); |
32 | } |
33 | |
34 | void KBlocksGameRecorder::save(const char * fileName, bool isBinaryMode) |
35 | { |
36 | FILE * pFile = fopen(fileName, "w" ); |
37 | if (isBinaryMode) |
38 | { |
39 | saveBinary(pFile); |
40 | } |
41 | else |
42 | { |
43 | saveText(pFile); |
44 | } |
45 | fclose(pFile); |
46 | } |
47 | |
48 | void KBlocksGameRecorder::saveText(FILE * pFile) |
49 | { |
50 | int tmpTime = 0; |
51 | timeLong oldTime = mGameRecord.front().time; |
52 | list<_game_record_data>::iterator it; |
53 | for(it = mGameRecord.begin(); it != mGameRecord.end(); it++) |
54 | { |
55 | tmpTime = (int)(it->time - oldTime); |
56 | oldTime = it->time; |
57 | fprintf(pFile, "%d %s %d %d\n" , tmpTime, KBlocksRecordText[it->type], it->index, it->value); |
58 | } |
59 | } |
60 | |
61 | void KBlocksGameRecorder::saveBinary(FILE * pFile) |
62 | { |
63 | int tmpTime = 0; |
64 | timeLong oldTime = mGameRecord.front().time; |
65 | list<_game_record_data>::iterator it; |
66 | for(it = mGameRecord.begin(); it != mGameRecord.end(); it++) |
67 | { |
68 | tmpTime = (int)(it->time - oldTime); |
69 | oldTime = it->time; |
70 | if (tmpTime > 255) |
71 | { |
72 | while(tmpTime > 255) |
73 | { |
74 | writeByte(pFile, 255); |
75 | writeByte(pFile, RecordDataType_Skipped); |
76 | writeByte(pFile, it->index); |
77 | writeByte(pFile, it->value); |
78 | tmpTime -= 255; |
79 | } |
80 | } |
81 | writeByte(pFile, tmpTime); |
82 | writeByte(pFile, it->type); |
83 | writeByte(pFile, it->index); |
84 | writeByte(pFile, it->value); |
85 | } |
86 | } |
87 | |
88 | void KBlocksGameRecorder::writeByte(FILE * pFile, int value) |
89 | { |
90 | int tmpByte = (value & 0xFF); |
91 | fputc(tmpByte, pFile); |
92 | } |
93 | |
94 | timeLong KBlocksGameRecorder::getMillisecOfNow() |
95 | { |
96 | timeval tmpCurTime; |
97 | |
98 | gettimeofday(&tmpCurTime, NULL); |
99 | |
100 | timeLong tmpMilliTime = (timeLong)tmpCurTime.tv_usec / 1000; |
101 | tmpMilliTime += (timeLong)tmpCurTime.tv_sec * 1000; |
102 | |
103 | return tmpMilliTime; |
104 | } |
105 | |