- 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)
$(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:

View File

@ -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__ */

View File

@ -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));
}

View File

@ -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;
}
}

View File

@ -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;

View File

@ -1,25 +1,24 @@
#include "screen.h"
#include <stdio.h>
#include <stdlib.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));
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)