xlib_playground

Xlib playground for experiments.
Log | Files | Refs

commit 9d68312d4f3078785d1db73202b955289acf4ff9
parent 92deabc9b78c9b53f79907ef5330d8a588de928f
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Tue, 10 Jan 2023 08:00:46 +0900

use list

Diffstat:
Mex9/main.c | 71++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
Mex9/main.h | 1+
2 files changed, 51 insertions(+), 21 deletions(-)

diff --git a/ex9/main.c b/ex9/main.c @@ -6,6 +6,7 @@ #include <string.h> #include "main.h" +#include "list.h" #include "world_map.h" /* @@ -63,6 +64,8 @@ void handle_inputs(enum key_state[]); void col_pf(struct Object *, struct Object *); // player-flag void col_pb(struct Object *, struct Object *); // player-block +void sort_list_dist(struct List *, struct Object *); + void start_menu(void) { @@ -125,12 +128,11 @@ game_play(void) int fps_count = 0; #endif struct timespec ts; - struct OL *bl[WORLD_WIDTH]; // block list header - struct OL *fl; // flag - struct OL *olp; + struct List *bl[WORLD_WIDTH]; // block list header + struct List *fl; // flag + struct List *olp; int scroll_dst = 0; - for (int xi = 0; xi < WORLD_WIDTH; xi++) bl[xi] = NULL; fl = NULL; @@ -141,17 +143,21 @@ game_play(void) col_func[TPLAYER][TFLAG] = col_func[TFLAG][TPLAYER] = &col_pf; col_func[TPLAYER][TBLOCK] = col_func[TFLAG][TPLAYER] = &col_pb; - for (int i = 0; i < WORLD_WIDTH * WORLD_HEIGHT; i++) { if (world_map[i] == 'b') { int xi = i % WORLD_WIDTH; // TODO: don't forget to free these things. - bl[xi] = addfront_ol(bl[xi], create_object(TBLOCK, 0x00FF00, + olp = laddfront(bl[xi], create_object(TBLOCK, 0x00FF00, i % WORLD_WIDTH * BLOCK_SIZE, i / WORLD_WIDTH * BLOCK_SIZE, 0, 0, 0, 0, BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE * BLOCK_SIZE)); + if (olp == NULL) { + fprintf(stderr, "error: adding item to list bl\n"); + } else { + bl[xi] = olp; + } } else if (world_map[i] == 'p') { player = create_object(TPLAYER, 0x0000FF, i % WORLD_WIDTH * BLOCK_SIZE, @@ -159,13 +165,22 @@ game_play(void) 0, 0, 0, GRAVITY, BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE * BLOCK_SIZE); + if (player == NULL) { + fprintf(stderr, "error: creating player\n"); + exit(1); + } } else if (world_map[i] == 'f') { - fl = addfront_ol(fl, create_object(TFLAG, 0xFFFF00, + olp = laddfront(fl, create_object(TFLAG, 0xFFFF00, i % WORLD_WIDTH * BLOCK_SIZE, i / WORLD_WIDTH * BLOCK_SIZE, 0, 0, 0, 0, BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE * BLOCK_SIZE)); + if (olp == NULL) { + fprintf(stderr, "error: adding item to list fl\n"); + } else { + fl = olp; + } } } @@ -207,7 +222,7 @@ game_play(void) scroll_dst = 0; } - struct OL *collidings = NULL; + struct List *collidings = NULL; player_is_falling = 1; for (int xi = (int) player->p.x / BLOCK_SIZE; @@ -215,24 +230,24 @@ game_play(void) xi < WORLD_WIDTH; xi++) { for (olp = bl[xi]; olp != NULL; olp = olp->next) { - if (is_on_floor_before(player, olp->o)) { + if (is_on_floor_before(player, (struct Object *)olp->item)) { player_is_falling = 0; } - if (test_collision(player, olp->o)) - collidings = addfront_ol(collidings, olp->o); + if (test_collision(player, (struct Object *)olp->item)) + collidings = laddfront(collidings, olp->item); } } if (collidings != NULL) { - sort_ol(collidings, player); + sort_list_dist(collidings, player); for (olp = collidings; olp != NULL; olp = olp->next) { - handle_collision(player, olp->o); + handle_collision(player, (struct Object *)olp->item); } } - free_ol(collidings); + lfree(collidings); // collision aganst the goal flag for (olp = fl; olp != NULL; olp = olp->next){ - handle_collision(player, olp->o); + handle_collision(player, (struct Object *)olp->item); } } @@ -261,15 +276,15 @@ game_play(void) xi < (WIN_WIDTH + scroll_dst) / BLOCK_SIZE + 1 && xi < WORLD_WIDTH; xi++) { for (olp = bl[xi]; olp != NULL; olp = olp->next) { - draw_object_scroll(olp->o, scroll_dst); + draw_object_scroll((struct Object *)olp->item, scroll_dst); } } draw_object_scroll(player, scroll_dst); for (olp = fl; olp != NULL; olp = olp->next) { - if (0 <= olp->o->p.x - scroll_dst && - olp->o->p.x - scroll_dst < WIN_WIDTH) { - draw_object_scroll(olp->o, scroll_dst); + if (0 <= ((struct Object *)olp->item)->p.x - scroll_dst && + ((struct Object *)olp->item)->p.x - scroll_dst < WIN_WIDTH) { + draw_object_scroll((struct Object *)olp->item, scroll_dst); } else { break; } @@ -277,8 +292,8 @@ game_play(void) } for (int xi = 0; xi < WORLD_WIDTH; xi++) - free_obj_and_ol(bl[xi]); - free_obj_and_ol(fl); + lfreei(bl[xi], (void (*)(void *))&free_object); + lfreei(fl, (void (*)(void *))&free_object); } void @@ -435,6 +450,20 @@ void col_pb(struct Object *op, struct Object *ob) { handle_collision_mf(op, ob); } + +void +sort_list_dist(struct List *lp, struct Object *o) +{ + struct List *p, *q; + for (p = lp; p != NULL && p->next != NULL; p = p->next) { + for (q = p->next; q != NULL; q = q->next) { + if (object_dist((struct Object *)q->item, o) < + object_dist((struct Object *)p->item, o)) + lswap(p, q); + } + } +} + int main(void) { diff --git a/ex9/main.h b/ex9/main.h @@ -74,6 +74,7 @@ int object_is_falling(struct Object *o, struct Object *fb, int num_f); struct Object *create_object(enum object_type, uint32_t, float, float, float, float, float, float, int, int, int); +void free_object(struct Object *); int is_on_floor_before(struct Object *, struct Object *);