summaryrefslogtreecommitdiff
path: root/viewer.c
blob: 1d427aed874c1acbf0852ae8aabb0586c5b30d88 (plain)
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
// Compile with gcc viewer.c `sdl2-config --cflags --libs` -lSDL2_image
#include "SDL.h"
#include "SDL_image.h"

/* Indental */
struct indental {
	char *key, *value, *txt;
	int indent;
	struct indental *next, *children;
};

void parse_indental_line(char *line, struct indental *entry) {
	entry->txt = line; // For deallocator.

	entry->indent = 0;
	while (isspace(*line)) line++, entry->indent++;

	entry->key = strtok(line, ":");
	entry->value = strtok(NULL, "\n");
	strtok(entry->key, " \t\r\n");
	if (entry->value != NULL) while (isspace(*entry->value)) entry->value++;
}

void free_indental_stack(struct indental*);
struct indental *parse_indental(char *filename) {
	FILE *file = fopen(filename, "r");
	struct indental *stack = NULL; // next is temporarily a back pointer.

	char *line = NULL;
	size_t line_len = 0;
	int lineno = 0;
	while (getline(&line, &line_len, file) != -1) {
		char *l = line;
		while (isspace(*l)) l++;
		if (*l == ';' || *l == '#' || *l == '\0') {
			lineno++;
			continue;
		}

		struct indental *entry = malloc(sizeof (struct indental));
		if (entry == NULL) {
			fprintf(stderr, "Out of memory!\n");
			free_indental_stack(stack);
			return NULL;
		}
		parse_indental_line(line, entry);
		line = NULL; line_len = 0; // Read memory claimed by `entry`.

		if (stack == NULL) stack = entry;
		else if (entry->indent > stack->indent) {
			stack->children = entry;
			entry->next = stack;
			stack = entry;
		} else {
			while (stack != NULL && entry->indent < stack->indent) {
				struct indental *parent = stack->next;
				stack->next = NULL;
				stack = parent;
			}

			if (stack == NULL || entry->indent > stack->indent) {
				fprintf(stderr, "Incorrect indent %i on %s:%i\n", entry->indent, filename, lineno);
				free_indental_stack(stack);
				return NULL;
			}

			entry->next = stack->next;
			stack->next = entry;
			stack = entry;
		}
	}

	fclose(file);
	while (stack->next != NULL) {
		struct indental *parent = stack->next;
		stack->next = NULL;
		stack = parent;
	}
	return stack;
}

void free_indental(struct indental *data) {
	if (data == NULL) return;

	free(data->txt);
	free_indental(data->next);
	free_indental(data->children);
	free(data);
}

void free_indental_stack(struct indental *stack) {
	if (stack == NULL) return;

	while (stack->next != NULL) {
		struct indental *parent = stack->next;
		stack->next = NULL;
		stack = parent;
	}
	free_indental(stack);
}

/* Trie */
struct attr {
	char *key, *value;
	struct attr *next;
};
struct trie {
	char label;
	int present : 1;
	int child_present : 1;
	struct trie *first_child;
	struct trie *next;
	struct attr *attrs;
};

struct trie init_trie() {
	struct trie ret = {
		.label = '\0',
		.present = 0,
		.child_present = 0,
		.first_child = NULL,
		.next = NULL,
		.attrs = NULL
	};
	return ret;
}
struct trie *insert_trie(struct trie *root, char *key) {
	while (root->first_child != NULL && *key != '\0') {
		root->child_present = 1;
		root = root->first_child;
		while (root->label != *key) {
			if (root->next == NULL) {
				struct trie *next = malloc(sizeof(struct trie));
				next->label = *key;
				next->present = 0;
				next->child_present = 1;
				next->next = NULL;
				next->first_child = NULL;

				root->next = next;
			}
			root = root->next;
		}
		key++;
	}

