From 51ba9e5494cb848cd29fa174c0dcea4a5ac96eef Mon Sep 17 00:00:00 2001 From: katherine Date: Thu, 11 Feb 2021 00:41:56 -0700 Subject: wolfrand --- 01-rwalk/Makefile | 29 +++++++++ 01-rwalk/screenshot.png | Bin 0 -> 1188 bytes 01-rwalk/src/main.c | 165 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 194 insertions(+) create mode 100644 01-rwalk/Makefile create mode 100644 01-rwalk/screenshot.png create mode 100644 01-rwalk/src/main.c (limited to '01-rwalk') diff --git a/01-rwalk/Makefile b/01-rwalk/Makefile new file mode 100644 index 0000000..da68641 --- /dev/null +++ b/01-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/01-rwalk/screenshot.png b/01-rwalk/screenshot.png new file mode 100644 index 0000000..a169b02 Binary files /dev/null and b/01-rwalk/screenshot.png differ diff --git a/01-rwalk/src/main.c b/01-rwalk/src/main.c new file mode 100644 index 0000000..28592e4 --- /dev/null +++ b/01-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