xlib_playground

Xlib playground for experiments.
Log | Files | Refs

commit 33fb14b033746c208b9acdc695ebc6722715f6e8
parent 5db2e8fc7018f80a3ddb3a3aef6aeb6f83e080fd
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Sat, 31 Dec 2022 16:53:24 +0900

use linked list util

Diffstat:
Mex8/main.c | 196+++++++++++++++++++++++--------------------------------------------------------
Mex8/util.c | 6++++++
2 files changed, 62 insertions(+), 140 deletions(-)

diff --git a/ex8/main.c b/ex8/main.c @@ -38,8 +38,7 @@ enum next_menu { }; /* variables */ -struct Object block[NUM_BLOCK]; -struct Object player; +struct Object *player; int player_is_falling; enum next_menu next_menu = START_MENU; @@ -117,31 +116,23 @@ game_play(void) int fps_count = 0; #endif struct timespec ts; - struct OL *blh[WORLD_WIDTH] = {0}; // block list header - struct OL *blc[WORLD_WIDTH] = {0}; // block list cursor - int bi[WORLD_WIDTH] = {0}; + struct OH *blh[WORLD_WIDTH] = {0}; // block list header + struct OL *blc; + + for (int xi = 0; xi < WORLD_WIDTH; xi++) + blh[xi] = create_ol(); for (int i = 0; i < WORLD_WIDTH * WORLD_HEIGHT; i++) { if (world_map[i] == 'b') { int xi = i % WORLD_WIDTH; - if (bi[xi] == 0) { - blh[xi] = (struct OL *) malloc(sizeof(struct OL)); - blc[xi] = blh[xi]; - } else { - blc[xi]->next = - (struct OL *)malloc(sizeof(struct OL)); - blc[xi] = blc[xi]->next; - } // TODO: don't forget to free these things. - blc[xi]->o = create_object(i % WORLD_WIDTH * BLOCK_SIZE, + append_ol(blh[xi], create_object(i % WORLD_WIDTH * BLOCK_SIZE, i / WORLD_WIDTH * BLOCK_SIZE, 0, 0, 0, 0, BLOCK_SIZE, BLOCK_SIZE, - BLOCK_SIZE * BLOCK_SIZE); - blc[xi]->next = NULL; - bi[xi]++; + BLOCK_SIZE * BLOCK_SIZE)); } else if (world_map[i] == 'p') { - player = *create_object(i % WORLD_WIDTH * BLOCK_SIZE, + player = create_object(i % WORLD_WIDTH * BLOCK_SIZE, i / WORLD_WIDTH * BLOCK_SIZE, 0, 0, 0, GRAVITY, BLOCK_SIZE, BLOCK_SIZE, @@ -155,59 +146,47 @@ game_play(void) receive_events(key_state); handle_inputs(key_state); - clock_gettime(CLOCK_MONOTONIC, &ts); t0 = ts.tv_nsec; for (int k = 0; k < SUB_TICK; k++) { - next_tick(&player, 1e9 / FPS / SUB_TICK); + next_tick(player, 1e9 / FPS / SUB_TICK); // game over when fall out of the screen - if (player.p.y > WORLD_HEIGHT * BLOCK_SIZE) { + if (player->p.y > WORLD_HEIGHT * BLOCK_SIZE) { next_menu = GAME_OVER; break; } // bind within the world - if (player.p.x < 0) { - player.p.x = 0; + if (player->p.x < 0) { + player->p.x = 0; } - if (WORLD_WIDTH * BLOCK_SIZE < player.p.x + - player.body.rectangle.w) { - player.p.x = WORLD_WIDTH * BLOCK_SIZE - - player.body.rectangle.w; + if (WORLD_WIDTH * BLOCK_SIZE < player->p.x + + player->body.rectangle.w) { + player->p.x = WORLD_WIDTH * BLOCK_SIZE - + player->body.rectangle.w; } + struct OH *collidings; + collidings = create_ol(); - struct OL *collidings, *cur; - collidings = NULL; - for (int xi = (int) player.p.x / BLOCK_SIZE; - xi <= (int) player.p.x / BLOCK_SIZE + 1 && xi < WORLD_WIDTH; + for (int xi = (int) player->p.x / BLOCK_SIZE; + xi <= (int) player->p.x / BLOCK_SIZE + 1 && xi < WORLD_WIDTH; xi++) { - blc[xi] = blh[xi]; - while (blc[xi] != NULL) { - if (test_collision(&player, blc[xi]->o)) { - if (collidings == NULL) { - collidings = (struct OL *)malloc( - sizeof(struct OL)); - collidings->o = NULL; - collidings->next = NULL; - cur = collidings; - } else { - cur->next = (struct OL *)malloc( - sizeof(struct OL)); - cur = cur->next; - cur->next = NULL; - } - cur->o = blc[xi]->o; - } - blc[xi] = blc[xi]->next; + blc = blh[xi]->first; + while (blc != NULL) { + if (test_collision(player, blc->o)) + append_ol(collidings, blc->o); + blc = blc->next; } } - sort_ll(collidings, &player); - cur = collidings; - while (cur != NULL) { - handle_collision_mf(&player, cur->o); - cur = cur->next; + if (collidings->first != NULL) { + sort_ol(collidings, player); + blc = collidings->first; + while (blc != NULL) { + handle_collision_mf(player, blc->o); + blc = blc->next; + } } } @@ -231,18 +210,18 @@ game_play(void) x_clear_area(); for (int xi = 0; xi < WORLD_WIDTH; xi++) { - blc[xi] = blh[xi]; - while (blc[xi] != NULL) { + blc = blh[xi]->first; + while (blc != NULL) { x_draw_rectangle(0x00FF00, - blc[xi]->o->p.x, blc[xi]->o->p.y, // position - blc[xi]->o->body.rectangle.w, - blc[xi]->o->body.rectangle.h); - blc[xi] = blc[xi]->next; + blc->o->p.x, blc->o->p.y, // position + blc->o->body.rectangle.w, + blc->o->body.rectangle.h); + blc = blc->next; } } x_draw_rectangle(0x009FFF, - player.p.x, player.p.y, // position - player.body.rectangle.w, player.body.rectangle.h); + player->p.x, player->p.y, // position + player->body.rectangle.w, player->body.rectangle.h); char status_string[128]; snprintf(status_string, 128, "falling: %d", player_is_falling); x_draw_string(0x00FFFF, @@ -250,6 +229,8 @@ game_play(void) status_string, strlen(status_string)); } + for (int xi = 0; xi < WORLD_WIDTH; xi++) + free_ol(blh[xi]); } void @@ -342,34 +323,34 @@ handle_inputs(enum key_state key_state[]) return; } if (key_state[KEY_D] == KEY_DOWN) { - if (player.v.x > 0) { - player.a.x = 500; + if (player->v.x > 0) { + player->a.x = 500; } else { - player.a.x = 1000; + player->a.x = 1000; } } else if (key_state[KEY_A] == KEY_DOWN) { - if (player.v.x > 0) { - player.a.x = -1000; + if (player->v.x > 0) { + player->a.x = -1000; } else { - player.a.x = -500; + player->a.x = -500; } } else { if (player_is_falling) - player.a.x = -player.v.x; + player->a.x = -player->v.x; else - player.a.x = -3 * player.v.x; + player->a.x = -3 * player->v.x; } - if (player.v.x < -200) player.v.x = -200; - if (player.v.x > 200) player.v.x = 200; + if (player->v.x < -200) player->v.x = -200; + if (player->v.x > 200) player->v.x = 200; /* if (key_state[KEY_S] == KEY_DOWN) - player.v.y += 300; + player->v.y += 300; if (key_state[KEY_W] == KEY_DOWN) - player.v.y += -300; + player->v.y += -300; */ if (!player_is_falling && key_state[KEY_SPACE] == KEY_DOWN) - player.v.y = -450; + player->v.y = -450; } void @@ -396,71 +377,6 @@ sort_ll(struct OL *ol, struct Object *player) } int -main2(void) -{ - struct OL *blh[WORLD_WIDTH]; - struct OL *blc[WORLD_WIDTH]; // block list header and cursor - int bi[WORLD_WIDTH] = {0}; - - for (int xi = 0; xi < WORLD_WIDTH; xi++) { - blh[xi] = (struct OL *) malloc(sizeof(struct OL)); - blc[xi] = blh[xi]; - } - - for (int i = 0; i < WORLD_WIDTH * WORLD_HEIGHT; i++) { - if (world_map[i] == 'b') { - int xi = i % WORLD_WIDTH; - if (bi[xi] > 0) { - blc[xi]->next = - (struct OL *)malloc(sizeof(struct OL)); - blc[xi] = blc[xi]->next; - } - struct Object *block; - // TODO: don't forget to free these things. - block = (struct Object *)malloc(sizeof(struct Object)); - block->pp.x = block->p.x = i % WORLD_WIDTH * BLOCK_SIZE; - block->pp.y = block->p.y = i / WORLD_WIDTH * BLOCK_SIZE; - block->a.x = 0; - block->a.y = 0; - block->v.x = 0; - block->v.y = 0; - block->body.rectangle.w = BLOCK_SIZE; - block->body.rectangle.h = BLOCK_SIZE; - block->m = block->body.rectangle.w * - block->body.rectangle.h; - blc[xi]->o = block; - bi[xi]++; - } else if (world_map[i] == 'p') { - player.pp.x = player.p.x = i % WORLD_WIDTH * BLOCK_SIZE; - player.pp.y = player.p.y = i / WORLD_WIDTH * BLOCK_SIZE; - player.v.x = 0; - player.v.y = 0; - player.a.x = 0; - player.a.y = GRAVITY; - player.body.rectangle.w = player.body.rectangle.h = BLOCK_SIZE; - player.m = player.body.rectangle.w * player.body.rectangle.h; - } - } - - for (int xi = 0; xi < WORLD_WIDTH; xi++) - blc[xi] = blh[xi]; - while (blc[24] != NULL) { - printf("blc[24]->o->p: (%f, %f)\n", blc[24]->o->p.x, blc[24]->o->p.y); - blc[24] = blc[24]->next; - } - - sort_ll(blh[24], &player); - blc[24] = blh[24]; - while (blc[24] != NULL) { - printf("blc[24]->o->p: (%4.0f, %4.0f) %d\n", blc[24]->o->p.x, blc[24]->o->p.y, - object_dist(blc[24]->o, &player)); - blc[24] = blc[24]->next; - } - - return 0; -} - -int main(void) { x_setup_window(0, 0, diff --git a/ex8/util.c b/ex8/util.c @@ -26,6 +26,12 @@ create_object(float px, float py, float vx, float vy, float ax, float ay, return o; } +void +free_object(struct Object *o) +{ + free(o); +} + /* * 1 if collide, 0 if not */