commit 24ed6c2acdb8e14e18659320e83d115631c97f39
parent 2d09861a9c093fa2cf783b7b4ceb9a0c0634cb94
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Sun,  8 Jan 2023 08:13:21 +0900
delete list header
Diffstat:
| M | ex9/main.c | | | 53 | ++++++++++++++++++++++++++--------------------------- | 
| M | ex9/main.h | | | 18 | +++++------------- | 
| M | ex9/util.c | | | 95 | ++++++++++++++++++++++++++++++++++--------------------------------------------- | 
3 files changed, 72 insertions(+), 94 deletions(-)
diff --git a/ex9/main.c b/ex9/main.c
@@ -125,16 +125,15 @@ game_play(void)
 	int fps_count = 0;
 #endif
 	struct timespec ts;
-	struct OH *blh[WORLD_WIDTH]; // block list header
-	struct OL *blc;
-	struct OH *flh; // flag
-	struct OL *flc;
+	struct OL *bl[WORLD_WIDTH]; // block list header
+	struct OL *fl; // flag
+	struct OL *olp;
 	int scroll_dst = 0;
 
 
 	for (int xi = 0; xi < WORLD_WIDTH; xi++)
-		blh[xi] = create_ol();
-	flh = create_ol();
+		bl[xi] = NULL;
+	fl = NULL;
 
 	for (int i = 0; i < NUM_OBJ_TYPE; i++)
 		for (int j = 0; j < NUM_OBJ_TYPE; j++)
