From f6da9f36c65231557e7a9f05a870261116a4f5b1 Mon Sep 17 00:00:00 2001 From: katherine Date: Wed, 10 Feb 2021 07:46:58 -0700 Subject: random walk toy --- README.md | 1 + rwalk/Makefile | 29 ++++++++++ rwalk/src/main.c | 165 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 195 insertions(+) create mode 100644 README.md create mode 100644 rwalk/Makefile create mode 100644 rwalk/src/main.c diff --git a/README.md b/README.md new file mode 100644 index 0000000..bcf5efc --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +allegro graphics toys diff --git a/rwalk/Makefile b/rwalk/Makefile new file mode 100644 index 0000000..da68641 --- /dev/null +++ b/rwalk/Makefile @@ -0,0 +1,29 @@ +CFLAGS+=-Wall -O2 $(shell pkg-config --cflags allegro-5 allegro_primitives-5 libsodium) +CFLAGSDEBUG=-Wall -ggdb3 -O0 -DDEBUG $(shell pkg-config --cflags allegro-5 allegro_primitives-5 libsodium) +LDFLAGS+=-Wall -O2 $(shell pkg-config --libs allegro-5 allegro_primitives-5 libsodium) +LDFLAGSDEBUG=-Wall -ggdb3 -O0 -DDEBUG $(shell pkg-config --libs allegro-5 allegro_primitives-5 libsodium) +SRCDIR=./src +OBJDIR=./build +SRC=$(wildcard $(SRCDIR)/*.c) +OBJ=$(patsubst $(SRCDIR)%.c,$(OBJDIR)%.o,$(SRC)) +BIN=rwalk + +all: $(OBJ) + $(CC) $(LDFLAGS) -o $(BIN) $^ + +$(OBJ): | $(OBJDIR) + +$(OBJDIR)/%.o: $(SRCDIR)/%.c + $(CC) $(CFLAGS) -c -o $@ $< + +$(OBJDIR): + mkdir -p $(OBJDIR) + +clean: + rm -rf $(OBJDIR) $(BIN) + +debug: CFLAGS=$(CFLAGSDEBUG) +debug: LDFLAGS=$(LDFLAGSDEBUG) +debug: all + +new: clean all 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 +#include +#include + +#include +#include + +#include + +#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; +} -- cgit v1.2.3