From 82bc592d50c9d4c80cb96a4f3c91ce275a96f433 Mon Sep 17 00:00:00 2001 From: Tobin Date: Fri, 17 Apr 2026 17:54:42 -0400 Subject: [PATCH] basic key handling --- main.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 82 insertions(+), 7 deletions(-) diff --git a/main.c b/main.c index 3420927..8af5576 100644 --- a/main.c +++ b/main.c @@ -1,29 +1,102 @@ #include "cat_api.h" #include -#define NS_PER_TICK 30000000 - void bye(void) { printf("bye!\n"); exit(0); } +struct keydowns +{ + char k; + struct keydowns* next; +}; + +struct keydowns* first_keydown = NULL; + +void add_keydown(char k) +{ + struct keydowns* new = malloc(sizeof(struct keydowns)); + new->k = k; + new->next = NULL; + if (first_keydown == NULL) first_keydown = new; + else + { + struct keydowns* head; + for (head = first_keydown; head->next != NULL; head = head->next) + if (head->k == k) return; + head->next = new; + } +} + +void lit_remove_keydown(struct keydowns* entry) +{ + if (first_keydown == entry) first_keydown = entry->next; + else + { + struct keydowns* head; + for (head = first_keydown; head->next != entry; head = head->next) ; + head->next = entry->next; + } +} + +void remove_keydown(char k) +{ + struct keydowns* head; + for (head = first_keydown; head != NULL; head = head->next) + if (head->k == k) lit_remove_keydown(head); +} + +int is_held(char k) +{ + struct keydowns* head; + for (head = first_keydown; head != NULL; head = head->next) + if (head->k == k) return 1; + return 0; +} + +void key_down(char k) +{ + add_keydown(k); + printf("%c pressed!\n", k); +} + +void key_up(char k) +{ + remove_keydown(k); + printf("%c un-pressed\n", k); +} + +void mouse_move(int x, int y) +{ +} + +void mouse_down(unsigned char button) +{ +} + +void mouse_up(unsigned char button) +{ +} + void process_input(void) { struct cat_event* e = cat_get_event(); if (e == NULL) return; if (e->type == QUIT) bye(); - else if (e->type == KEYDOWN) printf("keypress! %c\n", e->key); - else if (e->type == KEYUP) printf("key up! %c\n", e->key); - else if (e->type == MOUSEMOVE) printf("mouse move! (%d, %d)\n", e->mousex, e->mousey); - else if (e->type == MOUSEDOWN) printf("mouse down! %02x\n", e->mouse_button); - else if (e->type == MOUSEUP) printf("mouse up! %02x\n", e->mouse_button); + else if (e->type == KEYDOWN) key_down(e->key); + else if (e->type == KEYUP) key_up(e->key); + else if (e->type == MOUSEMOVE) mouse_move(e->mousex, e->mousey); + else if (e->type == MOUSEDOWN) mouse_down(e->mouse_button); + else if (e->type == MOUSEUP) mouse_up(e->mouse_button); } static long last_sec = 0; static long last_nsec = 0; +#define NS_PER_TICK 30000000 + void tick(void) { /* run every N milliseconds... nanoseconds? idk */ @@ -35,6 +108,8 @@ void tick(void) else return; yes: last_sec = t.tv_sec; last_nsec = t.tv_nsec; + + /* code to run every tick goes here */ } void game_loop(void)