diff options
Diffstat (limited to 'rwalk/src')
-rw-r--r-- | rwalk/src/main.c | 165 |
1 files changed, 165 insertions, 0 deletions
diff --git a/rwalk/src/main.c b/rwalk/src/main.c new file mode 100644 index 0000000..28592e4 --- /dev/null +++ b/rwalk/src/main.c @@ -0,0 +1,165 @@ +#include <stdlib.h> +#include <stdbool.h> +#include <stdio.h> + +#include <allegro5/allegro5.h> +#include <allegro5/allegro_primitives.h> + +#include <sodium.h> + +#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; + unsigned goframes; + uint32_t randbytes; + + struct { + uint32_t middle; + uint32_t upper; + uint32_t lower; + } biases[4] = { + /* unbiased */ + { + .middle = 0x80000000, + .upper = 0xc0000000, + .lower = 0x40000000, + }, + /* 1/64 */ + { + .middle = 0x7c000000, + .upper = 0xbe000000, + .lower = 0x3e000000, + }, + /* 1/32 */ + { + .middle = 0x78000000, + .upper = 0xbc000000, + .lower = 0x3c000000, + }, + /* 1/16 */ + { + .middle = 0x70000000, + .upper = 0xb8000000, + .lower = 0x38000000, + } + }; + unsigned setting = 0; + + ALLEGRO_DISPLAY* d = NULL; + ALLEGRO_TIMER* t = NULL; + ALLEGRO_EVENT_QUEUE* eq = NULL; + ALLEGRO_EVENT e; + + float x = 320.0, y = 240.0; + + + 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(t = al_create_timer(1.0 / 30.0), "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(641, 481), "display"); + + al_register_event_source(eq, al_get_keyboard_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_clear_to_color(al_map_rgb(0, 0, 0)); + + + goframes = 0; + al_start_timer(t); + while(true) { + al_wait_for_event(eq, &e); + + switch (e.type) { + + case ALLEGRO_EVENT_DISPLAY_CLOSE: + goto done; + + case ALLEGRO_EVENT_KEY_DOWN: + switch(e.keyboard.keycode) { + + case ALLEGRO_KEY_1: + setting = 0; + puts("1"); + break; + + case ALLEGRO_KEY_2: + setting = 1; + puts("2"); + break; + + case ALLEGRO_KEY_3: + setting = 2; + puts("3"); + break; + + case ALLEGRO_KEY_4: + setting = 3; + puts("4"); + break; + + default: + break; + + } + + if (! (e.keyboard.keycode == ALLEGRO_KEY_ENTER) ) + continue; + + goframes = 1000; + case ALLEGRO_EVENT_TIMER: + if (goframes == 0) { + al_flip_display(); + break; + } + + al_draw_filled_rectangle(x, y, x + 1.0, y + 1.0, al_map_rgb(255, 255, 255)); + + randbytes = randombytes_random(); + + if (randbytes >= biases[setting].middle) { + if (randbytes >= biases[setting].upper) + x += 1; + else + y += 1; + } else { + if (randbytes >= biases[setting].lower) + x -= 1; + else + y -= 1; + } + + al_flip_display(); + + goframes--; + if (goframes == 0) + puts("next"); + } + } + + +done: + + if (d) + al_destroy_display(d); + if (t) + al_destroy_timer(t); + if (eq) + al_destroy_event_queue(eq); + + return rval; +} |