commit bf449b2ef5ff9b1f59c2cc9393bb44c16c4177df
parent 8ffa18bbfb20799020f4a99a1ec9d3ac8f858ff2
Author: Matsuda Kenji <info@mtkn.jp>
Date: Sat, 31 Dec 2022 12:25:09 +0900
fix segfault
Diffstat:
M | ex8/main.c | | | 76 | ++++++++++++++++++++-------------------------------------------------------- |
M | ex8/main.h | | | 4 | ++-- |
2 files changed, 22 insertions(+), 58 deletions(-)
diff --git a/ex8/main.c b/ex8/main.c
@@ -53,8 +53,7 @@ void game_over(void);
void receive_events(enum key_state[]);
void handle_inputs(enum key_state[]);
/* sort */
-void sort_blocks(int colliding_blocks[], int distance[], int count);
-void sort_ll(struct ObjectList *ol, struct Object *player);
+void sort_ll(struct OL *ol, struct Object *player);
void
start_menu(void)
@@ -118,19 +117,19 @@ game_play(void)
int fps_count = 0;
#endif
struct timespec ts;
- struct ObjectList *blh[WORLD_WIDTH] = {0}; // block list header
- struct ObjectList *blc[WORLD_WIDTH] = {0}; // block list cursor
+ struct OL *blh[WORLD_WIDTH] = {0}; // block list header
+ struct OL *blc[WORLD_WIDTH] = {0}; // block list cursor
int bi[WORLD_WIDTH] = {0};
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 ObjectList *) malloc(sizeof(struct ObjectList));
+ blh[xi] = (struct OL *) malloc(sizeof(struct OL));
blc[xi] = blh[xi];
} else {
blc[xi]->next =
- (struct ObjectList *)malloc(sizeof(struct ObjectList));
+ (struct OL *)malloc(sizeof(struct OL));
blc[xi] = blc[xi]->next;
}
// TODO: don't forget to free these things.
@@ -179,7 +178,7 @@ game_play(void)
}
- struct ObjectList *collidings, *cur;
+ 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;
@@ -188,14 +187,14 @@ game_play(void)
while (blc[xi] != NULL) {
if (test_collision(&player, blc[xi]->o)) {
if (collidings == NULL) {
- collidings = (struct ObjectList *)malloc(
- sizeof(struct ObjectList));
+ collidings = (struct OL *)malloc(
+ sizeof(struct OL));
collidings->o = NULL;
collidings->next = NULL;
cur = collidings;
} else {
- cur->next = (struct ObjectList *)malloc(
- sizeof(struct ObjectList));
+ cur->next = (struct OL *)malloc(
+ sizeof(struct OL));
cur = cur->next;
cur->next = NULL;
}
@@ -204,28 +203,12 @@ game_play(void)
blc[xi] = blc[xi]->next;
}
}
- //sort_ll(collidings, &player);
+ sort_ll(collidings, &player);
cur = collidings;
while (cur != NULL) {
handle_collision_mf(&player, cur->o);
cur = cur->next;
}
-
- int colliding_blocks[9];
- int distance[9];
- int j = 0;
- for (int i = 0; i < NUM_BLOCK; i++)
- if (test_collision(&player, &block[i])) {
- colliding_blocks[j] = i;
- distance[j] = (player.p.x - block[i].p.x) *
- (player.p.x - block[i].p.x) +
- (player.p.y - block[i].p.y) *
- (player.p.y - block[i].p.y);
- j++;
- }
- sort_blocks(colliding_blocks, distance, j);
- for (int i = 0; i < j; i++)
- handle_collision_mf(&player, &block[colliding_blocks[i]]);
}
// fix fps
@@ -390,28 +373,7 @@ handle_inputs(enum key_state key_state[])
}
void
-swap(int list[], int i, int j)
-{
- int tmp = list[i];
- list[i] = list[j];
- list[j] = tmp;
-}
-
-void
-sort_blocks(int colliding_blocks[], int distance[], int count)
-{
- for (int i = 0; i < count - 1; i++) {
- for (int j = i + 1; j < count; j++) {
- if (distance[i] > distance[j]) {
- swap(colliding_blocks, i, j);
- swap(distance, i, j);
- }
- }
- }
-}
-
-void
-swap_ll(struct ObjectList *oi, struct ObjectList *oj)
+swap_ll(struct OL *oi, struct OL *oj)
{
struct Object *tmp;
tmp = oi->o;
@@ -427,9 +389,11 @@ object_dist(struct Object *o1, struct Object *o2)
}
void
-sort_ll(struct ObjectList *ol, struct Object *player)
+sort_ll(struct OL *ol, struct Object *player)
{
- struct ObjectList *oi, *oj;
+ if (ol == NULL)
+ return;
+ struct OL *oi, *oj;
for (oi = ol; oi->next != NULL; oi = oi->next) {
for (oj = oi->next; oj != NULL; oj = oj->next) {
if (object_dist(oj->o, player) < object_dist(oi->o, player))
@@ -441,12 +405,12 @@ sort_ll(struct ObjectList *ol, struct Object *player)
int
main2(void)
{
- struct ObjectList *blh[WORLD_WIDTH];
- struct ObjectList *blc[WORLD_WIDTH]; // block list header and cursor
+ 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 ObjectList *) malloc(sizeof(struct ObjectList));
+ blh[xi] = (struct OL *) malloc(sizeof(struct OL));
blc[xi] = blh[xi];
}
@@ -455,7 +419,7 @@ main2(void)
int xi = i % WORLD_WIDTH;
if (bi[xi] > 0) {
blc[xi]->next =
- (struct ObjectList *)malloc(sizeof(struct ObjectList));
+ (struct OL *)malloc(sizeof(struct OL));
blc[xi] = blc[xi]->next;
}
struct Object *block;
diff --git a/ex8/main.h b/ex8/main.h
@@ -41,9 +41,9 @@ struct Object {
int m;
};
-struct ObjectList {
+struct OL {
struct Object *o;
- struct ObjectList *next;
+ struct OL *next;
};
struct rect {