	while (*key != '\0' && !isspace(*key)) {
		struct trie *child = malloc(sizeof(struct trie));
		child->label = *key;
		child->present = 0;
		child->child_present = 1;
		child->first_child = NULL;
		child->next = NULL;

		root->first_child = child;
		root = child;
		key++;
	}
	root->present = 1;
	return root;
}
char *strclone(char *in) {
	if (in == NULL) return NULL;
	char *ret = malloc(strlen(in) + 1);
	strcpy(ret, in);
	return ret;
}
struct attr *indental_to_attr(struct indental *entry, struct trie *dest) {
	struct attr *ret = malloc(sizeof (struct attr));
	ret->key = strclone(entry->key); ret->value = strclone(entry->value);

	ret->next = dest->attrs; dest->attrs = ret;
	return ret;
}
struct trie parse_trie(char *filename) {
	struct indental *file = parse_indental(filename);
	struct trie ret = init_trie();

	for (struct indental *iter = file->children; iter != NULL; iter = iter->next) {
		if (iter->value != NULL) indental_to_attr(iter, &ret);
	}
	for (struct indental *iter = file->children; iter != NULL; iter = iter->next) {
		if (iter->value == NULL) {
			struct trie *leaf = insert_trie(&ret, iter->key);
			leaf->attrs = ret.attrs;
			for (struct indental *child = iter->children; child != NULL; child = child->next) {
				indental_to_attr(child, leaf);
			}
		}
	}
	free_indental(file);
	return ret;
}

struct trie *lookup_trie(struct trie *root, char *key) {
	if (root == NULL) return NULL;
	if (root->first_child == NULL) return NULL;
	root = root->first_child;

	while (*key != '\0') {
		while (root->label != *key) {
			if (root->next == NULL) return NULL;
			root = root->next;
		}

		key++;
		if (root->first_child == NULL) return NULL;
		root = root->first_child;
	}
	return root;
}

void free_trie(struct trie *root) {
	free_trie(root->first_child);
	free_trie(root->next);

	struct attr *attr = NULL, *next = root->attrs;
	while ((attr = next) != NULL) {
		next = attr->next;
		free(attr->key);
		free(attr->value);
		free(attr);
	}

	free(root);
}

/* Rendering */
char *get_attr(char *name, struct trie *node) {
	if (node == NULL || name == NULL) return "";

	for (struct attr *iter = node->attrs; iter != NULL; iter = iter->next) {
		if (strcmp(name, iter->key) == 0) return iter->value;
	}
	return "";
}
float get_attrf(char *name, struct trie *node) {
	float ret = 0.0;
	sscanf(get_attr(name, node), "%f", &ret);
	return ret;
}
Uint8 chan(char *name, struct trie *node) {
	return get_attrf(name, node) * 0xff;
}

void render_trie(SDL_Surface *target, struct trie *trie, int depth, SDL_Rect crop) {
	if (target == NULL || trie == NULL) return;

	SDL_Rect out = {.x = crop.x, .y = crop.y, .w = crop.w/3, .h = crop.h/3};
	for (; trie != NULL; trie = trie->next) {
		int label = trie->label - '0';
		int row = label/3, col = label%3;
		out.x = crop.x + col*out.w; out.y = crop.y + row*out.h;

		if ((depth == 0 && trie->child_present) || trie->present) {
			Uint32 colour = SDL_MapRGB(target->format, chan("red", trie), chan("green", trie), chan("blue", trie));
			SDL_FillRect(target, &out, colour);
		} else if (trie->child_present) render_trie(target, trie->first_child, depth - 1, out);
	}
}

char *strsubst(const char *s, const char *old, const char *new) {
	int oldlen = strlen(old), newlen = strlen(new);

	// count occurrences
	int cnt = 0, i;
	for (i = 0; s[i] != '\0'; i++) {
		if (strstr(&s[i], old) == &s[i]) {
			cnt++;
			i += oldlen - 1; // jump to offer old.
		}
	}

	// Allocate
	char *result = malloc(i + cnt*(newlen - oldlen) + 1);

	// Populate
	i = 0;
	while (*s) {
		if (strstr(s, old) == s) {
			strcpy(&result[i], new);
			i += newlen;
			s += oldlen;
		} else result[i++] = *s++;
	}
	result[i] = '\0';

	return result;
}

