commit 24023f9a5f4d742e6ebb574c3bb04da7ac5742b3
parent 9af836feebbf47829f7defca013e3991a55d7406
Author: Matsuda Kenji <info@mtkn.jp>
Date: Thu, 12 Jan 2023 10:00:54 +0900
Use typedef for structs
Diffstat:
M | ex9/list.c | | | 40 | ++++++++++++++++++++-------------------- |
M | ex9/list.h | | | 24 | ++++++++++++------------ |
M | ex9/main.c | | | 32 | ++++++++++++++++---------------- |
M | ex9/object.c | | | 84 | ++++++++++++++++++++++++++++++++++++++++---------------------------------------- |
M | ex9/object.h | | | 63 | +++++++++++++++++++++++++++++++-------------------------------- |
5 files changed, 121 insertions(+), 122 deletions(-)
diff --git a/ex9/list.c b/ex9/list.c
@@ -4,16 +4,16 @@
#include "list.h"
-static int __lqsort(struct List *p, struct List *tail,
+static int __lqsort(List *p, List *tail,
int (* cmp)(void *, void *));
-static void __lprint(struct List *p, struct List *tail, void (* pfnc)(void *));
+static void __lprint(List *p, List *tail, void (* pfnc)(void *));
/*
* Laddfront() adds item in front of List p.
* Returns NULL if error occured.
*/
-struct List *
-laddfront(struct List *p, void *item)
+List *
+laddfront(List *p, void *item)
{
if (item == NULL) {
// TODO: should print some message ??
@@ -21,8 +21,8 @@ laddfront(struct List *p, void *item)
return NULL;
}
- struct List *newlp;
- if((newlp = (struct List *)malloc(sizeof(struct List))) == NULL) {
+ List *newlp;
+ if((newlp = (List *)malloc(sizeof(List))) == NULL) {
fprintf(stderr, "laddfront: Error. Could not allocate memory.\n");
return NULL;
}
@@ -35,9 +35,9 @@ laddfront(struct List *p, void *item)
* Lfree() frees List *p. Its items should be freed elsewhere.
*/
void
-lfree(struct List *p)
+lfree(List *p)
{
- struct List *q;
+ List *q;
for (q = p; q != NULL; q = p) {
p = q->next;
free(q);
@@ -48,9 +48,9 @@ lfree(struct List *p)
* Lfreei() frees List *p. Its items are freed using ifree().
*/
void
-lfreei(struct List *p, void (* ifree)(void *))
+lfreei(List *p, void (* ifree)(void *))
{
- struct List *q;
+ List *q;
for (q = p; q != NULL; q = p) {
p = q->next;
ifree(q->item);
@@ -59,13 +59,13 @@ lfreei(struct List *p, void (* ifree)(void *))
}
void
-lprint(struct List *p, void (* pfnc)(void *))
+lprint(List *p, void (* pfnc)(void *))
{
__lprint(p, NULL, pfnc);
}
void
-__lprint(struct List *p, struct List *tail, void (* pfnc)(void *))
+__lprint(List *p, List *tail, void (* pfnc)(void *))
{
if (p == NULL) {
printf("\n");
@@ -87,9 +87,9 @@ __lprint(struct List *p, struct List *tail, void (* pfnc)(void *))
* Bubble sort. returns -1 if error occured, 0 if succeed.
*/
int
-lbsort(struct List *p, int (* cmp)(void *, void *))
+lbsort(List *p, int (* cmp)(void *, void *))
{
- struct List *q;
+ List *q;
for (;p != NULL && p->next != NULL; p = p->next) {
for (q = p->next; q != NULL; q = q->next) {
@@ -113,18 +113,18 @@ lbsort(struct List *p, int (* cmp)(void *, void *))
* Returns 0 if succeed, -1 if error occured.
*/
int
-lqsort(struct List *p, int (* cmp)(void *, void *))
+lqsort(List *p, int (* cmp)(void *, void *))
{
return __lqsort(p, NULL, cmp);
}
static int
-__lqsort(struct List *p, struct List *tail, int (* cmp)(void *, void *))
+__lqsort(List *p, List *tail, int (* cmp)(void *, void *))
{
if (p == tail || p->next == tail)
return 0;
- struct List *pivot, *q, *last;
+ List *pivot, *q, *last;
int i;
/* select the pivot randomly */
@@ -154,7 +154,7 @@ __lqsort(struct List *p, struct List *tail, int (* cmp)(void *, void *))
}
void
-lswap(struct List *p, struct List *q)
+lswap(List *p, List *q)
{
if (p == NULL || q == NULL)
return;
@@ -169,7 +169,7 @@ lswap(struct List *p, struct List *q)
* Only checks the address of each items.
*/
int
-leqa(struct List *p, struct List *q)
+leqa(List *p, List *q)
{
for (;p != NULL && q != NULL; p = p->next, q = q->next) {
if (p->item != q->item)
@@ -185,7 +185,7 @@ leqa(struct List *p, struct List *q)
* Compares each items with eq().
*/
int
-leqi(struct List *p, struct List *q, int (*eq)(void *, void*))
+leqi(List *p, List *q, int (*eq)(void *, void*))
{
for (;p != NULL && q != NULL; p = p->next, q = q->next) {
if (!eq(p->item, q->item))
diff --git a/ex9/list.h b/ex9/list.h
@@ -1,15 +1,15 @@
-struct List * laddfront(struct List *p, void *item);
-void lfree(struct List *p);
-void lfreei(struct List *p, void (* ifree)(void *));
-void lprint(struct List *p, void (* pfnc)(void *));
-int lbsort(struct List *p, int (* cmp)(void *, void *));
-int lqsort(struct List *p, int (* cmp)(void *, void *));
-void lswap(struct List *p, struct List *q);
-int leqa(struct List *p, struct List *q);
-int leqi(struct List *p, struct List *q, int (*eq)(void *, void *));
-
/* Linked list. An empty list must be initialized with NULL. */
-struct List {
+typedef struct List {
struct List *next;
void *item;
-};
+} List;
+
+List * laddfront(List *p, void *item);
+void lfree(List *p);
+void lfreei(List *p, void (* ifree)(void *));
+void lprint(List *p, void (* pfnc)(void *));
+int lbsort(List *p, int (* cmp)(void *, void *));
+int lqsort(List *p, int (* cmp)(void *, void *));
+void lswap(List *p, List *q);
+int leqa(List *p, List *q);
+int leqi(List *p, List *q, int (*eq)(void *, void *));
diff --git a/ex9/main.c b/ex9/main.c
@@ -46,8 +46,8 @@ enum next_menu {
/* variables */
enum next_menu next_menu = START_MENU;
-void (* col_func[NUM_OBJ_TYPE][NUM_OBJ_TYPE])(struct Object *,
- struct Object *);
+void (* col_func[NUM_OBJ_TYPE][NUM_OBJ_TYPE])(Object *,
+ Object *);
/* function prototypes */
/* menus */
@@ -57,15 +57,15 @@ void game_over(void);
void game_clear(void);
/* events */
void receive_events(enum key_state[]);
-void handle_inputs(enum key_state[], struct Object *);
+void handle_inputs(enum key_state[], Object *);
/* collision functions */
-void col_pf(struct Object *, struct Object *); // player-flag
-void col_pb(struct Object *, struct Object *); // player-block
+void col_pf(Object *, Object *); // player-flag
+void col_pb(Object *, Object *); // player-block
/* misc: these functions use interfaces from multiple header files */
/* TODO: I want to rewrite these more cleanly */
-void draw_object(const struct Object *);
-void draw_object_scroll(const struct Object *, int);
+void draw_object(const Object *);
+void draw_object_scroll(const Object *, int);
void
start_menu(void)
@@ -129,9 +129,9 @@ game_play(void)
int fps_count = 0;
#endif
struct timespec ts;
- struct List *ol; // block list
- struct Object *player;
- struct List *olc; // cursor used to scan ol
+ List *ol; // block list
+ Object *player;
+ List *olc; // cursor used to scan ol
int scroll_dst = 0;
ol = NULL;
@@ -250,7 +250,7 @@ game_play(void)
x_clear_area();
for (olc = ol; olc != NULL; olc = olc->next) {
- draw_object_scroll((struct Object *)olc->item, scroll_dst);
+ draw_object_scroll((Object *)olc->item, scroll_dst);
}
}
@@ -358,7 +358,7 @@ receive_events(enum key_state key_state[])
}
void
-handle_inputs(enum key_state key_state[], struct Object *player)
+handle_inputs(enum key_state key_state[], Object *player)
{
if (key_state[KEY_Q] == KEY_DOWN){
next_menu = GAME_OVER;
@@ -390,14 +390,14 @@ handle_inputs(enum key_state key_state[], struct Object *player)
}
}
-void col_pf(struct Object *o0, struct Object *o1) {
+void col_pf(Object *o0, Object *o1) {
if (!test_collision(o0, o1))
return;
handle_collision_mf(o0, o1);
next_menu = GAME_CLEAR;
}
-void col_pb(struct Object *op, struct Object *ob) {
+void col_pb(Object *op, Object *ob) {
if (op->type == TBLOCK && ob->type == TPLAYER) {
col_pb(ob, op);
return;
@@ -414,7 +414,7 @@ void col_pb(struct Object *op, struct Object *ob) {
void
-draw_object(const struct Object *o)
+draw_object(const Object *o)
{
if (o->shape == SRECTANGLE) {
x_draw_rectangle(o->color,
@@ -428,7 +428,7 @@ draw_object(const struct Object *o)
}
void
-draw_object_scroll(const struct Object *o, int sd)
+draw_object_scroll(const Object *o, int sd)
{
if (o->shape == SRECTANGLE) {
if (sd - o->body.rectangle.w <= o->p.x &&
diff --git a/ex9/object.c b/ex9/object.c
@@ -5,27 +5,27 @@
#include "list.h"
#include "object.h"
-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 *);
+static int test_collision_rr(Object *, Object *);
+static void rect_next_tick(Object *, long);
+static void rect_handle_collision_mf(Object *, Object *);
/*
* Pair of two objects with their distance.
* Used by lhandle_collision();
*/
-struct LLD {
- struct Object *o0;
- struct Object *o1;
+typedef struct LLD {
+ Object *o0;
+ Object *o1;
float dist;
-};
+} LLD;
-struct Object *
+Object *
create_object(enum object_type t, uint32_t color,
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));
+ Object *o;
+ o = (Object *)malloc(sizeof(Object));
o->type = t;
o->color = color;
o->shape = SRECTANGLE;
@@ -43,7 +43,7 @@ create_object(enum object_type t, uint32_t color,
}
void
-free_object(struct Object *o)
+free_object(Object *o)
{
free(o);
}
@@ -52,7 +52,7 @@ free_object(struct Object *o)
* 1 if collide, 0 if not
*/
int
-test_collision(struct Object *o1, struct Object *o2)
+test_collision(Object *o1, Object *o2)
{
if (o1->shape == SRECTANGLE && o2->shape == SRECTANGLE)
return test_collision_rr(o1, o2);
@@ -63,7 +63,7 @@ test_collision(struct Object *o1, struct Object *o2)
}
static int
-test_collision_rr(struct Object *o1, struct Object *o2)
+test_collision_rr(Object *o1, Object *o2)
{
if (o1->shape != SRECTANGLE || o2->shape != SRECTANGLE) {
fprintf(stderr, "test_collision_rr: invalid objects\n");
@@ -76,7 +76,7 @@ test_collision_rr(struct Object *o1, struct Object *o2)
}
void
-next_tick(struct Object *o, long ndt)
+next_tick(Object *o, long ndt)
{
switch (o->shape) {
case SRECTANGLE:
@@ -89,7 +89,7 @@ next_tick(struct Object *o, long ndt)
}
static void
-rect_next_tick(struct Object *o, long ndt)
+rect_next_tick(Object *o, long ndt)
{
if (o->shape != SRECTANGLE) {
fprintf(stderr, "rect_next_tick: invalid object shape\n");
@@ -104,7 +104,7 @@ rect_next_tick(struct Object *o, long ndt)
}
void
-handle_collision_mf(struct Object *om, struct Object *of)
+handle_collision_mf(Object *om, Object *of)
{
// TODO: too many testing?
if (!test_collision(om, of))
@@ -121,7 +121,7 @@ handle_collision_mf(struct Object *om, struct Object *of)
* Handle collision of a moving rect against fixed rect
*/
static void
-rect_handle_collision_mf(struct Object *om, struct Object *of)
+rect_handle_collision_mf(Object *om, Object *of)
{
// TODO: too many testing?
if (!test_collision(om, of))
@@ -155,14 +155,14 @@ rect_handle_collision_mf(struct Object *om, struct Object *of)
}
int
-object_dist(struct Object *o1, struct Object *o2)
+object_dist(Object *o1, Object *o2)
{
return (o1->p.x - o2->p.x) * (o1->p.x - o2->p.x) +
(o1->p.y - o2->p.y) * (o1->p.y - o2->p.y);
}
int
-is_on_floor(struct Object *p, struct Object *f)
+is_on_floor(Object *p, Object *f)
{
if (p->p.x + p->body.rectangle.w < f->p.x ||
f->p.x + f->body.rectangle.w < p->p.x)
@@ -171,7 +171,7 @@ is_on_floor(struct Object *p, struct Object *f)
}
int
-is_on_floor_before(struct Object *p, struct Object *f)
+is_on_floor_before(Object *p, Object *f)
{
if (p->pp.x + p->body.rectangle.w < f->pp.x ||
f->pp.x + f->body.rectangle.w < p->pp.x)
@@ -180,7 +180,7 @@ is_on_floor_before(struct Object *p, struct Object *f)
}
void
-ohandle_collision(struct Object *o0, struct Object *o1)
+ohandle_collision(Object *o0, Object *o1)
{
if (col_func[o0->type][o1->type] == NULL)
return;
@@ -188,7 +188,7 @@ ohandle_collision(struct Object *o0, struct Object *o1)
}
void
-oprint(struct Object *o)
+oprint(Object *o)
{
printf("[%d: (%4.0f, %4.0f)]", o->type, o->p.x, o->p.y);
}
@@ -197,7 +197,7 @@ oprint(struct Object *o)
* Compare object's x coordinate.
*/
int
-olcmpx(struct Object *p, struct Object *q)
+olcmpx(Object *p, Object *q)
{
if(p->p.x < q->p.x)
return -1;
@@ -212,13 +212,13 @@ olcmpx(struct Object *p, struct Object *q)
* TODO: change name to better one.
*/
void
-olqsortx(struct List *p)
+olqsortx(List *p)
{
lqsort(p, (int (*)(void *, void *))&olcmpx);
}
static int
-lldcmpdst(struct LLD *p, struct LLD *q)
+lldcmpdst(LLD *p, LLD *q)
{
if (p->dist < q->dist)
return -1;
@@ -229,7 +229,7 @@ lldcmpdst(struct LLD *p, struct LLD *q)
}
static void
-lldprint(struct LLD *p)
+lldprint(LLD *p)
{
printf("{");
oprint(p->o0);
@@ -242,32 +242,32 @@ lldprint(struct LLD *p)
* The order of list o will be changed.
*/
void
-lhandle_collision(struct List *p)
+lhandle_collision(List *p)
{
olqsortx(p);
- struct List *stack, *tmp;
+ List *stack, *tmp;
stack = NULL;
for (; p != NULL; p = p->next) {
// If p doesn't have intersection with an item in the stack
// Checking only against the first object suffices because stack is sorted.
if (stack != NULL &&
- ((struct Object *)stack->item)->p.x +
- ((struct Object *)stack->item)->body.rectangle.w <=
- ((struct Object *)p->item)->p.x) {
+ ((Object *)stack->item)->p.x +
+ ((Object *)stack->item)->body.rectangle.w <=
+ ((Object *)p->item)->p.x) {
// Handle collision among the items in the stack.
- // LLD: {struct Object *o0, struct Object *o1, float dist}
+ // LLD: {Object *o0, Object *o1, float dist}
// Sort LLD according to dist.
- struct List *lld, *tmplld;
- struct List *s, *t;
+ List *lld, *tmplld;
+ List *s, *t;
lld = NULL;
for (s = stack; s != NULL && s->next != NULL; s = s->next) {
for (t = s->next; t != NULL; t = t->next) {
- struct LLD *l;
- l = (struct LLD *)malloc(sizeof(struct LLD));
- l->o0 = (struct Object *)s->item;
- l->o1 = (struct Object *)t->item;
- l->dist = object_dist((struct Object *)s->item,
- (struct Object *)t->item);
+ LLD *l;
+ l = (LLD *)malloc(sizeof(LLD));
+ l->o0 = (Object *)s->item;
+ l->o1 = (Object *)t->item;
+ l->dist = object_dist((Object *)s->item,
+ (Object *)t->item);
tmplld = laddfront(lld, l);
if (tmplld == NULL) {
fprintf(stderr, "lhandle_collision: Error. Could not"
@@ -279,8 +279,8 @@ lhandle_collision(struct List *p)
}
lqsort(lld, (int (*)(void *, void *))&lldcmpdst);
for (tmplld = lld; tmplld != NULL; tmplld = tmplld->next) {
- ohandle_collision(((struct LLD *)tmplld->item)->o0,
- ((struct LLD *)tmplld->item)->o1);
+ ohandle_collision(((LLD *)tmplld->item)->o0,
+ ((LLD *)tmplld->item)->o1);
}
lfree(lld);
lfree(stack);
diff --git a/ex9/object.h b/ex9/object.h
@@ -14,57 +14,56 @@ enum object_type {
NUM_OBJ_TYPE,
};
-struct Point {
+typedef struct Point {
float x;
float y;
-};
+} Point;
-struct Rectangle { // origin is top left corner
+typedef struct Rectangle { // origin is top left corner
int w, h;
-};
+} Rectangle;
-struct Triangle {
- struct Point v2;
- struct Point v3;
-};
+typedef struct Triangle {
+ Point v2;
+ Point v3;
+} Triangle;
-struct Circle { // origin is center
+typedef struct Circle { // origin is center
float r;
-};
+} Circle;
union Body {
- struct Circle circle;
- struct Triangle triangle;
- struct Rectangle rectangle;
+ Circle circle;
+ Triangle triangle;
+ Rectangle rectangle;
};
-struct Object {
+typedef struct Object {
enum object_type type;
enum object_shape shape;
uint32_t color;
- struct Point pp;
- struct Point p;
- struct Point v;
- struct Point a;
+ Point pp;
+ Point p;
+ Point v;
+ Point a;
union Body body;
int m;
int on_floor;
-};
+} Object;
-extern void (* col_func[NUM_OBJ_TYPE][NUM_OBJ_TYPE])(struct Object *,
- struct Object *);
+extern void (* col_func[NUM_OBJ_TYPE][NUM_OBJ_TYPE])(Object *, Object *);
-int object_dist(struct Object *, struct Object *);
-void ohandle_collision(struct Object *, struct Object *);
-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 *);
-struct Object *create_object(enum object_type, uint32_t,
+int object_dist(Object *, Object *);
+void ohandle_collision(Object *, Object *);
+int test_collision(Object *, Object *); // 1 if collide, 0 if not
+void next_tick(Object *o, long);
+void handle_collision_mf(Object *, Object *);
+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(struct Object *, struct Object *);
-int is_on_floor_before(struct Object *, struct Object *);
-void oprint(struct Object *);
+void free_object(Object *);
+int is_on_floor(Object *, Object *);
+int is_on_floor_before(Object *, Object *);
+void oprint(Object *);
-void lhandle_collision(struct List *);
+void lhandle_collision(List *);