Files
cat_frend_help_friend_good/main.c
T
2026-04-20 13:48:11 -04:00

214 lines
4.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;
}
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;
static int jump_ticks = 0;
void key_down(char k)
{
add_keydown(k);
printf("%c pressed!\n", k);
switch (k)
{
case 'a': player_ax = -2; break;
case 'd': player_ax = 2; 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;
}
}
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
/* FIXME this is just temporary */
void in_bounds(void)
{
if (player_x < 0) player_x = 0;
if (player_y < 0) player_y = 0;
if (player_x > screen_width - 50) player_x = screen_width - 50;
if (player_y > screen_height- 50) player_y = screen_height - 50;
}
void apply_friction(void) /* FIXME */
{
if (player_vx > 0) player_vx -= 1;
else if (player_vx < 0) player_vx += 1;
if (player_vy > 0) player_vy -= 1;
else if (player_vy < 0) player_vy += 1;
}
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();
if (is_held(' ')) player_ay = -5;
else player_ay = 9.81;
player_vx += player_ax;
player_vy += player_ay;
apply_friction(); /* FIXME */
player_x += (int)player_vx;
player_y += (int)player_vy;
in_bounds();
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;
}