-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcgl.c
172 lines (155 loc) · 4.12 KB
/
cgl.c
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
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#ifdef __EMSCRIPTEN__
#include <GLES2/gl2.h>
#include <emscripten.h>
#endif
#include "cgl.h"
#define EXTERN
// Configuration
#define DEFAULT_SIZE 50
#define PROB_ALIVE 70 // %
// State
unsigned int size = DEFAULT_SIZE;
bool paused = false;
bool **cells = NULL;
unsigned int generation = 0;
unsigned int population = 0;
#ifdef __EMSCRIPTEN__
EMSCRIPTEN_KEEPALIVE
#endif
bool **init_cells() {
bool **c = (bool **)malloc(size * sizeof(bool *));
assert(c);
for (unsigned int i = 0; i < size; ++i) {
c[i] = (bool *)malloc(size * sizeof(bool));
}
for (unsigned int row = 0; row < size; ++row) {
assert(c[row]);
for (unsigned int col = 0; col < size; ++col) {
c[row][col] = false;
}
}
return c;
}
#ifdef __EMSCRIPTEN__
EMSCRIPTEN_KEEPALIVE
#endif
void randomize_cells(bool **c) {
for (unsigned int row = 0; row < size; ++row) {
// printf("c: %d c[%d]: %d\n", c, row, c[row]);
assert(c[row]);
for (unsigned int col = 0; col < size; ++col) {
int prob = rand() % 100; // [0, 100]
bool alive = (prob < PROB_ALIVE);
// printf("ROW: %d COL: %d ALIVE: %d\n", row, col, alive);
c[row][col] = alive;
}
}
}
#ifdef __EMSCRIPTEN__
EMSCRIPTEN_KEEPALIVE
#endif
void free_cells(bool **c) {
for (unsigned int i = 0; i < size; ++i) {
if (c[i]) free(c[i]);
}
if (c) free(c);
}
#ifdef __EMSCRIPTEN__
EMSCRIPTEN_KEEPALIVE
#endif
void loop() {
printf("Generation: %d, Population: %d\n", generation, population);
#ifdef __EMSCRIPTEN__
glClear(GL_COLOR_BUFFER_BIT);
#endif
tick();
}
#ifdef __EMSCRIPTEN__
EMSCRIPTEN_KEEPALIVE
#endif
void tick() {
bool **next = NULL;
next = init_cells();
advance(cells, next);
free_cells(cells);
cells = next;
++generation;
}
#ifdef __EMSCRIPTEN__
EMSCRIPTEN_KEEPALIVE
#endif
bool is_paused() { return paused; }
#ifdef __EMSCRIPTEN__
EMSCRIPTEN_KEEPALIVE
#endif
void toggle_play_pause() {
paused = !paused;
if (paused) {
#ifdef __EMSCRIPTEN__
emscripten_pause_main_loop();
#endif
printf("PAUSED\n");
} else {
#ifdef __EMSCRIPTEN__
emscripten_resume_main_loop();
#endif
printf("RESUMED\n");
}
}
#ifdef __EMSCRIPTEN__
EMSCRIPTEN_KEEPALIVE
#endif
unsigned int get_size() { return size; }
#ifdef __EMSCRIPTEN__
EMSCRIPTEN_KEEPALIVE
#endif
unsigned int get_population() { return population; }
#ifdef __EMSCRIPTEN__
EMSCRIPTEN_KEEPALIVE
#endif
unsigned int get_adj_cells_alive(bool **c, unsigned int row, unsigned int col) {
/*
1 2 3
4 X 6
7 8 9
*/
unsigned int count = 0;
if (row > 0 && col > 0 && c[row - 1][col - 1]) ++count; // 1
if (row > 0 && c[row - 1][col]) ++count; // 2
if (row < (size - 1) && col < (size - 1) && c[row + 1][col + 1]) ++count; // 3
if (col > 0 && c[row][col - 1]) ++count; // 4
if (col < (size - 1) && c[row][col + 1]) ++count; // 6
if (row < (size - 1) && col > 0 && c[row + 1][col - 1]) ++count; // 7
if (row < (size - 1) && c[row + 1][col]) ++count; // 8
if (row < (size - 1) && col < (size - 1) && c[row + 1][col + 1]) ++count; // 9
return count;
}
#ifdef __EMSCRIPTEN__
EMSCRIPTEN_KEEPALIVE
#endif
void advance(bool **curr, bool **next) {
unsigned int next_population = 0;
for (unsigned int row = 0; row < size; ++row) {
for (unsigned int col = 0; col < size; ++col) {
bool alive = curr[row][col];
unsigned int adj_cells_alive = get_adj_cells_alive(curr, row, col);
if (alive) {
if (adj_cells_alive < 2 || adj_cells_alive > 3) {
alive = false;
}
} else {
if (adj_cells_alive == 3) {
alive = true;
}
}
next[row][col] = alive;
if (alive) ++next_population;
}
}
population = next_population;
}