diff options
Diffstat (limited to '04-conway')
-rw-r--r-- | 04-conway/screenshot.png | bin | 1658 -> 2183 bytes | |||
-rw-r--r-- | 04-conway/src/main.c | 69 |
2 files changed, 51 insertions, 18 deletions
diff --git a/04-conway/screenshot.png b/04-conway/screenshot.png Binary files differindex f262070..ccef355 100644 --- a/04-conway/screenshot.png +++ b/04-conway/screenshot.png diff --git a/04-conway/src/main.c b/04-conway/src/main.c index fd591d9..75ed338 100644 --- a/04-conway/src/main.c +++ b/04-conway/src/main.c @@ -8,12 +8,14 @@ #include <sodium.h> -#define BUF_W 512 -#define BUF_H 512 +#define BUF_W 256 +#define BUF_H 256 + +#define DISP_SCALE 3 #define SEED_BITS 4096 -#define FPS 20.0 +#define FPS 60.0 #define DO_INIT(check, desc) \ if (!(check)) { \ @@ -39,11 +41,14 @@ int main(int argc, char **argv) uint8_t bits1[BUF_H][BUF_W / 8] = {0}; uint8_t bits2[BUF_H][BUF_W / 8] = {0}; - uint8_t framerate = 2; + uint8_t framerate = 8; unsigned goframes = 1; unsigned x, y, sum; - bool pause = 1; + bool pause = true; + bool mouse_down = false; + + ALLEGRO_MOUSE_STATE ms; ALLEGRO_DISPLAY *d = NULL; ALLEGRO_BITMAP *b = NULL; @@ -57,16 +62,21 @@ int main(int argc, char **argv) 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(2 * BUF_W, 2 * BUF_H), "display"); + 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)); @@ -77,13 +87,6 @@ reset: memset(bits1, 0, sizeof(bits1)); - for (x = 0; x < SEED_BITS; x++) { - bit_set( - bits1[randombytes_random() % BUF_H], - randombytes_random() % BUF_W - ); - } - while(true) { al_wait_for_event(eq, &e); @@ -92,18 +95,39 @@ reset: case ALLEGRO_EVENT_DISPLAY_CLOSE: goto done; + case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: + bit_set(bits1[e.mouse.y / DISP_SCALE], e.mouse.x / DISP_SCALE); + 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: - puts("reset"); - goto reset; + framerate = (framerate == 2 ? 16 : framerate / 2); + printf("fps: %.1f\n", FPS / ((float)framerate) ); + break; case ALLEGRO_KEY_2: - framerate = (framerate == 1 ? 8 : framerate / 2); - printf("fps: %.1f\n", FPS / ((float)framerate) ); + memset(bits1, 0, sizeof(bits1)); + for (x = 0; x < SEED_BITS; x++) { + bit_set( + bits1[randombytes_random() % BUF_H], + randombytes_random() % BUF_W + ); + } break; + case ALLEGRO_KEY_3: + goto reset; + case ALLEGRO_KEY_ENTER: pause = !pause; printf("%s\n", pause ? "pause" : "play"); @@ -118,6 +142,11 @@ reset: case ALLEGRO_EVENT_TIMER: + if (mouse_down) { + al_get_mouse_state(&ms); + bit_set(bits1[ms.y / 2], ms.x / 2); + } + if (goframes > 1) { goframes--; break; @@ -167,7 +196,11 @@ reset: al_unlock_bitmap(b); al_set_target_backbuffer(d); - al_draw_scaled_bitmap(b, 0, 0, BUF_W, BUF_H, 0, 0, 2 * BUF_W, 2 * BUF_H, 0); + 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(); goframes = framerate; |