aboutsummaryrefslogtreecommitdiffstats
path: root/05-brain/src/main.c
blob: dfa2d2ca146cfad1ee7dcb87959864c7aba7ad95 (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
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

#include <allegro5/allegro5.h>
#include <allegro5/allegro_primitives.h>

#include <sodium.h>

#define BUF_W 256
#define BUF_H 256

#define DISP_SCALE 3

#define SEED_BITS 4096

#define FPS 30.0

#define DO_INIT(check, desc) \
	if (!(check)) { \
		fprintf(stderr, "err: can't initialise %s\n", desc); \
		rval = 1; \
		goto done; \
	}

int main(int argc, char **argv)
{
	int rval = 0;

	uint8_t bits1[BUF_H][BUF_W] = {0};
	uint8_t bits2[BUF_H][BUF_W] = {0};

	uint8_t framerate = 8;
	unsigned goframes = 1;
	unsigned x, y, sum;

	bool pause = true;
	bool mouse_down = false;

	ALLEGRO_MOUSE_STATE ms;

	ALLEGRO_DISPLAY *d = NULL;
	ALLEGRO_BITMAP *b = NULL;
	ALLEGRO_TIMER *t = NULL;
	ALLEGRO_EVENT_QUEUE *eq = NULL;
	ALLEGRO_EVENT e;


	DO_INIT(sodium_init() >= 0, "sodium");

	DO_INIT(al_init(), "allegro");
	DO_INIT(al_init_primitives_addon(), "primitives");
	DO_INIT(al_install_keyboard(), "keyboard");
	DO_INIT(al_install_mouse(), "mouse");

	DO_INIT(t = al_create_timer(1.0 / FPS), "timer");
	DO_INIT(eq = al_create_event_queue(), "event queue");

	al_set_new_display_option(ALLEGRO_SINGLE_BUFFER, 1, ALLEGRO_REQUIRE);
	DO_INIT(d = al_create_display(
			DISP_SCALE * BUF_W, DISP_SCALE * BUF_H
			), "display"
	);
	al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
	DO_INIT(b = al_create_bitmap(BUF_W, BUF_H), "buffer");

	al_register_event_source(eq, al_get_keyboard_event_source());
	al_register_event_source(eq, al_get_mouse_event_source());
	al_register_event_source(eq, al_get_display_event_source(d));
	al_register_event_source(eq, al_get_timer_event_source(t));


	al_start_timer(t);

reset:

	memset(bits1, 0, sizeof(bits1));

	while(true) {
		al_wait_for_event(eq, &e);

		switch (e.type) {

		case ALLEGRO_EVENT_DISPLAY_CLOSE:
			goto done;

		case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
			bits1[e.mouse.y / DISP_SCALE][e.mouse.x / DISP_SCALE] = 1;
			mouse_down = true;
			break;

		case ALLEGRO_EVENT_MOUSE_BUTTON_UP:
			mouse_down = false;
			break;

		case ALLEGRO_EVENT_KEY_DOWN:
			switch(e.keyboard.keycode) {

			case ALLEGRO_KEY_Q:
				goto done;

			case ALLEGRO_KEY_1:
				framerate = (framerate == 1 ? 16 : framerate / 2);
				printf("fps: %.1f\n", FPS / ((float)framerate) );
				break;

			case ALLEGRO_KEY_2:
				memset(bits1, 0, sizeof(bits1));
				for (x = 0; x < SEED_BITS; x++) {
					bits1[randombytes_random() % BUF_H][randombytes_random() % BUF_W] = 1;
				}
				break;

			case ALLEGRO_KEY_3:
				goto reset;

			case ALLEGRO_KEY_ENTER:
				pause = !pause;
				printf("%s\n", pause ? "pause" : "play");
				break;

			default:
				break;

			}

			break;

		case ALLEGRO_EVENT_TIMER:

			if (mouse_down) {
				al_get_mouse_state(&ms);
				bits1[ms.y / DISP_SCALE][ms.x / DISP_SCALE] = 1;
			}

			/* step */

			if (goframes) {
				goframes--;
			} else if (!pause) {
				for (y = 0; y < BUF_H; y++) {
					for (x = 0; x < BUF_W; x++) {
						if (bits1[y][x] == 2) {
							bits2[y][x] = 0;
							continue;
						}

						if (bits1[y][x] == 1) {
							bits2[y][x] = 2;
							continue;
						}

						sum =
							  (1 == bits1[(y-1) % BUF_H][(x-1) % BUF_W])
							+ (1 == bits1[(y-1) % BUF_H][ x           ])
							+ (1 == bits1[(y-1) % BUF_H][(x+1) % BUF_W])
							+ (1 == bits1[ y           ][(x-1) % BUF_W])
							+ (1 == bits1[ y           ][(x+1) % BUF_W])
							+ (1 == bits1[(y+1) % BUF_H][(x-1) % BUF_W])
							+ (1 == bits1[(y+1) % BUF_H][ x           ])
							+ (1 == bits1[(y+1) % BUF_H][(x+1) % BUF_W])
						;

						if (sum == 2)
							bits2[y][x] = 1;
					}
				}

				memcpy(bits1, bits2, sizeof(bits1));
				goframes = framerate;
			}

			/* draw frame */

			al_lock_bitmap(b, ALLEGRO_PIXEL_FORMAT_ANY, ALLEGRO_LOCK_WRITEONLY);

			al_set_target_bitmap(b);

			for (y = 0; y < BUF_H; y++) {
				for (x = 0; x < BUF_W; x++) {
					switch (bits1[y][x]) {

					case 0:
						al_put_pixel(x, y, al_map_rgb(0, 0, 0));
						break;

					case 1:
						al_put_pixel(x, y, al_map_rgb(255, 255, 255));
						break;

					case 2:
						al_put_pixel(x, y, al_map_rgb(255, 0, 0));
						break;

					}
				}
			}

			al_unlock_bitmap(b);

			al_set_target_backbuffer(d);
			al_draw_scaled_bitmap(b,
				0, 0, BUF_W, BUF_H,
				0, 0, DISP_SCALE * BUF_W, DISP_SCALE * BUF_H,
				0
			);
			al_flip_display();

		}
	}


done:

	if (d)
		al_destroy_display(d);
	if (b)
		al_destroy_bitmap(b);
	if (t)
		al_destroy_timer(t);
	if (eq)
		al_destroy_event_queue(eq);
	
	return rval;
}