/
main.cpp
260 lines (218 loc) · 7.54 KB
/
main.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include <windows.h>
#include <time.h>
#include <assert.h>
#include <stdio.h>
static const int DEFAULT_WINDOW_WIDTH = 500;
static const int DEFAULT_WINDOW_HEIGHT = 500;
static const int CELL_WIDTH_PX = 5;
static const int CELL_HEIGHT_PX = 5;
static const int CELL_COLOR_ALIVE = 0xFFFF0000;
static const int CELL_COLOR_DEAD = 0xFF000000;
static const float TICK_DURATION = 0.1f;
static int globalWindowWidth;
static int globalWindowHeight;
struct GameState {
int cellCols, cellRows;
bool *cells1, *cells2;
bool *cells, *newCells;
float tickTimer;
BITMAPINFO *bitmapInfo;
int *bitmapBuffer;
int bitmapWidth, bitmapHeight;
};
inline int getLiveNeighbourCount(GameState *state, const int row, const int col) {
int count = 0;
const int NEIGHBOUR_COUNT = 8;
const int neighbours[NEIGHBOUR_COUNT][2] = {
{ -1, -1 },{ -1, 0 },{ -1, 1 },
{ 0, -1 }, { 0, 1 },
{ 1, -1 },{ 1, 0 },{ 1, 1 },
};
for (int i = 0; i < NEIGHBOUR_COUNT; ++i) {
int r = (row + neighbours[i][0]) % state->cellRows;
if (r < 0) r += state->cellRows;
int c = (col + neighbours[i][1]) % state->cellCols;
if (c < 0) c += state->cellCols;
bool *cell = state->cells + r*state->cellCols + c;
if (*cell) ++count;
}
return count;
}
static void initGame(GameState *state, const int windowWidth, const int windowHeight) {
state->bitmapWidth = (windowWidth / CELL_WIDTH_PX) * CELL_WIDTH_PX;
state->bitmapHeight = (windowHeight / CELL_HEIGHT_PX) * CELL_HEIGHT_PX;
state->cellCols = state->bitmapWidth / CELL_WIDTH_PX;
state->cellRows = state->bitmapHeight / CELL_HEIGHT_PX;
if (state->cells1) free(state->cells1);
state->cells1 = (bool *)malloc(state->cellRows * state->cellCols * sizeof(bool));
if (state->cells2) free(state->cells2);
state->cells2 = (bool *)malloc(state->cellRows * state->cellCols * sizeof(bool));
state->cells = state->cells1;
state->newCells = state->cells2;
// seed initial cells
for (int row = 0; row < state->cellRows; ++row)
for (int col = 0; col < state->cellCols; ++col)
*(state->cells + row*state->cellCols + col) = (rand() % 2) != 0;
state->tickTimer = 0.0f;
if (state->bitmapInfo) free(state->bitmapInfo);
state->bitmapInfo = (BITMAPINFO *)malloc(sizeof(BITMAPINFO));
state->bitmapInfo->bmiHeader.biSize = sizeof(state->bitmapInfo->bmiHeader);
state->bitmapInfo->bmiHeader.biWidth = state->bitmapWidth;
state->bitmapInfo->bmiHeader.biHeight = -state->bitmapHeight;
state->bitmapInfo->bmiHeader.biPlanes = 1;
state->bitmapInfo->bmiHeader.biBitCount = 32;
state->bitmapInfo->bmiHeader.biCompression = BI_RGB;
if (state->bitmapBuffer) free(state->bitmapBuffer);
state->bitmapBuffer = (int*)malloc(state->bitmapWidth * state->bitmapHeight * sizeof(int));
}
static LRESULT CALLBACK wndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_SIZE:
globalWindowWidth = LOWORD(lParam);
globalWindowHeight = HIWORD(lParam);
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASS wc = {};
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = wndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpszClassName = L"GameOfLifeWindowClass";
if (!RegisterClass(&wc)) {
MessageBox(NULL, L"RegisterClass failed!", NULL, NULL);
return 1;
}
int windowWidth = globalWindowWidth = DEFAULT_WINDOW_WIDTH;
int windowHeight = globalWindowHeight = DEFAULT_WINDOW_HEIGHT;
RECT clientRect = { 0, 0, windowWidth, windowHeight };
DWORD windowStyle = WS_OVERLAPPEDWINDOW | WS_VISIBLE;
AdjustWindowRect(&clientRect, windowStyle, NULL);
HWND hWnd = CreateWindowEx(NULL, wc.lpszClassName, L"Game of Life", windowStyle, 300, 0,
clientRect.right - clientRect.left, clientRect.bottom - clientRect.top, NULL, NULL, hInstance, NULL);
if (!hWnd) {
MessageBox(NULL, L"CreateWindowEx failed!", NULL, NULL);
return 1;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
LARGE_INTEGER perfCounterFrequency = { 0 };
QueryPerformanceFrequency(&perfCounterFrequency);
LARGE_INTEGER perfCounter = { 0 };
QueryPerformanceCounter(&perfCounter);
LARGE_INTEGER prevPerfCounter = { 0 };
float dt = 0.0f;
float realDt = dt;
const float targetFps = 60.0f;
const float targetDt = 1.0f / targetFps;
srand((unsigned int)time(NULL));
GameState *gameState = (GameState *)malloc(sizeof(GameState));
memset(gameState, 0, sizeof(GameState));
initGame(gameState, windowWidth, windowHeight);
bool gameIsRunning = true;
while (gameIsRunning) {
prevPerfCounter = perfCounter;
QueryPerformanceCounter(&perfCounter);
dt = (float)(perfCounter.QuadPart - prevPerfCounter.QuadPart) / (float)perfCounterFrequency.QuadPart;
realDt = dt;
if (dt > targetDt)
dt = targetDt;
MSG msg;
while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
switch (msg.message) {
case WM_QUIT:
gameIsRunning = false;
break;
case WM_KEYDOWN:
case WM_KEYUP:
switch (msg.wParam) {
case VK_ESCAPE:
gameIsRunning = false;
break;
}
break;
default:
TranslateMessage(&msg);
DispatchMessage(&msg);
break;
}
}
//
// UPDATE
//
// When the window is resized, reinitialize the game with the new window size.
if (globalWindowWidth != windowWidth || globalWindowHeight != windowHeight) {
windowWidth = globalWindowWidth;
windowHeight = globalWindowHeight;
initGame(gameState, globalWindowWidth, globalWindowHeight);
}
// Handle the left mouse button: make the clicked cell alive.
if (GetKeyState(VK_LBUTTON) & (1 << 15)) {
POINT mousePos;
GetCursorPos(&mousePos);
ScreenToClient(hWnd, &mousePos);
int row = mousePos.y / CELL_HEIGHT_PX;
int col = mousePos.x / CELL_WIDTH_PX;
if (row < gameState->cellRows && col < gameState->cellCols) {
bool *cell = gameState->cells + row*gameState->cellCols + col;
*cell = true;
}
}
gameState->tickTimer += dt;
if (gameState->tickTimer > TICK_DURATION) {
gameState->tickTimer = 0;
for (int row = 0; row < gameState->cellRows; ++row) {
for (int col = 0; col < gameState->cellCols; ++col) {
bool *cell = gameState->cells + row*gameState->cellCols + col;
bool *newCell = gameState->newCells + row*gameState->cellCols + col;
*newCell = *cell;
int liveNeighbours = getLiveNeighbourCount(gameState, row, col);
if (*cell) {
if (liveNeighbours < 2 || liveNeighbours > 3)
*newCell = false;
}
else {
if (liveNeighbours == 3)
*newCell = true;
}
}
}
bool *tmp = gameState->cells;
gameState->cells = gameState->newCells;
gameState->newCells = tmp;
}
//
// RENDER
//
for (int row = 0; row < gameState->cellRows; ++row) {
for (int col = 0; col < gameState->cellCols; ++col) {
bool *cell = gameState->cells + row*gameState->cellCols + col;
int color = *cell ? CELL_COLOR_ALIVE : CELL_COLOR_DEAD;
for (int r = 0; r < CELL_HEIGHT_PX; ++r) {
for (int c = 0; c < CELL_WIDTH_PX; ++c) {
int *pixel = gameState->bitmapBuffer + (row*CELL_HEIGHT_PX + r)*gameState->cellCols*CELL_WIDTH_PX + col*CELL_WIDTH_PX + c;
*pixel = color;
}
}
}
}
HDC hDC = GetDC(hWnd);
StretchDIBits(hDC, 0, 0, gameState->bitmapWidth, gameState->bitmapHeight,
0, 0, gameState->bitmapWidth, gameState->bitmapHeight,
gameState->bitmapBuffer, gameState->bitmapInfo, DIB_RGB_COLORS, SRCCOPY);
ReleaseDC(hWnd, hDC);
#if 0
char textBuffer[256];
sprintf(textBuffer, "dt: %f, fps: %f\n", realDt, 1.0f/realDt);
OutputDebugStringA(textBuffer);
#endif
}
return 0;
}