-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
282 lines (240 loc) · 8.88 KB
/
main.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
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
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// Initial window size
#define WINDOW_HEIGHT 1000
#define WINDOW_WIDTH 1000
// controls the number of cells both vertically and horizontally
#define GRID_DIVISIONS 100
// boolean definitions
#define FALSE 0
#define TRUE 1
// key definitions
#define KEY_PAUSE 65 // space
#define KEY_DECREASE 20 // minus
#define KEY_INCREASE 21 // plus
// Ticks per second (roughly based on clock cycles)
#define INITIAL_TPS 2
// for holding all data regarding the rendering of the grid in the window as well as the window itself
typedef struct XWinData
{
Display *display;
Window window;
GC graphicsContext;
XWindowAttributes windowAttributes;
int screen;
// color setup, mostly for telemetry type text drawn in window
Colormap colormap;
XColor red;
XColor green;
XColor blue;
XColor grey;
}XWinData;
// for holding data relivant to the simulation behind the scenes
typedef struct SimulationData
{
int running;
int TPS;
int ticksFromStart;
clock_t lastTickTime;
int** mainGrid;
int** swapGrid;
}SimulationData;
// function to redraw the window when changes are made in the array or when the window is resized
void redrawGrid(XWinData *winData, SimulationData simData);
void tickGridArray(SimulationData* simData);
int sumAreaAroundPoint(int x, int y, int** grid);
int** allocGrid(int w, int h);
int main(int ac, char** av)
{
XWinData winData;
SimulationData simData;
XEvent event;
int x,y;
simData.mainGrid = allocGrid(GRID_DIVISIONS, GRID_DIVISIONS);
simData.swapGrid = allocGrid(GRID_DIVISIONS, GRID_DIVISIONS);
simData.ticksFromStart = 0;
simData.TPS = INITIAL_TPS;
simData.running = FALSE;
simData.lastTickTime = clock() / (CLOCKS_PER_SEC / 1000);
winData.display = XOpenDisplay(NULL);
if(winData.display == NULL)
{
fprintf(stderr, "Failed to open display\n");
exit(1);
}
winData.screen = DefaultScreen(winData.display);
winData.window = XCreateSimpleWindow(winData.display,
RootWindow(winData.display, winData.screen),
0, 0, // window starting x and y location
WINDOW_HEIGHT, WINDOW_WIDTH, // window starting height and width values
0, // window border thickness
BlackPixel(winData.display, winData.screen), // foreground color
WhitePixel(winData.display, winData.screen)); // background color
// setting window name, for style points obviously
XStoreName(winData.display, winData.window, "Game of Life");
// we want to process clicks and keyboard input, as well as when the window becomes visible or is resized
XSelectInput(winData.display, winData.window, ExposureMask | KeyPressMask | ButtonPressMask);
winData.graphicsContext = XCreateGC(winData.display, winData.window, 0, NULL);
// getting color set up in window data structure, XAllocNamedColor can return an error value... which I am choosing to ignore for now
winData.colormap = DefaultColormap(winData.display, winData.screen);
XAllocNamedColor(winData.display, winData.colormap, "red", &winData.red, &winData.red);
XAllocNamedColor(winData.display, winData.colormap, "green", &winData.green, &winData.green);
XAllocNamedColor(winData.display, winData.colormap, "blue", &winData.blue, &winData.blue);
XAllocNamedColor(winData.display, winData.colormap, "gray92", &winData.grey, &winData.grey);
// make window visible
XMapWindow(winData.display, winData.window);
while(1)
{
// only attempt to process events if there is events waiting
// doing it this way because XNextEvent is a blocking call and would cause issues
// with simulation timing
if(XPending(winData.display))
{
XNextEvent(winData.display, &event);
// window resize or reveal, we basically just want to update the window data and redraw the whole thing
if(event.type == Expose)
{
XGetWindowAttributes(winData.display, winData.window, &winData.windowAttributes);
}
if(event.type == ButtonPress && !simData.running)
{
x = event.xbutton.x / (winData.windowAttributes.width/GRID_DIVISIONS);
y = event.xbutton.y / (winData.windowAttributes.height/GRID_DIVISIONS);
simData.mainGrid[x][y] = !simData.mainGrid[x][y];
}
if(event.type == KeyPress)
{
switch(event.xkey.keycode)
{
case KEY_PAUSE:
simData.running = !simData.running;
break;
case KEY_INCREASE:
simData.TPS++;
break;
case KEY_DECREASE:
if(simData.TPS > 1)
simData.TPS--;
break;
default:
//printf("Key Pressed %d\n", event.xkey.keycode); << for finding keycodes... disabled normally
}
}
redrawGrid(&winData, simData);
}
// tick timer
if((clock() / (CLOCKS_PER_SEC / 1000)) - simData.lastTickTime > 1000/simData.TPS && simData.running)
{
simData.ticksFromStart += 1;
simData.lastTickTime = clock() / (CLOCKS_PER_SEC / 1000);
tickGridArray(&simData);
redrawGrid(&winData, simData);
}
}
XCloseDisplay(winData.display);
return 0;
}
int** allocGrid(int w, int h)
{
int** grid = calloc(w, sizeof(int*));
for(int i = 0; i < w; i++)
{
grid[i] = calloc(h, sizeof(int));
}
return grid;
}
void tickGridArray(SimulationData* simData)
{
int sum;
for(int x = 0; x < GRID_DIVISIONS; x++)
{
for(int y = 0; y < GRID_DIVISIONS; y++)
{
sum = sumAreaAroundPoint(x, y, simData->mainGrid);
if(sum == 3)
simData->swapGrid[x][y] = 1;
else if(sum < 2 || sum > 3)
simData->swapGrid[x][y] = 0;
else
simData->swapGrid[x][y] = simData->mainGrid[x][y];
}
}
int** tmp;
tmp = simData->mainGrid;
simData->mainGrid = simData->swapGrid;
simData->swapGrid = tmp;
}
int sumAreaAroundPoint(int x, int y, int** grid)
{
int sum = 0;
for(int i = -1; i <= 1; i++)
{
for(int j = -1; j <= 1; j++)
{
if((i != 0 || j != 0) &&
x+i >= 0 && y+j >= 0 &&
x+i < GRID_DIVISIONS &&
y+j < GRID_DIVISIONS)
{
sum += grid[x+i][y+j];
}
else if((i != 0 || j != 0))
{
// this section is my edge handling rule, not part of the actual game of life
// due to an actual infinite grid being imposible to impliment...
// I simply chose to do this as a way to break stable patterns near edges (stop gliders from becoming static blocks)
sum -= grid[x][y];
}
}
}
return sum;
}
void redrawGrid(XWinData *winData, SimulationData simData)
{
int** grid = simData.mainGrid;
// clear screen by temporarilly changing the foreground color to white and drawing over the whole window
XSetForeground(winData->display, winData->graphicsContext, WhitePixel(winData->display, winData->screen));
XFillRectangle(winData->display, winData->window, winData->graphicsContext, 0,0, winData->windowAttributes.width, winData->windowAttributes.height);
XSetForeground(winData->display, winData->graphicsContext, BlackPixel(winData->display, winData->screen));
XSetForeground(winData->display, winData->graphicsContext, winData->grey.pixel);
for(int i = 1; i < GRID_DIVISIONS; i++)
{
XDrawLine(winData->display, winData->window, winData->graphicsContext, i*(winData->windowAttributes.width/GRID_DIVISIONS), 0, i*(winData->windowAttributes.width/GRID_DIVISIONS), winData->windowAttributes.height);
XDrawLine(winData->display, winData->window, winData->graphicsContext, 0, i*(winData->windowAttributes.height/GRID_DIVISIONS), winData->windowAttributes.width, i*(winData->windowAttributes.height/GRID_DIVISIONS));
}
XSetForeground(winData->display, winData->graphicsContext, BlackPixel(winData->display, winData->screen));
// draw in every square of the grid based on the value contained in the associated 2D array
for(int x = 0; x < GRID_DIVISIONS; x++)
{
for(int y = 0; y < GRID_DIVISIONS; y++)
{
if(grid[x][y] == 1)
{
XFillRectangle(winData->display,
winData->window,
winData->graphicsContext,
x*(winData->windowAttributes.width / GRID_DIVISIONS), // x location to draw at
y*(winData->windowAttributes.height / GRID_DIVISIONS), // y location to draw at
(winData->windowAttributes.width / GRID_DIVISIONS), // width of rectangle to draw
(winData->windowAttributes.height / GRID_DIVISIONS)); // height of rectangle to draw
}
}
}
if(simData.running)
{
XSetForeground(winData->display, winData->graphicsContext, winData->green.pixel);
XDrawString(winData->display, winData->window, winData->graphicsContext, 3,15, "Simulation Running", 18);
}
else
{
XSetForeground(winData->display, winData->graphicsContext, winData->red.pixel);
XDrawString(winData->display, winData->window, winData->graphicsContext, 3,15, "Simulation Paused", 17);
}
char telemetry[255] = {0};
sprintf(telemetry ,"TPS: %d, Current Tick: %d", simData.TPS, simData.ticksFromStart);
XSetForeground(winData->display, winData->graphicsContext, winData->blue.pixel);
XDrawString(winData->display, winData->window, winData->graphicsContext, winData->windowAttributes.width/2, 15, telemetry, strlen(telemetry));
}