From e2d01db144df6cf938891500d7785a96888f00b8 Mon Sep 17 00:00:00 2001 From: Nakidai Date: Sat, 9 Dec 2023 00:35:49 +0300 Subject: [PATCH] Refactor - Fix bug when Make cannot create obj/platform - Remove some mallocs --- Makefile | 9 +++------ include/player.h | 6 +++--- include/screen.h | 12 ++++++------ src/main.c | 50 ++++++++++++++++++++++++------------------------ src/player.c | 21 +++++++++----------- src/screen.c | 19 +++++++++--------- 6 files changed, 55 insertions(+), 62 deletions(-) diff --git a/Makefile b/Makefile index deb2798..a55765b 100644 --- a/Makefile +++ b/Makefile @@ -11,16 +11,13 @@ DEFLDFLAGS = $(shell if echo "" | cc -E -dM -xc - | grep __FreeBSD__ > /dev/null all: $(OUT) -$(OBJDIR): - mkdir $(OBJDIR) - -$(OBJDIR)/platform: $(OBJDIR) - mkdir $(OBJDIR)/platform +$(OBJDIR)/platform: + mkdir -p $(OBJDIR)/platform $(OBJDIR)/%.o: $(SRCDIR)/%.c $(CC) -c -o $@ $< $(CFLAGS) $(INCLUDE) -$(OUT): $(OBJDIR) $(OBJDIR)/platform $(OBJ) +$(OUT): $(OBJDIR)/platform $(OBJ) $(CC) -o $@ $(OBJ) $(LDFLAGS) $(DEFLDFLAGS) clean: diff --git a/include/player.h b/include/player.h index c5ed465..e013f2c 100644 --- a/include/player.h +++ b/include/player.h @@ -21,10 +21,10 @@ struct player_t int score; }; -Player *playerCreate(Direction direction, int x, int y, int score); +void playerCreate(Player *buffer, Direction direction, int x, int y, int score); -bool playerCheckSelfCollision(Player *player); -bool playerCheckFoodCollision(Player *player, Food food); +bool playerCheckSelfCollision(Player player); +bool playerCheckFoodCollision(Player player, Food food); bool playerDoTick(Player *player, Food food); #endif /* __PLAYER_H__ */ diff --git a/include/screen.h b/include/screen.h index cc78e7f..7b394ea 100644 --- a/include/screen.h +++ b/include/screen.h @@ -11,17 +11,17 @@ typedef struct screen_t Point *screen; } Screen; -Screen *screenCreate(int width, int height, Point fill_value); -void screenShow(Screen *screen); +void screenCreate(Screen *buffer, int width, int height, Point fill_value); +void screenShow(Screen screen); -static inline Point *screenGetPoint(Screen *screen, int x, int y) +static inline Point *screenGetPoint(Screen screen, int x, int y) { - return screen->screen + x + (y * screen->width); + return screen.screen + x + (y * screen.width); } -static inline void screenSet(Screen *screen, Point fill_value) +static inline void screenSet(Screen screen, Point fill_value) { - memset(screen->screen, fill_value, screen->width * screen->height * sizeof(char)); + memset(screen.screen, fill_value, screen.width * screen.height * sizeof(char)); } diff --git a/src/main.c b/src/main.c index 30dcf0d..0f55f0a 100644 --- a/src/main.c +++ b/src/main.c @@ -12,14 +12,14 @@ #include "platform/thread.h" #include "platform/screen.h" -void drawPlayer(Player *player, Screen *screen) +void drawPlayer(Player player, Screen screen) { PlayerNode *node; - for (node = player->tail; node != NULL; node = node->next) + for (node = player.tail; node != NULL; node = node->next) *screenGetPoint(screen, node->x, node->y) = '#'; } -Food generateFood(Player *player) +Food generateFood(Player player) { Food food; do @@ -32,19 +32,19 @@ Food generateFood(Player *player) int main(int argc, char **argv) { srand((unsigned int)time(NULL)); - Player *player = playerCreate(DOWN, DEFX, DEFY, 0); - Screen *screen = screenCreate(SIZE, SIZE, ' '); + Player player; playerCreate(&player, DOWN, DEFX, DEFY, 0); + Screen screen; screenCreate(&screen, SIZE, SIZE, ' '); int i; int head_x, head_y; Food food = generateFood(player); - bool *running = malloc(sizeof(bool)); *running = true; - int *key = malloc(sizeof(char)); *key = 0; + bool running = true; + int key = 0; bool stopped = false; - InputArgs input_args = (InputArgs){ key, running }; + InputArgs input_args = (InputArgs){ &key, &running }; threadCreate(input, &input_args); - while (*running) + while (running) { screenSet(screen, ' '); drawPlayer(player, screen); @@ -52,37 +52,37 @@ int main(int argc, char **argv) resetCoordinates(); screenShow(screen); for (i = 0; i < SIZE*2; ++i) putchar('-'); - printf("\nScore: %d\n", player->score); + printf("\nScore: %d\n", player.score); sleepMS(SLEEP); - switch (*key) + switch (key) { case 'q': - *running = false; return 0; + running = false; return 0; case 'p': stopped = !stopped; break; case 'w': - if (player->direction == DOWN) break; - player->direction = UP; break; + if (player.direction == DOWN) break; + player.direction = UP; break; case 'd': - if (player->direction == LEFT) break; - player->direction = RIGHT; break; + if (player.direction == LEFT) break; + player.direction = RIGHT; break; case 's': - if (player->direction == UP) break; - player->direction = DOWN; break; + if (player.direction == UP) break; + player.direction = DOWN; break; case 'a': - if (player->direction == RIGHT) break; - player->direction = LEFT; break; - } *key = 0; + if (player.direction == RIGHT) break; + player.direction = LEFT; break; + } key = 0; if (stopped) continue; - if (playerDoTick(player, food) && player->score < SIZE*SIZE - 1) + if (playerDoTick(&player, food) && player.score < SIZE*SIZE - 1) food = generateFood(player); - head_x = player->head->x; - head_y = player->head->y; + head_x = player.head->x; + head_y = player.head->y; if (head_x >= SIZE || head_x < 0 || head_y >= SIZE || head_y < 0 || playerCheckSelfCollision(player)) { - *running = false; + running = false; break; } } diff --git a/src/player.c b/src/player.c index 046ceda..2c54883 100644 --- a/src/player.c +++ b/src/player.c @@ -2,35 +2,32 @@ #include "player.h" -Player *playerCreate(Direction direction, int x, int y, int score) +void playerCreate(Player *buffer, Direction direction, int x, int y, int score) { - Player *player = (Player *)malloc(sizeof(Player)); PlayerNode *head = (PlayerNode *)malloc(sizeof(PlayerNode)); head->x = x; head->y = y; head->next = NULL; - player->tail = head; - player->head = head; - player->score = score; - player->direction = direction; - - return player; + buffer->tail = head; + buffer->head = head; + buffer->score = score; + buffer->direction = direction; } -bool playerCheckFoodCollision(Player *player, Food food) +bool playerCheckFoodCollision(Player player, Food food) { - for (PlayerNode *node = player->tail; node != NULL; node = node->next) + for (PlayerNode *node = player.tail; node != NULL; node = node->next) if (node->x == food.x && node->y == food.y) return true; return false; } -bool playerCheckSelfCollision(Player *player) +bool playerCheckSelfCollision(Player player) { PlayerNode *nodei, *nodej; - for (nodei = player->tail; nodei != NULL; nodei = nodei->next) + for (nodei = player.tail; nodei != NULL; nodei = nodei->next) for (nodej = nodei->next; nodej != NULL; nodej = nodej->next) if (nodei->x == nodej->x && nodei->y == nodej->y) return true; diff --git a/src/screen.c b/src/screen.c index 704cf5e..2e1580d 100644 --- a/src/screen.c +++ b/src/screen.c @@ -1,25 +1,24 @@ -#include "screen.h" #include #include #include -Screen *screenCreate(int width, int height, Point fill_value) +#include "screen.h" + +void screenCreate(Screen *buffer, int width, int height, Point fill_value) { Point *screen = malloc(width * height * sizeof(Point)); memset(screen, fill_value, width * height * sizeof(Point)); - Screen *out = malloc(sizeof(Screen)); - out->width = width; - out->height = height; - out->screen = screen; - return out; + buffer->width = width; + buffer->height = height; + buffer->screen = screen; } -void screenShow(Screen *screen) +void screenShow(Screen screen) { int x, y, i; - int width = screen->width; - int height = screen->height; + int width = screen.width; + int height = screen.height; Point point; for (y = 0; y < height; ++y)