xlib_playground

Xlib playground for experiments.
Log | Files | Refs

commit 3374603c5434713983c4add2d29fb9a4dbb799b1
parent dccf869678b3b7517425a0043f0e6a599fa6e47e
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Fri, 30 Dec 2022 19:16:54 +0900

add create_object function and be trying to use linked list

Diffstat:
Mex8/main.c | 111+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
Mex8/main.h | 3++-
Mex8/physics.c | 20++++++++++++++++++++
3 files changed, 86 insertions(+), 48 deletions(-)

diff --git a/ex8/main.c b/ex8/main.c @@ -117,30 +117,43 @@ game_play(void) int fps_count = 0; #endif struct timespec ts; - - int bi = 0; - for (int i = 0; i < WORLD_WIDTH * WORLD_HEIGHT; i++) { - if (world_map[i] == 'b') { - block[bi].pp.x = block[bi].p.x = i % WORLD_WIDTH * BLOCK_SIZE; - block[bi].pp.y = block[bi].p.y = i / WORLD_WIDTH * BLOCK_SIZE; - block[bi].a.x = 0; - block[bi].a.y = 0; - block[bi].v.x = 0; - block[bi].v.y = 0; - block[bi].body.rectangle.w = BLOCK_SIZE; - block[bi].body.rectangle.h = BLOCK_SIZE; - block[bi].m = block[bi].body.rectangle.w * - block[bi].body.rectangle.h; - bi++; - } 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; + struct ObjectList *blh[WORLD_WIDTH]; // block list header + struct ObjectList *blc[WORLD_WIDTH]; // block list cursor + int bi[WORLD_WIDTH] = {0}; + + for (int xi = 0; xi < WORLD_WIDTH; xi++) { + blh[xi] = (struct ObjectList *) malloc(sizeof(struct ObjectList)); + 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 ObjectList *)malloc(sizeof(struct ObjectList)); + blc[xi] = blc[xi]->next; + } + // TODO: don't forget to free these things. + blc[xi]->o = create_object(i % WORLD_WIDTH * BLOCK_SIZE, + i / WORLD_WIDTH * BLOCK_SIZE, + 0, 0, 0, 0, + BLOCK_SIZE, BLOCK_SIZE, + BLOCK_SIZE * BLOCK_SIZE); + bi[xi]++; + } else if (world_map[i] == 'p') { + player = *create_object(i % WORLD_WIDTH * BLOCK_SIZE, + i / WORLD_WIDTH * BLOCK_SIZE, + 0, 0, 0, 0, + BLOCK_SIZE, BLOCK_SIZE, + BLOCK_SIZE * BLOCK_SIZE); + } + } + for (int xi = 0; xi < WORLD_WIDTH; xi++) { + blc[xi] = blh[xi]; + while (blc[xi] != NULL) { + printf("%3d: (%f, %f)\n", xi, blc[xi]->o->p.x, blc[xi]->o->p.y); + blc[xi] = blc[xi]->next; } } @@ -402,19 +415,24 @@ sort_ll(struct ObjectList *ol, struct Object *player) } int -main(void) +main2(void) { - struct ObjectList *blh, *blc; - int bi = 0; + struct ObjectList *blh[WORLD_WIDTH]; + struct ObjectList *blc[WORLD_WIDTH]; // block list header and cursor + int bi[WORLD_WIDTH] = {0}; - blh = (struct ObjectList *) malloc(sizeof(struct ObjectList)); - blc = blh; + for (int xi = 0; xi < WORLD_WIDTH; xi++) { + blh[xi] = (struct ObjectList *) malloc(sizeof(struct ObjectList)); + blc[xi] = blh[xi]; + } for (int i = 0; i < WORLD_WIDTH * WORLD_HEIGHT; i++) { if (world_map[i] == 'b') { - if (bi > 0) { - blc->next = (struct ObjectList *)malloc(sizeof(struct ObjectList)); - blc = blc->next; + int xi = i % WORLD_WIDTH; + if (bi[xi] > 0) { + blc[xi]->next = + (struct ObjectList *)malloc(sizeof(struct ObjectList)); + blc[xi] = blc[xi]->next; } struct Object *block; // TODO: don't forget to free these things. @@ -429,8 +447,8 @@ main(void) block->body.rectangle.h = BLOCK_SIZE; block->m = block->body.rectangle.w * block->body.rectangle.h; - blc->o = block; - bi++; + 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; @@ -443,27 +461,26 @@ main(void) } } - - blc = blh; - while (blc != NULL) { - printf("blc->o->p: (%f, %f)\n", blc->o->p.x, blc->o->p.y); - blc = blc->next; + 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, &player); - blc = blh; - printf("blc: %p", blc); - while (blc != NULL) { - printf("blc->o->p: (%4.0f, %4.0f) %d\n", blc->o->p.x, blc->o->p.y, - object_dist(blc->o, &player)); - blc = blc->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 -main2(void) +main(void) { x_setup_window(0, 0, WORLD_WIDTH * BLOCK_SIZE, WORLD_HEIGHT * BLOCK_SIZE, diff --git a/ex8/main.h b/ex8/main.h @@ -68,7 +68,8 @@ int test_collision(struct Object *, struct Object *); // 1 if collide, 0 if not void next_tick(struct Object *o, long); void handle_collision_mf(struct Object *, struct Object *); int object_is_falling(struct Object *o, struct Object *fb, int num_f); - +struct Object *create_object(float, float, float, float, float, float, + int, int, int); /* * x.c */ diff --git a/ex8/physics.c b/ex8/physics.c @@ -1,4 +1,5 @@ #include <stdio.h> +#include <stdlib.h> #include "main.h" @@ -6,6 +7,25 @@ static int test_collision_rr(struct Object *, struct Object *); static void rect_next_tick(struct Object *, long); static void rect_handle_collision_mf(struct Object *, struct Object *); +struct Object * +create_object(float px, float py, float vx, float vy, float ax, float ay, + int w, int h, int m) +{ + struct Object *o; + o = (struct Object *)malloc(sizeof(struct Object)); + o->pp.x = o->p.x = px; + o->pp.y = o->p.y = py; + o->v.x = vx; + o->v.y = vy; + o->a.x = ax; + o->a.y = ay; + o->shape = SRECTANGLE; + o->body.rectangle.w = w; + o->body.rectangle.h = h; + o->m = m; + return o; +} + /* * 1 if collide, 0 if not */