-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.cpp
More file actions
67 lines (57 loc) · 1.38 KB
/
Logger.cpp
File metadata and controls
67 lines (57 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "stdafx.h"
#include "Logger.h"
CLogger::CLogger()
{
}
CLogger::~CLogger()
{
}
char* CLogger::pszFileName = "gamedata.dat";
void CLogger::OpenFile()
{
m_pFile = fopen(pszFileName, "wb");
if (m_pFile == nullptr)
{
perror("打开文件失败: ");
}
fseek(m_pFile, 0x20, SEEK_SET);//移动到0x20
}
void CLogger::SaveRunTime(clock_t time)
{
fseek(m_pFile, 0, SEEK_SET);
fwrite(&time, sizeof(time), 1, m_pFile);
fseek(m_pFile, 0x20, SEEK_SET);
}
void CLogger::SaveData(CFood* food, CSnake* snake, clock_t time)
{
if (m_pFile==nullptr)
{
return;
}
//食物
fwrite(&food->m_nRow, 1, sizeof(food->m_nRow), m_pFile);
fwrite(&food->m_nCol, 1, sizeof(food->m_nCol), m_pFile);
fwrite(&food->m_nShapeID, 1, sizeof(food->m_nShapeID), m_pFile);
fwrite(&food->m_nFoodColorID, 1, sizeof(food->m_nShapeID), m_pFile);
//蛇:长度、方向、结点
int nCount = snake->m_Body.size();
fwrite(&nCount, 1, sizeof(nCount), m_pFile);
fwrite(&snake->m_eDir, 1, sizeof(snake->m_eDir), m_pFile);
for (int i = 0; i < nCount; i++)
{
tagSnakeNode* pCurNode = snake->m_Body[i];
fwrite(&pCurNode->m_nRow, 1,
sizeof(pCurNode->m_nRow), m_pFile);
fwrite(&pCurNode->m_nCol, 1,
sizeof(pCurNode->m_nCol), m_pFile);
}
//时间
fwrite(&time, 1, sizeof(time), m_pFile);
}
void CLogger::CloseFile()
{
if (m_pFile != nullptr)
{
fclose(m_pFile);
}
}