#include "cat_api.h" #include 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; } static int player_x = 100; static int player_y = 100; static float player_vx = 0.0; static float player_vy = 0.0; static float player_ax = 0.0; static float player_ay = 9.81; void key_down(char k) { add_keydown(k); printf("%c pressed!\n", k); switch (k) { case 'a': player_ax = -1; break; case 'd': player_ax = 1; break; case 0x20: player_ay -= 1; break; } } void key_up(char k) { remove_keydown(k); printf("%c un-pressed\n", k); switch (k) { case 'a': player_ax = 0; break; case 'd': player_ax = 0; break; case 0x20: player_ay += 1; break; } } static int mousex = 0; static int mousey = 0; void mouse_move(int x, int y) { /* clear old mouse icn */ mousex = x; mousey = y; printf("mouse moved! (%d, %d)\n", x, y); } void mouse_down(unsigned char button) { printf("mouse button %02x clicked!\n", button); /* TODO struct for this too?? that seems like a lot. what if we *don't* use mouse for anything... */ } void mouse_up(unsigned char button) { printf("mouse button %02x un-clicked!\n", 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) 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); } void erase_entity(/*struct entity*/) { /* get sprite info, etc */ /* FIXME */ cat_fill(1, 0, player_x, player_y, 50, 50, 0, 0, 0); } void draw_entity(/* struct entity */) { /* get sprite info, etc */ /* FIXME */ cat_fill(1, 1, player_x, player_y, 50, 50, 115, 72, 6); } void erase_entities(void) { /* TODO more than just player entity */ erase_entity(/*player*/); } void draw_entities(void) { /* TODO more than just player entity */ draw_entity(/*player*/); } static long last_sec = 0; static long last_nsec = 0; #define NS_PER_TICK 30000000 void tick(void) { /* run every N milliseconds... nanoseconds? idk */ struct timespec t; clock_gettime(CLOCK_REALTIME, &t); if (last_sec == 0) goto yes; else if ((t.tv_sec - last_sec) > 0) goto yes; else if ((t.tv_nsec - last_nsec) > NS_PER_TICK) goto yes; else return; /* do a tick */ yes: last_sec = t.tv_sec; last_nsec = t.tv_nsec; /* code to run every tick goes here */ erase_entities(); player_vx += player_ax; player_vy += player_ay; player_x += (int)player_vx; player_y += (int)player_vy; draw_entities(); } void game_loop(void) { cat_render(); process_input(); tick(); } int main(int argc, char** argv) { cat_init(); cat_create_window(1920, 1080); cat_play_wav("etc/example.wav"); /* layer, visibility, x, y, width, height, r, g, b */ cat_fill(0, 1, 0, 0, screen_width - 1, screen_height - 1, 132, 155, 132); while (1) game_loop(); return 0; }