void render_base(SDL_Surface *target, char *zone, char *path, SDL_Rect crop) {
	if (target == NULL || zone == NULL || path == NULL || strlen(path) == 0) return;
	char *expanded_path = strsubst(path, "*", zone);

	SDL_Surface *background = IMG_Load(expanded_path);
	if (background == NULL) return;
	SDL_BlitSurface(background, NULL, target, &crop);

	SDL_FreeSurface(background);
	free(expanded_path);
}

/* Navigation functions */
void rotate(char *cellid, char *rotation) {
	if (rotation == NULL) return;
	for (int i = 1; cellid[i] != '\0'; i++) {
		cellid[i] = rotation[cellid[i] - '0'];
	}
}
char inverse_matrix[] =
	"876"
	"543"
	"210";
char cw_matrix[] =
	"630"
	"741"
	"852";
char ccw_matrix[] =
	"258"
	"147"
	"036";

void down(char *cellid) {
	int i = strlen(cellid);
	while (--i > 0) {
		int pos = cellid[i] - '0';
		pos += 3;
		if (pos > 8) {
			pos -= 9;
			cellid[i] = pos + '0';
		} else {
			cellid[i] = pos + '0';
			return;
		}
		i--;
	}
	if (cellid[0] == 'N') cellid[0] = 'P';
	else if (cellid[0] == 'S') {cellid[0] = 'O'; rotate(cellid, inverse_matrix);}
	else {
		cellid[0] = 'S';
		if (cellid[0] == 'O') rotate(cellid, inverse_matrix);
		else if (cellid[0] == 'P') rotate(cellid, cw_matrix);
		else if (cellid[0] == 'R') rotate(cellid, ccw_matrix);
	}
}
void up(char *cellid) {
	int i = strlen(cellid);
	while (--i > 0) {
		int pos = cellid[i] - '0';
		pos -= 3;
		if (pos < 0) {
			pos += 9;
			cellid[i] = pos + '0';
		} else {
			cellid[i] = pos + '0';
			return;
		}
	}
	if (cellid[0] == 'N') {cellid[0] = 'Q'; rotate(cellid, inverse_matrix);}
	else if (cellid[0] == 'S') cellid[0] = 'R';
	else {
		cellid[0] = 'N';
		if (cellid[0] == 'O') rotate(cellid, ccw_matrix);
		else if (cellid[0] == 'Q') rotate(cellid, cw_matrix);
		else if (cellid[0] == 'R') rotate(cellid, inverse_matrix);
	}
}

void left(char *cellid) {
	int i = strlen(cellid);
	while (--i > 0) {
		int row = (cellid[i] - '0') / 3;
		int col = (cellid[i] - '0') % 3;
		col -= 1;
		int handle_parent = col < 0;
		if (handle_parent) col += 3;
		int pos = row * 3 + col;
		cellid[i] = pos + '0';
		if (!handle_parent) return;
		i--;
	}
	if (*cellid == 'N') {*cellid = 'O'; rotate(cellid, cw_matrix);}
	else if (*cellid == 'S') {*cellid = 'P'; rotate(cellid, ccw_matrix);}
	else if (*cellid == 'O') *cellid = 'R';
	else cellid[0]--;
}

void right(char *cellid) {
	int i = strlen(cellid);
	while (--i > 0) {
		int row = (cellid[i] - '0') / 3;
		int col = (cellid[i] - '0') % 3;
		col += 1;
		int handle_parent = col > 2;
		if (handle_parent) col -= 3;
		int pos = row * 3 + col;
		cellid[i] = pos + '0';
		if (!handle_parent) return;
	}
	if (*cellid == 'N') {*cellid = 'Q'; rotate(cellid, ccw_matrix);}
	else if (*cellid == 'S') {*cellid = 'R'; rotate(cellid, cw_matrix);}
	else if (*cellid == 'R') *cellid = 'O';
	else cellid[0]++;
}

