-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
480 lines (418 loc) · 14.5 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#include <iostream>
#include <random>
#include <string>
#include <allegro5\allegro.h>
#include <allegro5\allegro_image.h>
#include <allegro5\allegro_font.h>
#include <allegro5\allegro_ttf.h>
void nextGeneration(bool** pCells, bool** pNext, short int iTotalX, short int iTotalY, long int& iTotalAlive) {
short int rowPrev = 0;
short int rowNext = 0;
short int colPrev = 0;
short int colNext = 0;
short int totalNeighbors = 0;
for (short int i = 0; i < iTotalX; i++) {
for (short int j = 0; j < iTotalY; j++) {
pNext[i][j] = false;
if (i == 0) {
rowPrev = iTotalX - 1;
rowNext = 1;
}
else {
if (i == (iTotalX - 1)) {
rowNext = 0;
rowPrev = i - 1;
}
else {
rowPrev = i - 1;
rowNext = i + 1;
}
}
if (j == 0) {
colPrev = iTotalY - 1;
colNext = 1;
}
else {
if (j == (iTotalY - 1)) {
colPrev = j - 1;
colNext = 0;
}
else {
colPrev = j - 1;
colNext = j + 1;
}
}
totalNeighbors += (!pCells[rowPrev][colPrev]) ? 0 : 1;
totalNeighbors += (!pCells[rowPrev][j]) ? 0 : 1;
totalNeighbors += (!pCells[rowPrev][colNext]) ? 0 : 1;
totalNeighbors += (!pCells[i][colPrev]) ? 0 : 1;
totalNeighbors += (!pCells[i][colNext]) ? 0 : 1;
totalNeighbors += (!pCells[rowNext][colPrev]) ? 0 : 1;
totalNeighbors += (!pCells[rowNext][j]) ? 0 : 1;
totalNeighbors += (!pCells[rowNext][colNext]) ? 0 : 1;
if (pCells[i][j] == true && (totalNeighbors == 2 || totalNeighbors == 3)) {
pNext[i][j] = true;
++iTotalAlive;
}
else if (pCells[i][j] == false && totalNeighbors == 3) {
pNext[i][j] = true;
++iTotalAlive;
}
totalNeighbors = 0;
}
}
}
void redrawCells(ALLEGRO_BITMAP* pBuffer, bool** pCells, bool** pNextCells, short int iWidth, short int iHeight, ALLEGRO_COLOR oCellColor, ALLEGRO_COLOR oBackColor) {
al_lock_bitmap(pBuffer, al_get_bitmap_format(pBuffer), ALLEGRO_LOCK_WRITEONLY);
al_set_target_bitmap(pBuffer);
for (short int i = 0; i < iWidth; i++) {
for (short int j = 0; j < iHeight; j++) {
if (pCells[i][j] == true) {
al_put_pixel(i, j, oCellColor);
}
else {
al_put_pixel(i, j, oBackColor);
}
}
}
al_unlock_bitmap(pBuffer);
}
void initCells(bool** pCells, bool** pNextCells, short int iAppWidth, short int iAppHeight, int fLifeScarcity) {
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_real_distribution<double> dist(1.0f, 1000.0f);
for (short int i = 0; i < iAppWidth; i++) {
pCells[i] = new bool[iAppHeight];
pNextCells[i] = new bool[iAppHeight];
for (short int j = 0; j < iAppHeight; j++) {
pNextCells[i][j] = false;
int fRandom = std::round(dist(mt));
if (fRandom % fLifeScarcity == 0) {
pCells[i][j] = true;
}
else {
pCells[i][j] = false;
}
}
}
}
void clearCells(bool** pCells, bool** pNextCells, short int iAppWidth, short int iAppHeight) {
for (short int i = 0; i < iAppWidth; i++) {
for (short int j = 0; j < iAppHeight; j++) {
pCells[i][j] = false;
pNextCells[i][j] = false;
}
}
}
void randomCells(bool** pCells, short int iAppWidth, short int iAppHeight, int fLifeScarcity) {
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_real_distribution<double> dist(1.0f, 1000.0f);
for (short int i = 0; i < iAppWidth; i++) {
for (short int j = 0; j < iAppHeight; j++) {
int fRandom = std::round(dist(mt));
if (fRandom % fLifeScarcity == 0) {
pCells[i][j] = true;
}
else {
pCells[i][j] = false;
}
}
}
}
void copyCells(bool** pCells, bool** pNextCells, short int iAppWidth, short int iAppHeight) {
for (short int i = 0; i < iAppWidth; i++) {
memcpy(pCells[i], pNextCells[i], (iAppHeight * sizeof(bool)));
}
}
unsigned int randr(unsigned int min, unsigned int max) {
double scaled = (double)rand() / RAND_MAX;
return (max - min + 1)*scaled + min;
}
bool isPatternStable(long int* iAliveHistory, short int iSize) {
int iStable = 0;
for (short int i = 0; i < iSize; i++) {
for (short int j = 0; j < iSize; j++) {
if (iAliveHistory[i] == iAliveHistory[j]) {
++iStable;
}
}
}
if (iStable >= (iSize * 10)) {
return true;
}
else {
return false;
}
}
bool areIdenticalCellMap(bool** pCells, bool** pNextCells, short int iAppWidth, short int iAppHeight) {
bool bReturn = true;
for (short int i = 0; i < iAppWidth; i++) {
for (short int j = 0; j < iAppHeight; j++) {
if (pCells[i][j] != pNextCells[i][j]) {
return false;
}
}
}
return bReturn;
}
// COPIED FROM http://stackoverflow.com/questions/4643512/replace-substring-with-another-substring-c
std::string ReplaceString(std::string subject, const std::string& search,
const std::string& replace) {
size_t pos = 0;
while ((pos = subject.find(search, pos)) != std::string::npos) {
subject.replace(pos, search.length(), replace);
pos += replace.length();
}
return subject;
}
void ReplaceStringInPlace(std::string& subject, const std::string& search,
const std::string& replace) {
size_t pos = 0;
while ((pos = subject.find(search, pos)) != std::string::npos) {
subject.replace(pos, search.length(), replace);
pos += replace.length();
}
}
int main(short int argc, char** argv) {
al_init();
al_install_keyboard();
al_install_mouse();
al_init_image_addon();
al_init_font_addon();
al_init_ttf_addon();
ALLEGRO_MONITOR_INFO oMonitorInfo;
al_get_monitor_info(0, &oMonitorInfo);
short int iDisplayWidth = oMonitorInfo.x2 * 0.70f;
short int iDisplayHeight = oMonitorInfo.y2 * 0.70f;
short int iAppWidth = iDisplayWidth * 0.95f;
short int iAppHeight = iDisplayHeight * 0.95f;
short int iMarginHorizontal = (iDisplayWidth - iAppWidth) / 2;
short int iMarginVertical = ((iDisplayHeight - iAppHeight) / 2);
int iGenerations = 0;
short int iFPS = 30;
float iLifeMin = 3.0f;
float iLifeMax = 37.0f;
short int iFontSize = (iDisplayWidth > 1024) ? 12 : 10;
if (iDisplayWidth < 800) {
iFontSize = 8;
}
long int iSimulations = 1;
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_real_distribution<double> dist(iLifeMin, iLifeMax);
int iLifeScarcity = std::round(dist(mt));
bool** pCells = new bool*[iAppWidth];
bool** pNextGenCells = new bool*[iAppWidth];
initCells(pCells, pNextGenCells, iAppWidth, iAppHeight, iLifeScarcity);
ALLEGRO_DISPLAY* pDisplay = al_create_display(iDisplayWidth, iDisplayHeight);
ALLEGRO_EVENT_QUEUE* pQueue = al_create_event_queue();
ALLEGRO_TIMER* pTimer = al_create_timer(1.0f / iFPS);
ALLEGRO_TIMER* pSecondBySecondTimer = al_create_timer(1.0f);
ALLEGRO_BITMAP* pBuffer = al_create_bitmap(iAppWidth, iAppHeight);
ALLEGRO_COLOR oBackgroundColor = al_map_rgb(0, 0, 0);
ALLEGRO_COLOR oCellColor = al_map_rgb(randr(150, 255), randr(150, 255), randr(150, 255));
ALLEGRO_FONT* oFont = al_load_ttf_font("VeraMono.ttf", iFontSize, 0);
ALLEGRO_FONT* oFontLarge = al_load_ttf_font("VeraMono.ttf", (iFontSize * 3), 0);
al_inhibit_screensaver(true);
al_register_event_source(pQueue, al_get_keyboard_event_source());
al_register_event_source(pQueue, al_get_mouse_event_source());
al_register_event_source(pQueue, al_get_timer_event_source(pTimer));
al_register_event_source(pQueue, al_get_timer_event_source(pSecondBySecondTimer));
al_set_target_backbuffer(pDisplay);
al_clear_to_color(oBackgroundColor);
al_flip_display();
al_start_timer(pTimer);
al_start_timer(pSecondBySecondTimer);
ALLEGRO_EVENT oEvent;
short int iBufferUsed = 0;
short int iBufferDrawn = 0;
bool bRedraw = false;
std::string sHeaderStatistics = "GEN [GENXXXXX] FPS [FPSXXXXX] CELLS [CELLSXXXXX] GENS/S [GENSSXXXXX] SCARCTY [SCARXXXXX] TIME [TIMEXXXXX]";
std::string sHeaderStats = "";
/* std::string sHeaderText_2 = "";
std::string sHeaderText_3 = "";
std::string sHeaderText_4 = "";
std::string sHeaderText_5 = "";
std::string sHeaderText_6 = ""; */
std::string sCountdownText = "";
std::string sSimulations = "";
std::string sStats = "CELLS: ";
sStats.append(std::to_string((iAppWidth * iAppHeight)));
sStats.append(", MAP SIZE (KB): ");
sStats.append(std::to_string((iAppWidth * iAppHeight * sizeof(bool)) / 1024));
sStats.append(" (SPACE) Pause (C)olor, (R)eload, (S)carcity, (F) +1 FPS, (G) -1 FPS, (ESC) Exit");
long int iTotalAlive = 0;
int iPatternStableBuffer = (iFPS * 4);
long int* iTotalPatternStable = new long int[iPatternStableBuffer];
short int iTotalPatternCounter = 0;
long int iSecondsRunning = 0;
float fPosText2 = (iAppWidth * 0.15);
float fPosText3 = (iAppWidth * 0.30);
float fPosText4 = (iAppWidth * 0.50);
float fPosText5 = (iAppWidth * 0.70);
float fPosText6 = (iAppWidth * 0.85);
float fPosTextSim = (iAppWidth * 0.75);
bool bPatternIsStable = false;
int iCountdownSeconds = 10;
bool bDrawingOn = false;
bool bTimerStopped = false;
ALLEGRO_COLOR oRandColor = al_map_rgb(randr(0, 255), randr(0, 255), randr(0, 255));
while (true) {
al_wait_for_event(pQueue, &oEvent);
if (oEvent.type == ALLEGRO_EVENT_TIMER) {
if (!bTimerStopped) {
if (oEvent.timer.source == pTimer) {
iTotalAlive = 0;
redrawCells(pBuffer, pCells, pNextGenCells, iAppWidth, iAppHeight, oCellColor, oBackgroundColor);
nextGeneration(pCells, pNextGenCells, iAppWidth, iAppHeight, iTotalAlive);
al_set_target_backbuffer(pDisplay);
al_clear_to_color(oBackgroundColor);
al_draw_bitmap(pBuffer, iMarginHorizontal, iMarginVertical, 0);
sHeaderStats = ReplaceString(sHeaderStatistics, "[GENXXXXX]", std::to_string(iGenerations));
sHeaderStats = ReplaceString(sHeaderStats, "[FPSXXXXX]", std::to_string(iFPS));
sHeaderStats = ReplaceString(sHeaderStats, "[CELLSXXXXX]", std::to_string(iTotalAlive));
sHeaderStats = ReplaceString(sHeaderStats, "[SCARXXXXX]", std::to_string(iLifeScarcity));
sHeaderStats = ReplaceString(sHeaderStats, "[TIMEXXXXX]", std::to_string(iSecondsRunning));
if (iGenerations > 0 && iSecondsRunning > 0) {
sHeaderStats = ReplaceString(sHeaderStats, "[GENSSXXXXX]", std::to_string(iGenerations / iSecondsRunning));
}
else {
sHeaderStats = ReplaceString(sHeaderStats, "[GENSSXXXXX]", "0");
}
sSimulations = "SIMS ";
sSimulations.append(std::to_string(iSimulations));
int iLengthSims = al_get_text_width(oFont, sSimulations.c_str());
int iLengthStats = al_get_text_width(oFont, sHeaderStats.c_str());
al_draw_text(oFont, oCellColor, ((iAppWidth - iLengthStats) / 2), 1.0f, 0, sHeaderStats.c_str());
al_draw_text(oFont, oCellColor, (iDisplayWidth - (iLengthSims + 25.0f)), (iAppHeight + iMarginVertical + 5.0f), 0, sSimulations.c_str());
al_draw_text(oFont, oCellColor, 25.0f, (iAppHeight + iMarginVertical + 5.0f), 0, sStats.c_str());
if (bPatternIsStable == true) {
sCountdownText.clear();
sCountdownText.append("PATTERN STABILIZED, RESTARTING IN... ");
int iLengthStr = al_get_text_width(oFontLarge, sCountdownText.c_str());
sCountdownText.append(std::to_string(iCountdownSeconds));
al_draw_text(oFontLarge, oRandColor, ((iAppWidth - iLengthStr) / 2), (iAppHeight * 0.45f), 0, sCountdownText.c_str());
}
al_flip_display();
++iGenerations;
copyCells(pCells, pNextGenCells, iAppWidth, iAppHeight);
if (iTotalPatternCounter == iPatternStableBuffer) {
bPatternIsStable = isPatternStable(iTotalPatternStable, iPatternStableBuffer);
delete iTotalPatternStable;
iTotalPatternStable = new long int[iPatternStableBuffer];
iTotalPatternCounter = 0;
}
iTotalPatternStable[iTotalPatternCounter] = iTotalAlive;
++iTotalPatternCounter;
}
if (oEvent.timer.source == pSecondBySecondTimer) {
if (bPatternIsStable == true) {
if (iCountdownSeconds > 1) {
--iCountdownSeconds;
}
else {
bPatternIsStable = false;
iTotalPatternCounter = 0;
iGenerations = 0;
iSecondsRunning = 0;
iCountdownSeconds = 10;
++iSimulations;
clearCells(pCells, pNextGenCells, iAppWidth, iAppHeight);
randomCells(pCells, iAppWidth, iAppHeight, iLifeScarcity);
}
}
else {
iCountdownSeconds = 10;
}
++iSecondsRunning;
oRandColor = al_map_rgb(randr(0, 255), randr(0, 255), randr(0, 255));
}
}
}
if (oEvent.type == ALLEGRO_EVENT_KEY_DOWN) {
if (oEvent.keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
break;
}
if (oEvent.keyboard.keycode == ALLEGRO_KEY_SPACE) {
if (!bTimerStopped) {
bTimerStopped = true;
al_stop_timer(pTimer);
}
else {
bTimerStopped = false;
al_start_timer(pTimer);
}
}
if (oEvent.keyboard.keycode == ALLEGRO_KEY_R) {
bPatternIsStable = false;
iTotalPatternCounter = 0;
iGenerations = 0;
iSecondsRunning = 0;
iCountdownSeconds = 10;
++iSimulations;
clearCells(pCells, pNextGenCells, iAppWidth, iAppHeight);
randomCells(pCells, iAppWidth, iAppHeight, iLifeScarcity);
}
if (oEvent.keyboard.keycode == ALLEGRO_KEY_S) {
bPatternIsStable = false;
iTotalPatternCounter = 0;
iGenerations = 0;
iSecondsRunning = 0;
iCountdownSeconds = 10;
iLifeScarcity = randr(iLifeMin, iLifeMax);
++iSimulations;
clearCells(pCells, pNextGenCells, iAppWidth, iAppHeight);
randomCells(pCells, iAppWidth, iAppHeight, iLifeScarcity);
}
if (oEvent.keyboard.keycode == ALLEGRO_KEY_M) {
bPatternIsStable = false;
iTotalPatternCounter = 0;
iGenerations = 0;
iSecondsRunning = 0;
iCountdownSeconds = 10;
iLifeScarcity = iLifeMin;
++iSimulations;
clearCells(pCells, pNextGenCells, iAppWidth, iAppHeight);
randomCells(pCells, iAppWidth, iAppHeight, iLifeScarcity);
}
if (oEvent.keyboard.keycode == ALLEGRO_KEY_N) {
bPatternIsStable = false;
iTotalPatternCounter = 0;
iGenerations = 0;
iSecondsRunning = 0;
iCountdownSeconds = 10;
iLifeScarcity = iLifeMax;
++iSimulations;
clearCells(pCells, pNextGenCells, iAppWidth, iAppHeight);
randomCells(pCells, iAppWidth, iAppHeight, iLifeScarcity);
}
if (oEvent.keyboard.keycode == ALLEGRO_KEY_F) {
++iFPS;
al_set_timer_speed(pTimer, (1.0f / iFPS));
}
if (oEvent.keyboard.keycode == ALLEGRO_KEY_G) {
if(iFPS > 3) {
--iFPS;
}
al_set_timer_speed(pTimer, (1.0f / iFPS));
}
if (oEvent.keyboard.keycode == ALLEGRO_KEY_C) {
int iRCell = randr(0, 255);
int iGCell = randr(0, 255);
int iBCell = randr(0, 255);
oCellColor = al_map_rgb(iRCell, iGCell, iBCell);
}
}
} // End main loop
al_destroy_event_queue(pQueue);
al_destroy_display(pDisplay);
delete iTotalPatternStable;
for (short int i = 0; i < iAppWidth; i++) {
delete pCells[i];
delete pNextGenCells[i];
}
delete[] pCells;
delete[] pNextGenCells;
return 0;
}