-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwater-sdl.c
330 lines (288 loc) · 7.83 KB
/
water-sdl.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL2/SDL.h>
#define SIZE 2000
#define SPRING_X 500
#define SPRING_Y 0
enum Tile {
TILE_EMPTY,
TILE_CLAY,
TILE_WATER,
TILE_FLOWING,
};
int** alloc_map(void) {
const int map_size = SIZE * SIZE * sizeof(int);
int *map = malloc(map_size);
if(map == NULL) {
perror("Failed to alloc map");
exit(EXIT_FAILURE);
}
int **rows = malloc(SIZE * sizeof(int*));
if(rows == NULL) {
perror("Failed to alloc rows");
exit(EXIT_FAILURE);
}
memset(map, 0, map_size);
for(int i = 0; i < SIZE; ++i) {
rows[i] = map + (i*SIZE);
}
return rows;
}
struct MapSize {
int x_min, x_max;
int y_min, y_max;
};
struct MapSize read_input(int **map) {
struct MapSize ms = {
.x_min = SIZE,
.x_max = 0,
.y_min = SIZE,
.y_max = 0,
};
char axis, unused;
int pos, start, end;
while(scanf("%c=%d, %c=%d..%d\n", &axis, &pos, &unused, &start, &end) == 5) {
if(axis == 'x') {
for(int y = start; y <= end; ++y) {
map[y][pos] = TILE_CLAY;
}
if(pos < ms.x_min) ms.x_min = pos;
if(pos > ms.x_max) ms.x_max = pos;
if(start < ms.y_min) ms.y_min = start;
if(end > ms.y_max) ms.y_max = end;
} else if(axis == 'y') {
for(int x = start; x <= end; ++x) {
map[pos][x] = TILE_CLAY;
}
if(start < ms.x_min) ms.x_min = start;
if(end > ms.x_max) ms.x_max = end;
if(pos < ms.y_min) ms.y_min = pos;
if(pos > ms.y_max) ms.y_max = pos;
} else {
fprintf(stderr,
"Failed to parse line: %c=%d, %c=%d..%d\n",
axis, pos, unused, start, end
);
}
}
return ms;
}
void open_window(const struct MapSize *ms, const int scale, SDL_Window **win, SDL_Renderer **ren) {
// +2 so we can see one tile to the left and one to the right
const int window_w = ms->x_max - ms->x_min + 1 + 2;
const int window_h = (ms->y_max + 1 < 1500) ? (ms->y_max + 1) : 1500;
if(SDL_Init(SDL_INIT_EVENTS | SDL_INIT_TIMER | SDL_INIT_VIDEO)) {
fprintf(stderr, "SDL2 init failed: %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
if(SDL_CreateWindowAndRenderer(window_w * scale, window_h * scale, 0, win, ren)) {
fprintf(stderr, "Failed to open SDL2 window: %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
SDL_RenderSetLogicalSize(*ren, window_w, window_h);
}
void drawFrame(SDL_Renderer *ren, int **map, const struct MapSize *ms, const int cameraY) {
SDL_SetRenderDrawColor(ren, 0, 0, 0, 255);
SDL_RenderClear(ren);
for(int y = (cameraY > 0) ? cameraY : 0; y <= ms->y_max; ++y) {
for(int x = ms->x_min - 1; x <= ms->x_max + 1; ++x) {
switch(map[y][x]) {
case TILE_CLAY:
SDL_SetRenderDrawColor(ren, 127, 127, 127, 255);
break;
case TILE_FLOWING:
SDL_SetRenderDrawColor(ren, 0, 195, 255, 255);
break;
case TILE_WATER:
SDL_SetRenderDrawColor(ren, 0, 0, 255, 255);
break;
case TILE_EMPTY:
goto skip;
}
int pixel_x = x - (ms->x_min - 1);
SDL_RenderDrawPoint(ren, pixel_x, y - cameraY);
skip: ;
}
}
SDL_RenderPresent(ren);
}
void process_events(int *quit, int *pause, int *cameraY) {
SDL_Event ev;
while(SDL_PollEvent(&ev)) {
if(ev.type == SDL_QUIT)
*quit = 1;
else if(ev.type == SDL_KEYDOWN) {
if(ev.key.keysym.sym == SDLK_ESCAPE)
*quit = 1;
else if(ev.key.keysym.sym == SDLK_SPACE)
*pause = !*pause;
} else if(ev.type == SDL_MOUSEWHEEL) {
*cameraY -= ev.wheel.y;
}
}
}
void parse_options(int argc, char **argv, int *scale, int *timestep) {
*scale = 1;
*timestep = 50;
int i = 1;
while(i < argc) {
if(strcmp(argv[i], "--scale") == 0) {
if(i + 1 == argc) {
fprintf(stderr, "Error: the --scale option requires an argument\n");
exit(EXIT_FAILURE);
}
*scale = atoi(argv[i+1]);
i += 2;
} else if(strcmp(argv[i], "--timestep") == 0) {
if(i + 1 == argc) {
fprintf(stderr, "Error: the --scale option requires an argument\n");
exit(EXIT_FAILURE);
}
*timestep = atoi(argv[i+1]);
i += 2;
} else {
fprintf(stderr, "Error: unexpected command-line argument \"%s\"\n", argv[i]);
exit(EXIT_FAILURE);
}
}
}
struct FlowGroup {
int active;
int x_min, x_max, y;
struct FlowGroup *child_left, *child_right;
};
struct FlowGroup* flowGroup_new(const int x, const int y) {
struct FlowGroup *fg = malloc(sizeof(struct FlowGroup));
*fg = (struct FlowGroup) {
.active = 1,
.x_min = x,
.x_max = x,
.y = y,
.child_left = NULL,
.child_right = NULL,
};
return fg;
}
int flowGroup_process(struct FlowGroup *fg, int **map, const struct MapSize *ms) {
// groups below the max_y are active indefinitely
if(fg->y > ms->y_max) return 0;
int changes = 0;
int childChanges = 0;
// Check current child groups
if(fg->child_left) {
childChanges += flowGroup_process(fg->child_left, map, ms);
if(!fg->child_left->active) {
free(fg->child_left);
fg->child_left = NULL;
}
}
if(fg->child_right) {
childChanges += flowGroup_process(fg->child_right, map, ms);
if(!fg->child_right->active) {
free(fg->child_right);
fg->child_right = NULL;
}
}
// Try to create a new child groups, or expand left/right
while(!fg->child_left) {
const enum Tile under_min = map[fg->y + 1][fg->x_min];
if((under_min == TILE_CLAY) || (under_min == TILE_WATER)) {
const enum Tile left = map[fg->y][fg->x_min - 1];
if((left == TILE_EMPTY) || (left == TILE_FLOWING)) {
// printf("Expanded from %d-%d:%d to %d-%d:%d\n", fg->x_min, fg->x_max, fg->y, fg->x_min-1, fg->x_max, fg->y);
map[fg->y][fg->x_min - 1] = TILE_FLOWING;
fg->x_min -= 1;
changes = 1;
} else {
break;
}
} else if(under_min == TILE_EMPTY) {
fg->child_left = flowGroup_new(fg->x_min, fg->y+1);
map[fg->y+1][fg->x_min] = TILE_FLOWING;
changes = 1;
} else {
break;
}
}
while(!fg->child_right) {
const enum Tile under_max = map[fg->y + 1][fg->x_max];
if((under_max == TILE_CLAY) || (under_max == TILE_WATER)) {
const enum Tile right = map[fg->y][fg->x_max + 1];
if((right == TILE_EMPTY) || (right == TILE_FLOWING)) {
// printf("Expanded from %d-%d:%d to %d-%d:%d\n", fg->x_min, fg->x_max, fg->y, fg->x_min, fg->x_max+1, fg->y);
map[fg->y][fg->x_max + 1] = TILE_FLOWING;
fg->x_max += 1;
changes = 1;
} else {
break;
}
} else if((under_max == TILE_EMPTY) && (fg->x_min != fg->x_max)) {
fg->child_right = flowGroup_new(fg->x_max, fg->y + 1);
map[fg->y+1][fg->x_max] = TILE_FLOWING;
changes = 1;
} else {
break;
}
}
if(!fg->child_left && !fg->child_right) {
fg->active = 0;
int under_flowing = 0;
for(int x = fg->x_min; x <= fg->x_max; ++x) {
const enum Tile under = map[fg->y + 1][x];
if(under == TILE_FLOWING) {
under_flowing = 1;
break;
}
}
if(!under_flowing) {
for(int x = fg->x_min; x <= fg->x_max; ++x) {
map[fg->y][x] = TILE_WATER;
}
}
changes = 1;
}
return changes + childChanges;
}
int main(int argc, char **argv) {
int scale, timestep;
parse_options(argc, argv, &scale, ×tep);
int **map = alloc_map();
struct MapSize ms = read_input(map);
SDL_Window *win;
SDL_Renderer *ren;
open_window(&ms, scale, &win, &ren);
drawFrame(ren, map, &ms, 0);
struct FlowGroup *spring = flowGroup_new(SPRING_X, SPRING_Y);
map[SPRING_Y][SPRING_X] = TILE_FLOWING;
int quit = 0;
int pause = 0;
int camera_y = 0;
while(!quit) {
SDL_Delay(timestep);
process_events(&quit, &pause, &camera_y);
if(pause) continue;
int changes = flowGroup_process(spring, map, &ms);
if(changes == 0) {
printf("No more changes - stopping\n");
SDL_Delay(5000);
break;
}
drawFrame(ren, map, &ms, camera_y);
}
int flowing_tiles = 0;
int still_tiles = 0;
for(int y = ms.y_min; y <= ms.y_max; ++y) {
for(int x = ms.x_min - 1; x <= ms.x_max + 1; ++x) {
switch(map[y][x]) {
case TILE_FLOWING:
flowing_tiles += 1;
break;
case TILE_WATER:
still_tiles += 1;
break;
}
}
}
printf("%d water tiles total - %d still tiles and %d flowing tiles\n", still_tiles + flowing_tiles, still_tiles, flowing_tiles);
}