/* Main */
int WIDTH = 512, HEIGHT = 512;
int main(int argc, char **argv) {
	if (argc != 3) {
		fprintf(stderr, "USAGE: %s CELLID FILE\n", argv[0]);
		return 1;
	}

	// Construct test trie
	struct trie geom = parse_trie(argv[2]);
	char crop[201];
	if (strlen(argv[1]) >= 200) {
		printf("cellid must be less than 200 characters long!\n");
		return 1;
	}
	strncpy(crop, argv[1], sizeof(crop));
	struct trie *view = lookup_trie(&geom, crop);

	if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
		printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
		return 1;
	}
	int IMG_FLAGS = IMG_INIT_PNG;
	if (!(IMG_Init(IMG_FLAGS) & IMG_FLAGS)) {
		printf("WARNING: Failed to fully initialize image loader! Some base tiles might not be displayed.\n");
	}
	SDL_Window *window = SDL_CreateWindow("DGGS Viewer", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_SHOWN);
	if (window == NULL) {
		printf("SDL could not create window! SDL_Error: %s\n", SDL_GetError());
		return 1;
	}
	SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
	SDL_Surface *screen;
	if (renderer == NULL) {
		printf("Failed to initialize renderer, rotations will not be applied!\n");
		screen = SDL_GetWindowSurface(window);
	} else {
		screen = SDL_CreateRGBSurface(0, WIDTH, HEIGHT, 32, 0, 0, 0, 0);
	}

	// SDL_Surface *screen = SDL_GetWindowSurface(window);
	SDL_Rect rect = {0, 0, WIDTH, HEIGHT};
	SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0xff, 0xff, 0xff));
	render_base(screen, crop, get_attr("background", view != NULL ? view : &geom), rect);
	render_trie(screen, view, 100, rect);
	SDL_UpdateWindowSurface(window);

	int quit = 0;
	while (!quit) {
		SDL_Event e;
		while (SDL_PollEvent(&e) != 0) {
			if (e.type == SDL_QUIT) quit = 1;
			else if (e.type == SDL_KEYDOWN) {
				int crop_len = strlen(crop);
				if (crop_len >= 200 && e.key.keysym.sym != SDLK_KP_0) continue;
				if (crop_len == 1 && e.key.keysym.sym == SDLK_KP_0) continue;

				switch (e.key.keysym.sym) {
				case SDLK_KP_0:
					crop[crop_len-1] = '\0';
					break;
				case SDLK_KP_1:
					crop[crop_len] = '0';
					break;
				case SDLK_KP_2:
					crop[crop_len] = '1';
					break;
				case SDLK_KP_3:
					crop[crop_len] = '2';
					break;
				case SDLK_KP_4:
					crop[crop_len] = '3';
					break;
				case SDLK_KP_5:
					crop[crop_len] = '4';
					break;
				case SDLK_KP_6:
					crop[crop_len] = '5';
					break;
				case SDLK_KP_7:
					crop[crop_len] = '6';
					break;
				case SDLK_KP_8:
					crop[crop_len] = '7';
					break;
				case SDLK_KP_9:
					crop[crop_len] = '8';
					break;
				case SDLK_UP:
					up(crop);
					break;
				case SDLK_DOWN:
					down(crop);
					break;
				case SDLK_LEFT:
					left(crop);
					break;
				case SDLK_RIGHT:
					right(crop);
					break;
				default: continue;
				}
				crop[crop_len+1] = '\0';

				printf("%s\n", crop);
				view = lookup_trie(&geom, crop);
				SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0xff, 0xff, 0xff));
				render_base(screen, crop, get_attr("background", view != NULL ? view : &geom), rect);
				render_trie(screen, view, 100, rect);
				SDL_UpdateWindowSurface(window);
			}
		}
		SDL_Delay(33);
	}

	SDL_DestroyWindow(window);
	SDL_Quit();
	return 0;
}

/* How to implement rotation?

1. Rotate output image. Use SDL_RenderCopyEx
2. Use the "matrix" constants to rotate numpad input.
3. Use rotation as well as keycode to determine which buttons to use for arrow keys.
4. Use keypad . key to increment the rotation value &3. */