-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecavisual.c
444 lines (323 loc) · 12 KB
/
ecavisual.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
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
/*
ecavisual.c
Copyright (c) 2018 andynines
MIT License
*/
// Includes
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "eca.h"
#include "ecaio.h"
#include "allocs.h"
#include "utils.h"
#include "SDL.h"
// TODO: Test with Valgrind memcheck
// Constants
#define WINDOW_TITLE ("ECA Visual")
#define SCREEN_WIDTH (480)
#define SCREEN_HEIGHT (360)
#define BACKGROUND_R (0xFF)
#define BACKGROUND_G (0xFF)
#define BACKGROUND_B (0xFF)
#define BACKGROUND_A (0xFF)
#define CELL_R (0x0)
#define CELL_G (0x0)
#define CELL_B (0x0)
#define CELL_A (0xFF)
// Data structures
struct rectNode {
SDL_Rect cellRect;
int colIndex;
struct rectNode* nextNodePtr;
};
typedef struct rectNode RectNode;
typedef struct {
RectNode* intlNodePtr;
} RectRow;
typedef struct {
int age;
int xStretch;
int yStretch;
SDL_Rect background;
RectRow* rowArr;
} Drawing;
typedef struct {
bool active;
SDL_Window* windowPtr;
SDL_Renderer* rendererPtr;
SDL_Event event;
Simulation* simPtr;
Drawing* drawingPtr;
} App;
// Functions for working with rectangle linked lists
RectNode* createNode(Drawing* drawingPtr, int rowIndex, int colIndex) {
// Create a new RectNode struct
RectNode* newNodePtr;
newNodePtr = (RectNode*) safeMalloc(sizeof(RectNode));
newNodePtr->cellRect = (SDL_Rect) {colIndex * (drawingPtr->xStretch),
rowIndex * (drawingPtr->yStretch),
drawingPtr->xStretch,
drawingPtr->yStretch};
newNodePtr->colIndex = colIndex;
newNodePtr->nextNodePtr = (RectNode*) 0;
return newNodePtr;
}
RectNode* findLastNode(RectNode* intlNodePtr) {
// Return pointer to last RectNode in a row; return null pointer if list is empty
RectNode* currentNodePtr;
RectNode* nextNodePtr;
if ((currentNodePtr = intlNodePtr) == (RectNode*) 0) {
return intlNodePtr;
}
while ((nextNodePtr = currentNodePtr->nextNodePtr) != (RectNode*) 0) {
currentNodePtr = nextNodePtr;
}
return currentNodePtr;
}
void storeRect(Drawing* drawingPtr, int rowIndex, int colIndex) {
// Store an SDL_Rect at the specified location in the drawing
RectNode* newNodePtr;
RectNode* lastNodePtr;
RectNode* createNode(Drawing* drawingPtr, int rowIndex, int colIndex);
RectNode* findLastNode(RectNode* intlNodePtr);
newNodePtr = createNode(drawingPtr, rowIndex, colIndex);
lastNodePtr = findLastNode(drawingPtr->rowArr[rowIndex].intlNodePtr);
if (lastNodePtr == (RectNode*) 0) {
drawingPtr->rowArr[rowIndex].intlNodePtr = newNodePtr;
} else {
lastNodePtr->nextNodePtr = newNodePtr;
}
}
void storeRow(App* appPtr, int rowIndex) {
// Create a linked list of SDL_Rects representing a single sim generation
int colIndex;
Simulation* simPtr;
bool rectState;
extern bool getCellState(CellBlock* intlBlockPtr, int colIndex);
void storeRect(Drawing* drawingPtr, int rowIndex, int colIndex);
simPtr = appPtr->simPtr;
for (colIndex = 0; colIndex < (simPtr->habitatSize); ++colIndex) {
rectState = getCellState(simPtr->genArr[rowIndex].blockArr, colIndex);
if (rectState) {
storeRect(appPtr->drawingPtr, rowIndex, colIndex);
}
}
}
void destroyRow(App* appPtr, int rowIndex) {
// Remove a row of rectangles from memory
RectNode* currentNodePtr;
RectNode* nextNodePtr;
currentNodePtr = appPtr->drawingPtr->rowArr[rowIndex].intlNodePtr;
while (currentNodePtr != (RectNode*) 0) {
nextNodePtr = currentNodePtr->nextNodePtr;
free(currentNodePtr);
currentNodePtr = nextNodePtr;
}
appPtr->drawingPtr->rowArr[rowIndex].intlNodePtr = (RectNode*) 0;
}
void shiftRectsUp(Drawing* drawingPtr, int rowIndex) {
// Move through a list of rects and shift each one's y coordinate
RectNode* currentNodePtr;
if ((currentNodePtr = drawingPtr->rowArr[rowIndex].intlNodePtr) == (RectNode*) 0) {
return;
}
do {
currentNodePtr->cellRect.y -= drawingPtr->yStretch;
} while ((currentNodePtr = currentNodePtr->nextNodePtr) != (RectNode*) 0);
}
void shiftRowsUp(App* appPtr) {
// Move up each rect row in a drawing
int genBufferSize;
Drawing* drawingPtr;
int rowIndex;
void destroyRow(App* appPtr, int rowIndex);
void shiftRectsUp(Drawing* drawingPtr, int rowIndex);
genBufferSize = appPtr->simPtr->genBufferSize;
drawingPtr = appPtr->drawingPtr;
// Destroy the first generation - will be overwritten
destroyRow(appPtr, 0);
// Shift back the generations that will persist in the buffer
for (rowIndex = 0; rowIndex < (genBufferSize - 1); ++rowIndex) {
drawingPtr->rowArr[rowIndex].intlNodePtr = drawingPtr->rowArr[rowIndex + 1].intlNodePtr;
shiftRectsUp(drawingPtr, rowIndex);
}
drawingPtr->rowArr[genBufferSize - 1].intlNodePtr = (RectNode*) 0;
}
// Functions for working with the drawing struct
Drawing* createDrawing(Simulation* simPtr) {
// Allocate memory to store SDL drawing information
Drawing* newDrawingPtr;
newDrawingPtr = (Drawing*) safeMalloc(sizeof(Drawing));
newDrawingPtr->age = 0;
newDrawingPtr->xStretch = SCREEN_WIDTH / (simPtr->habitatSize);
newDrawingPtr->yStretch = SCREEN_HEIGHT / (simPtr->genBufferSize);
newDrawingPtr->background = (SDL_Rect) {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
newDrawingPtr->rowArr = (RectRow*) safeMalloc(sizeof(RectRow) * (simPtr->genBufferSize));
return newDrawingPtr;
}
void initDrawing(App* appPtr) {
// Erase and redraw the entirety of a visual sim
int rowIndex;
extern void iterateSim(Simulation* simPtr, int iterations);
void destroyRow(App* appPtr, int rowIndex);
void storeRow(App* appPtr, int rowIndex);
iterateSim(appPtr->simPtr, (appPtr->simPtr->genBufferSize) - 1);
for (rowIndex = 0; rowIndex < (appPtr->simPtr->genBufferSize); ++rowIndex) {
destroyRow(appPtr, rowIndex);
storeRow(appPtr, rowIndex);
}
}
void iterateDrawing(App* appPtr) {
// Update a drawing struct to correspond with an iterated sim
extern void iterateSim(Simulation* simPtr, int iterations);
void shiftRowsUp(App* appPtr);
void storeRow(App* appPtr, int rowIndex);
iterateSim(appPtr->simPtr, 1);
shiftRowsUp(appPtr);
storeRow(appPtr, (appPtr->simPtr->genBufferSize) - 1);
}
void destroyDrawing(App* appPtr) {
// Remove all of a drawing's data from memory
int rowIndex;
void destroyRow(App* appPtr, int rowIndex);
for (rowIndex = 0; rowIndex < (appPtr->simPtr->genBufferSize); ++rowIndex) {
destroyRow(appPtr, rowIndex);
}
free(appPtr->drawingPtr->rowArr);
free(appPtr->drawingPtr);
}
// Methods for operating app
App* errorSDL(char* errorMessage) {
// Write SDL error to terminal
fprintf(stderr, "%s\n%s\n", errorMessage, SDL_GetError());
return (App*) 0;
}
App* createApp(Simulation* simPtr, Drawing* drawingPtr) {
// Create a structure that stores all data for graphical representation
App* newAppPtr;
App* errorSDL(char* errorMessage);
newAppPtr = (App*) safeMalloc(sizeof(App));
newAppPtr->active = true;
newAppPtr->windowPtr = NULL;
newAppPtr->rendererPtr = NULL;
newAppPtr->simPtr = simPtr;
newAppPtr->drawingPtr = drawingPtr;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
return errorSDL("Could not initialize");
}
newAppPtr->windowPtr = SDL_CreateWindow(WINDOW_TITLE,
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_SHOWN);
if (newAppPtr->windowPtr == NULL) {
return errorSDL("Could not create window");
}
newAppPtr->rendererPtr = SDL_CreateRenderer(newAppPtr->windowPtr, -1, SDL_RENDERER_ACCELERATED);
if (newAppPtr->rendererPtr == NULL) {
return errorSDL("Could not create renderer");
}
return newAppPtr;
}
void destroyApp(App* appPtr) {
// Remove an entire application's data from memory
extern void destroySim(Simulation* simPtr);
void destroyDrawing(App* appPtr);
destroySim(appPtr->simPtr);
destroyDrawing(appPtr);
free(appPtr);
}
void draw(App* appPtr) {
// Create a visual representation of a simulation
int rowIndex;
RectNode* currentNodePtr;
SDL_RenderClear(appPtr->rendererPtr);
// Draw background
SDL_SetRenderDrawColor(appPtr->rendererPtr, BACKGROUND_R,
BACKGROUND_G,
BACKGROUND_B,
BACKGROUND_A);
SDL_RenderFillRect(appPtr->rendererPtr, &(appPtr->drawingPtr->background));
// Move through each row and render rectangles representing live cells
SDL_SetRenderDrawColor(appPtr->rendererPtr, CELL_R,
CELL_G,
CELL_B,
CELL_A);
for (rowIndex = 0; rowIndex < (appPtr->simPtr->genBufferSize); ++rowIndex) {
currentNodePtr = appPtr->drawingPtr->rowArr[rowIndex].intlNodePtr;
while (currentNodePtr != (RectNode*) 0) {
SDL_RenderFillRect(appPtr->rendererPtr, &(currentNodePtr->cellRect));
currentNodePtr = currentNodePtr->nextNodePtr;
}
}
SDL_RenderPresent(appPtr->rendererPtr);
}
void resizeSim(Simulation* simPtr, int habitatSize, int genBufferSize) {
// Resize a simulation that is too large to fit the window
int genIndex;
int blockReq;
CellBlock* currentBlockPtr;
simPtr->genArr = safeRealloc(simPtr->genArr, sizeof(Generation) * SCREEN_HEIGHT);
simPtr->genBufferSize = genBufferSize;
blockReq = (BLOCK_BITS + SCREEN_WIDTH - 1) / BLOCK_BITS; // Ceiling division
for (genIndex = 0; genIndex < genBufferSize; ++genIndex) {
currentBlockPtr = simPtr->genArr[genIndex].blockArr;
simPtr->genArr[genIndex].blockArr = safeRealloc(currentBlockPtr, BLOCK_BYTES * blockReq);
}
simPtr->habitatSize = habitatSize;
fprintf(stderr,
"Warning: Adjusted simulation size to fit current window\n"
"Current window size: %ix%i pixels\n"
"\n",
SCREEN_WIDTH, SCREEN_HEIGHT);
}
int operateApp(App* appPtr) {
// Graphical application logic
extern void infoStr(Simulation* simPtr);
void initDrawing(App* appPtr);
void iterateDrawing(App* appPtr);
void draw(App* appPtr);
void destroyApp(App* appPtr);
infoStr(appPtr->simPtr);
initDrawing(appPtr);
while (appPtr->active) {
draw(appPtr);
SDL_WaitEvent(&(appPtr->event));
if (appPtr->event.type == SDL_QUIT) {
appPtr->active = false;
} else if (appPtr->event.type == SDL_KEYDOWN) {
if (appPtr->event.key.keysym.sym == SDLK_SPACE) {
iterateDrawing(appPtr);
}
}
}
SDL_Quit();
destroyApp(appPtr);
return EXIT_SUCCESS;
}
int main(int argc, char* argv[]) {
// Build all parts of the simulation and then let it loose
Simulation* simPtr;
Drawing* drawingPtr;
App* appPtr;
extern Simulation* createUserSim(int argc, char* argv[]);
void resizeSim(Simulation* simPtr, int habitatSize, int genBufferSize);
Drawing* createDrawing(Simulation* simPtr);
App* createApp(Simulation* simPtr, Drawing* drawingPtr);
int operateApp(App* appPtr);
simPtr = createUserSim(argc, argv);
if (simPtr == (Simulation*) 0) {
return EXIT_FAILURE;
}
if (((simPtr->habitatSize) > SCREEN_WIDTH) || ((simPtr->genBufferSize) > SCREEN_HEIGHT)) {
resizeSim(simPtr, SCREEN_WIDTH, SCREEN_HEIGHT);
}
drawingPtr = createDrawing(simPtr);
appPtr = createApp(simPtr, drawingPtr);
if (appPtr == (App*) 0) {
return EXIT_FAILURE;
}
return operateApp(appPtr);
}