Files
cat_frend_help_friend_good/main.c
T
2026-04-17 17:54:42 -04:00

130 lines
2.4 KiB
C

#include "cat_api.h"
#include <time.h>
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) 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 */
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;
/* code to run every tick goes here */
}
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;
}