- Fix bug when Make cannot create obj/platform
- Remove some mallocs
fix-mingw
Nakidai 2023-12-09 00:35:49 +03:00
parent bfaf0f0e02
commit e2d01db144
6 changed files with 55 additions and 62 deletions

View File

@ -11,16 +11,13 @@ DEFLDFLAGS = $(shell if echo "" | cc -E -dM -xc - | grep __FreeBSD__ > /dev/null
all: $(OUT) all: $(OUT)
$(OBJDIR): $(OBJDIR)/platform:
mkdir $(OBJDIR) mkdir -p $(OBJDIR)/platform
$(OBJDIR)/platform: $(OBJDIR)
mkdir $(OBJDIR)/platform
$(OBJDIR)/%.o: $(SRCDIR)/%.c $(OBJDIR)/%.o: $(SRCDIR)/%.c
$(CC) -c -o $@ $< $(CFLAGS) $(INCLUDE) $(CC) -c -o $@ $< $(CFLAGS) $(INCLUDE)
$(OUT): $(OBJDIR) $(OBJDIR)/platform $(OBJ) $(OUT): $(OBJDIR)/platform $(OBJ)
$(CC) -o $@ $(OBJ) $(LDFLAGS) $(DEFLDFLAGS) $(CC) -o $@ $(OBJ) $(LDFLAGS) $(DEFLDFLAGS)
clean: clean:

View File

@ -21,10 +21,10 @@ struct player_t
int score; 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 playerCheckSelfCollision(Player player);
bool playerCheckFoodCollision(Player *player, Food food); bool playerCheckFoodCollision(Player player, Food food);
bool playerDoTick(Player *player, Food food); bool playerDoTick(Player *player, Food food);
#endif /* __PLAYER_H__ */ #endif /* __PLAYER_H__ */

View File

@ -11,17 +11,17 @@ typedef struct screen_t
Point *screen; Point *screen;
} Screen; } Screen;
Screen *screenCreate(int width, int height, Point fill_value); void screenCreate(Screen *buffer, int width, int height, Point fill_value);
void screenShow(Screen *screen); 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));
} }

View File

@ -12,14 +12,14 @@
#include "platform/thread.h" #include "platform/thread.h"
#include "platform/screen.h" #include "platform/screen.h"
void drawPlayer(Player *player, Screen *screen) void drawPlayer(Player player, Screen screen)
{ {
PlayerNode *node; 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) = '#'; *screenGetPoint(screen, node->x, node->y) = '#';
} }
Food generateFood(Player *player) Food generateFood(Player player)
{ {
Food food; Food food;
do do
@ -32,19 +32,19 @@ Food generateFood(Player *player)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
srand((unsigned int)time(NULL)); srand((unsigned int)time(NULL));
Player *player = playerCreate(DOWN, DEFX, DEFY, 0); Player player; playerCreate(&player, DOWN, DEFX, DEFY, 0);
Screen *screen = screenCreate(SIZE, SIZE, ' '); Screen screen; screenCreate(&screen, SIZE, SIZE, ' ');
int i; int i;
int head_x, head_y; int head_x, head_y;
Food food = generateFood(player); Food food = generateFood(player);
bool *running = malloc(sizeof(bool)); *running = true; bool running = true;
int *key = malloc(sizeof(char)); *key = 0; int key = 0;
bool stopped = false; bool stopped = false;
InputArgs input_args = (InputArgs){ key, running }; InputArgs input_args = (InputArgs){ &key, &running };
threadCreate(input, &input_args); threadCreate(input, &input_args);
while (*running) while (running)
{ {
screenSet(screen, ' '); screenSet(screen, ' ');
drawPlayer(player, screen); drawPlayer(player, screen);
@ -52,37 +52,37 @@ int main(int argc, char **argv)
resetCoordinates(); resetCoordinates();
screenShow(screen); screenShow(screen);
for (i = 0; i < SIZE*2; ++i) putchar('-'); for (i = 0; i < SIZE*2; ++i) putchar('-');
printf("\nScore: %d\n", player->score); printf("\nScore: %d\n", player.score);
sleepMS(SLEEP); sleepMS(SLEEP);
switch (*key) switch (key)
{ {
case 'q': case 'q':
*running = false; return 0; running = false; return 0;
case 'p': case 'p':
stopped = !stopped; break; stopped = !stopped; break;
case 'w': case 'w':
if (player->direction == DOWN) break; if (player.direction == DOWN) break;
player->direction = UP; break; player.direction = UP; break;
case 'd': case 'd':
if (player->direction == LEFT) break; if (player.direction == LEFT) break;
player->direction = RIGHT; break; player.direction = RIGHT; break;
case 's': case 's':
if (player->direction == UP) break; if (player.direction == UP) break;
player->direction = DOWN; break; player.direction = DOWN; break;
case 'a': case 'a':
if (player->direction == RIGHT) break; if (player.direction == RIGHT) break;
player->direction = LEFT; break; player.direction = LEFT; break;
} *key = 0; } key = 0;
if (stopped) continue; if (stopped) continue;
if (playerDoTick(player, food) && player->score < SIZE*SIZE - 1) if (playerDoTick(&player, food) && player.score < SIZE*SIZE - 1)
food = generateFood(player); food = generateFood(player);
head_x = player->head->x; head_x = player.head->x;
head_y = player->head->y; head_y = player.head->y;
if (head_x >= SIZE || head_x < 0 || head_y >= SIZE || head_y < 0 || playerCheckSelfCollision(player)) if (head_x >= SIZE || head_x < 0 || head_y >= SIZE || head_y < 0 || playerCheckSelfCollision(player))
{ {
*running = false; running = false;
break; break;
} }
} }

View File

@ -2,35 +2,32 @@
#include "player.h" #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)); PlayerNode *head = (PlayerNode *)malloc(sizeof(PlayerNode));
head->x = x; head->x = x;
head->y = y; head->y = y;
head->next = NULL; head->next = NULL;
player->tail = head; buffer->tail = head;
player->head = head; buffer->head = head;
player->score = score; buffer->score = score;
player->direction = direction; buffer->direction = direction;
return player;
} }
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) if (node->x == food.x && node->y == food.y)
return true; return true;
return false; return false;
} }
bool playerCheckSelfCollision(Player *player) bool playerCheckSelfCollision(Player player)
{ {
PlayerNode *nodei, *nodej; 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) for (nodej = nodei->next; nodej != NULL; nodej = nodej->next)
if (nodei->x == nodej->x && nodei->y == nodej->y) if (nodei->x == nodej->x && nodei->y == nodej->y)
return true; return true;

View File

@ -1,25 +1,24 @@
#include "screen.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
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)); Point *screen = malloc(width * height * sizeof(Point));
memset(screen, fill_value, width * height * sizeof(Point)); memset(screen, fill_value, width * height * sizeof(Point));
Screen *out = malloc(sizeof(Screen)); buffer->width = width;
out->width = width; buffer->height = height;
out->height = height; buffer->screen = screen;
out->screen = screen;
return out;
} }
void screenShow(Screen *screen) void screenShow(Screen screen)
{ {
int x, y, i; int x, y, i;
int width = screen->width; int width = screen.width;
int height = screen->height; int height = screen.height;
Point point; Point point;
for (y = 0; y < height; ++y) for (y = 0; y < height; ++y)