#include "cat_api.h" #include #define NS_PER_TICK 30000000 void bye(void) { printf("bye!\n"); exit(0); } 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); } static long last_sec = 0; static long last_nsec = 0; 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; yes: last_sec = t.tv_sec; last_nsec = t.tv_nsec; } void game_loop(void) { process_input(); tick(); cat_render(); } int main(int argc, char** argv) { cat_init(); cat_create_window(1920, 1080); cat_fill(0, 1, 0, 0, screen_width - 1, screen_height - 1, 132, 155, 132); while (1) game_loop(); return 0; }