@@ -142,11 +141,12 @@ 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.
-            append_ol(blh[xi], create_object(TBLOCK, 0x00FF00,
+            bl[xi] = addfront_ol(bl[xi], create_object(TBLOCK, 0x00FF00,
 				i % WORLD_WIDTH * BLOCK_SIZE,
 				i / WORLD_WIDTH * BLOCK_SIZE,
 				0, 0, 0, 0,
@@ -160,7 +160,7 @@ game_play(void)
 				BLOCK_SIZE, BLOCK_SIZE,
 				BLOCK_SIZE * BLOCK_SIZE);
 		} else if (world_map[i] == 'f') {
-			append_ol(flh, create_object(TFLAG, 0xFFFF00,
+			fl = addfront_ol(fl, create_object(TFLAG, 0xFFFF00,
 				i % WORLD_WIDTH * BLOCK_SIZE,
 				i / WORLD_WIDTH * BLOCK_SIZE,
 				0, 0, 0, 0,
@@ -207,33 +207,32 @@ game_play(void)
 					scroll_dst = 0;
 			}
 
-			struct OH *collidings;
-			collidings = create_ol();
+			struct OL *collidings = NULL;
 
 			player_is_falling = 1;
 			for (int xi = (int) player->p.x / BLOCK_SIZE;
 				xi <= (int) player->p.x / BLOCK_SIZE + 1 &&
 				xi < WORLD_WIDTH;
 				xi++) {
-				for (blc = blh[xi]->first; blc != NULL; blc = blc->next) {
-					if (is_on_floor_before(player, blc->o)) {
+				for (olp = bl[xi]; olp != NULL; olp = olp->next) {
+					if (is_on_floor_before(player, olp->o)) {
 						player_is_falling = 0;
 					}
-					if (test_collision(player, blc->o))
-						append_ol(collidings, blc->o);
+					if (test_collision(player, olp->o))
+						collidings = addfront_ol(collidings, olp->o);
 				}
 			}
-			if (collidings->first != NULL) {
+			if (collidings != NULL) {
 				sort_ol(collidings, player);
-				for (blc = collidings->first; blc != NULL; blc = blc->next) {
-					handle_collision(player, blc->o);
+				for (olp = collidings; olp != NULL; olp = olp->next) {
+					handle_collision(player, olp->o);
 				}
 			}
 			free_ol(collidings);
 
 			// collision aganst the goal flag
-			for (flc = flh->first; flc != NULL; flc = flc->next){
-				if (test_collision(player, flc->o)) {
+			for (olp = fl; olp != NULL; olp = olp->next){
+				if (test_collision(player, olp->o)) {
 					next_menu = GAME_CLEAR;
 					break;
 				}
@@ -264,16 +263,16 @@ game_play(void)
 		for (int xi = scroll_dst / BLOCK_SIZE;
 			xi < (WIN_WIDTH + scroll_dst) / BLOCK_SIZE + 1 &&
 			xi < WORLD_WIDTH; xi++) {
-			for (blc = blh[xi]->first; blc != NULL; blc = blc->next) {
-				draw_object_scroll(blc->o, scroll_dst);
+			for (olp = bl[xi]; olp != NULL; olp = olp->next) {
+				draw_object_scroll(olp->o, scroll_dst);
 			}
 		}
 		draw_object_scroll(player, scroll_dst);
 
-		for (flc = flh->first; flc != NULL; flc = flc->next) {
-			if (0 <= flc->o->p.x - scroll_dst &&
-				flc->o->p.x - scroll_dst < WIN_WIDTH) {
-				draw_object_scroll(flc->o, 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);
 			} else {
 				break;
 			}
@@ -281,8 +280,8 @@ game_play(void)
 
 	}
 	for (int xi = 0; xi < WORLD_WIDTH; xi++)
-		free_obj_and_ol(blh[xi]);
-	free_obj_and_ol(flh);
+		free_obj_and_ol(bl[xi]);
+	free_obj_and_ol(fl);
 }
 
 void
diff --git a/ex9/main.h b/ex9/main.h
@@ -51,14 +51,6 @@ struct Object {
 	int m;
 };
 
-struct OH {
-	struct OL *first;
-};
-
-/*
- * linked list which starts with OH and
- * terminates with ->next == NULL
- */
 struct OL {
 	struct Object *o;
 	struct OL *next;
@@ -67,13 +59,13 @@ struct OL {
 extern void (* col_func[NUM_OBJ_TYPE][NUM_OBJ_TYPE])(struct Object *, struct Object *);
 
 void handle_collision(struct Object *, struct Object *);
-struct OH *create_ol(void);
-void free_ol(struct OH *);
-void free_obj_and_ol(struct OH *);
-void append_ol(struct OH *, struct Object *);
+void free_ol(struct OL *);
+void free_obj_and_ol(struct OL *);
+struct OL *addfront_ol(struct OL *, struct Object *);
 void swap_ol(struct OL *, struct OL *);
-void sort_ol(struct OH *, struct Object *);
+void sort_ol(struct OL *, struct Object *);
 int object_dist(struct Object *, struct Object *);
+void print_ol(struct OL *);
 
 int test_collision(struct Object *, struct Object *); // 1 if collide, 0 if not
 void next_tick(struct Object *o, long);
diff --git a/ex9/util.c b/ex9/util.c
@@ -162,84 +162,64 @@ object_is_falling(struct Object *o, struct Object *fb, int num_f)
 	return 1;
 }
 
-struct OH *
+struct OL *
 create_ol(void)
 {
-	struct OH *oh;
-	oh = (struct OH *)malloc(sizeof(struct OH));
-	oh->first = NULL;
-	return oh;
+	struct OL *ol;
+	ol = (struct OL *)malloc(sizeof(struct OL));
+	ol->next = NULL;
+	ol->o = NULL;
+	return ol;
 }
 
 void
-free_ol(struct OH *oh)
+free_ol(struct OL *ol)
 {
-	if (oh == NULL)
-		return;
-	struct OL *ol0, *ol1;
-	ol0 = ol1 = oh->first;
-	while (ol1 != NULL) {
-		ol1 = ol0->next;
-		free(ol0);
-		ol0 = ol1;
+	struct OL *p, *q;
+	for (p = q = ol; q != NULL; p = q) {
+		q = p->next;
+		free(p); // asume that p->o is freed elsewhere
 	}
-	free(oh);
 }
 
 void
-free_obj_and_ol(struct OH *oh)
+free_obj_and_ol(struct OL *ol)
 {
-	if (oh == NULL)
-		return;
-	struct OL *ol0, *ol1;
-	ol0 = ol1 = oh->first;
-	while (ol1 != NULL) {
-		ol1 = ol0->next;
-		free(ol0->o);
-		free(ol0);
-		ol0 = ol1;
+	struct OL *p, *q;
+	for (p = q = ol; q != NULL; p = q) {
+		q = p->next;
+		free(p->o);
+		free(p);
 	}
-
 }
 
-void
-append_ol(struct OH *oh, struct Object *o)
+struct OL *
+addfront_ol(struct OL *ol, struct Object *o)
 {
-    struct OL *cur;
-
-    if (oh->first == NULL) {
-        oh->first = (struct OL *)malloc(sizeof(struct OL));
-        cur = oh->first;
-    } else {
-        cur = oh->first;
-        while (cur->next != NULL)
-            cur = cur->next;
-        cur->next = (struct OL *)malloc(sizeof(struct OL));
-        cur = cur->next;
-    }
-    cur->o = o;
-    cur->next = NULL;
+    struct OL *nol;
+	nol = (struct OL *)malloc(sizeof(struct OL));
+	nol->next = ol;
+	nol->o = o;
+	return nol;
 }
 
 void
-swap_ol(struct OL *ol0, struct OL *ol1)
+swap_ol(struct OL *p, struct OL *q)
 {
 	struct Object *tmp;
-	tmp = ol0->o;
-	ol0->o = ol1->o;
-	ol1->o = tmp;
+	tmp = p->o;
+	p->o = q->o;
+	q->o = tmp;
 }
 
 void
-sort_ol(struct OH *oh, struct Object *player)
+sort_ol(struct OL *ol, struct Object *player)
 {
-    if (oh == NULL)
-        return;
-    struct OL *oi, *oj;
-    for (oi = oh->first; 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))
-                swap_ol(oi, oj);
+    struct OL *p, *q;
+    for (p = ol; p != NULL && p->next != NULL; p = p->next) {
+        for (q = p->next; q != NULL; q = q->next) {
+            if (object_dist(q->o, player) < object_dist(p->o, player))
+                swap_ol(p, q);
         }
     }
 }
@@ -266,3 +246,10 @@ is_on_floor_before(struct Object *player, struct Object *floor)
 void handle_collision(struct Object *o0, struct Object *o1) {
 	(* col_func[o0->type][o1->type])(o0, o1);
 }
+
+void print_ol(struct OL *ol) {
+	for (; ol != NULL; ol = ol->next) {
+		printf("(%3.0f, %3.0f)%s", ol->o->p.x, ol->o->p.y,
+			ol->next == NULL ? "\n" : "->");
+	}
+}