From 8fa69f4f98042ad425d72fa2b1898667a3136018 Mon Sep 17 00:00:00 2001 From: Tobin Date: Tue, 21 Apr 2026 15:24:33 -0400 Subject: [PATCH] finish doing that --- main.c | 53 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/main.c b/main.c index d787ab0..0bb6249 100644 --- a/main.c +++ b/main.c @@ -64,7 +64,9 @@ struct entity int y; int width; int height; - unsigned int color; /* replace this with sprite TODO */ + unsigned char r; /* replace this with sprite TODO */ + unsigned char g; + unsigned char b; float vx; float vy; float ax; @@ -74,8 +76,8 @@ struct entity struct entity* first_entity = NULL; -void add_entity(int is_player, int is_static, int x, int y, int width, int height, unsigned int color, - float vx, float vy, float ax, float ay) +void add_entity(int is_player, int is_static, int x, int y, int width, int height, unsigned char r, + unsigned char g, unsigned char b, float vx, float vy, float ax, float ay) { struct entity* new = malloc(sizeof(struct entity)); new->is_player = is_player; @@ -84,7 +86,9 @@ void add_entity(int is_player, int is_static, int x, int y, int width, int heigh new->y = y; new->width = width; new->height = height; - new->color = color; + new->r = r; + new->g = g; + new->b = b; new->vx = vx; new->vy = vy; new->ax = ax; @@ -124,8 +128,8 @@ void key_up(char k) printf("%c un-pressed\n", k); switch (k) { - case 'a': player_ax = 0; break; - case 'd': player_ax = 0; break; + case 'a': get_player()->ax = 0; break; + case 'd': get_player()->ax = 0; break; } } @@ -163,31 +167,33 @@ void process_input(void) else if (e->type == MOUSEUP) mouse_up(e->mouse_button); } -void erase_entity(/*struct entity*/) +void erase_entity(struct entity* e) { /* get sprite info, etc */ /* FIXME */ - cat_fill(1, 0, player_x, player_y, 50, 50, 0, 0, 0); + if (e->x < 0 || e->y < 0 || e->x >= screen_width || e->y >= screen_height) return; + cat_fill(1, 0, e->x, e->y, e->width, e->height, 0, 0, 0); } -void draw_entity(/* struct entity */) +void draw_entity(struct entity* e) { /* get sprite info, etc */ /* FIXME */ - cat_fill(1, 1, player_x, player_y, 50, 50, 115, 72, 6); + if (e->x < 0 || e->y < 0 || e->x >= screen_width || e->y >= screen_height) return; + cat_fill(1, 1, e->x, e->y, e->width, e->height, e->r, e->g, e->b); /* if out of bounds do not draw */ } void erase_entities(void) { /* TODO more than just player entity */ - erase_entity(/*player*/); + erase_entity(get_player()); } void draw_entities(void) { /* TODO more than just player entity */ - draw_entity(/*player*/); + draw_entity(get_player()); } static long last_sec = 0; @@ -197,10 +203,11 @@ static long last_nsec = 0; void apply_friction(void) /* FIXME */ { - if (get_player()->vx > 0) get_player()->vx -= 1; - else if (get_player()->vx < 0) get_player()->vx += 1; - if (get_player()->vy > 0) get_player()->vy -= 1; - else if (get_player()->vy < 0) get_player()->vy += 1; + struct entity* p = get_player(); + if (p->vx > 0) p->vx -= 1; + else if (p->vx < 0) p->vx += 1; + if (p->vy > 0) p->vy -= 1; + else if (p->vy < 0) p->vy += 1; } void tick(void) @@ -218,13 +225,14 @@ void tick(void) /* code to run every tick goes here */ erase_entities(); - if (is_held(' ')) get_player()->ay = -5; - else get_player()->ay = 9.81; - get_player()->vx += get_player()->ax; - get_player()->vy += get_player()->ay; + struct entity* p = get_player(); + if (is_held(' ')) p->ay = -5; + else p->ay = 9.81; + p->vx += p->ax; + p->vy += p->ay; apply_friction(); /* FIXME */ - get_player()->x += (int)get_player()->vx; - get_player()->y += (int)get_player()->vy; + p->x += (int)p->vx; + p->y += (int)p->vy; draw_entities(); } @@ -242,6 +250,7 @@ int main(int argc, char** argv) 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); + /* create the player */ add_entity(1, 0, 0, 0, 50, 50, 100, 50, 0, 0, 0, 0, 0); while (1) game_loop(); return 0; }