Changeset View
Changeset View
Standalone View
Standalone View
KBlocksGameRecorder.cpp
| 1 | /*************************************************************************** | 1 | /*************************************************************************** | ||
|---|---|---|---|---|---|
| 2 | * KBlocks, a falling blocks game by KDE * | 2 | * KBlocks, a falling blocks game by KDE * | ||
| 3 | * Copyright (C) 2010 Zhongjie Cai <squall.leonhart.cai@gmail.com> * | 3 | * Copyright (C) 2010 Zhongjie Cai <squall.leonhart.cai@gmail.com> * | ||
| 4 | * * | 4 | * * | ||
| 5 | * This program is free software; you can redistribute it and/or modify * | 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 * | 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 * | 7 | * the Free Software Foundation; either version 2 of the License, or * | ||
| 8 | * (at your option) any later version. * | 8 | * (at your option) any later version. * | ||
| 9 | ***************************************************************************/ | 9 | ***************************************************************************/ | ||
| 10 | #include "KBlocksGameRecorder.h" | 10 | #include "KBlocksGameRecorder.h" | ||
| 11 | 11 | #include "utils.h" | |||
| 12 | #include <sys/time.h> | | |||
| 13 | 12 | | |||
| 14 | KBlocksGameRecorder::KBlocksGameRecorder() | 13 | KBlocksGameRecorder::KBlocksGameRecorder() | ||
| 15 | { | 14 | { | ||
| 16 | mGameRecord.clear(); | 15 | mGameRecord.clear(); | ||
| 17 | } | 16 | } | ||
| 18 | 17 | | |||
| 19 | KBlocksGameRecorder::~KBlocksGameRecorder() | 18 | KBlocksGameRecorder::~KBlocksGameRecorder() | ||
| 20 | { | 19 | { | ||
| 21 | mGameRecord.clear(); | 20 | mGameRecord.clear(); | ||
| 22 | } | 21 | } | ||
| 23 | 22 | | |||
| 24 | void KBlocksGameRecorder::append(int index, int type, int value) | 23 | void KBlocksGameRecorder::append(int index, int type, int value) | ||
| 25 | { | 24 | { | ||
| 26 | _game_record_data tmpLastData; | 25 | _game_record_data tmpLastData; | ||
| 27 | tmpLastData.index = index; | 26 | tmpLastData.index = index; | ||
| 28 | tmpLastData.type = type; | 27 | tmpLastData.type = type; | ||
| 29 | tmpLastData.value = value; | 28 | tmpLastData.value = value; | ||
| 30 | tmpLastData.time = getMillisecOfNow(); | 29 | tmpLastData.time = Utils::getMillisecOfNow(); | ||
| 31 | mGameRecord.push_back(tmpLastData); | 30 | mGameRecord.push_back(tmpLastData); | ||
| 32 | } | 31 | } | ||
| 33 | 32 | | |||
| 34 | void KBlocksGameRecorder::save(const char *fileName, bool isBinaryMode) | 33 | void KBlocksGameRecorder::save(const char *fileName, bool isBinaryMode) | ||
| 35 | { | 34 | { | ||
| 36 | FILE *pFile = fopen(fileName, "w"); | 35 | FILE *pFile = fopen(fileName, "w"); | ||
| 37 | if (isBinaryMode) { | 36 | if (isBinaryMode) { | ||
| 38 | saveBinary(pFile); | 37 | saveBinary(pFile); | ||
| Show All 40 Lines | |||||
| 79 | } | 78 | } | ||
| 80 | 79 | | |||
| 81 | void KBlocksGameRecorder::writeByte(FILE *pFile, int value) | 80 | void KBlocksGameRecorder::writeByte(FILE *pFile, int value) | ||
| 82 | { | 81 | { | ||
| 83 | int tmpByte = (value & 0xFF); | 82 | int tmpByte = (value & 0xFF); | ||
| 84 | fputc(tmpByte, pFile); | 83 | fputc(tmpByte, pFile); | ||
| 85 | } | 84 | } | ||
| 86 | 85 | | |||
| 87 | timeLong KBlocksGameRecorder::getMillisecOfNow() | | |||
| 88 | { | | |||
| 89 | timeval tmpCurTime; | | |||
| 90 | | ||||
| 91 | gettimeofday(&tmpCurTime, NULL); | | |||
| 92 | | ||||
| 93 | timeLong tmpMilliTime = (timeLong)tmpCurTime.tv_usec / 1000; | | |||
| 94 | tmpMilliTime += (timeLong)tmpCurTime.tv_sec * 1000; | | |||
| 95 | | ||||
| 96 | return tmpMilliTime; | | |||
| 97 | } | | |||