From 1c7a0ef6b2a9c17a431bbf7778023d4ea88a457e Mon Sep 17 00:00:00 2001 From: Nakidai Date: Fri, 23 Feb 2024 17:11:50 +0300 Subject: [PATCH 1/3] Probably fix #3 --- CMakeLists.txt | 5 +++++ Makefile | 8 ++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index af519c3..23897ad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,9 +17,14 @@ add_executable(csnake set_target_properties(csnake PROPERTIES C_STANDARD 11) set_target_properties(csnake PROPERTIES C_EXTENSIONS FALSE) + if(${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD") target_link_libraries(csnake pthread) +elseif(${MINGW}) + set(CMAKE_C_FLAGS "-D_UCRT") + target_link_libraries(csnake ucrt) endif() + target_include_directories(csnake PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include) set(SIZE 10 CACHE STRING "Size of game field") diff --git a/Makefile b/Makefile index 67ee4cf..7932892 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,11 @@ OBJDIR = obj SRC = main.c screen.c input.c player.c sleep.c platform/getch.c platform/game.c OBJ = $(addprefix $(OBJDIR)/,$(SRC:.c=.o)) -DEFLDFLAGS = $(shell if echo "" | cc -E -dM -xc - | grep __FreeBSD__ > /dev/null 2>&1; then echo "-lpthread"; fi) +# Link pthread on FreeBSD +DEFLDFLAGS += $(shell if echo "" | cc -E -dM -xc - | grep __FreeBSD__ > /dev/null 2>&1; then echo "-lpthread"; fi) +# Use ucrt on MinGW +DEFLDFLAGS += $(shell if echo "" | cc -E -dM -xc - | grep __MINGW32__ > /dev/null 2>&1; then echo "-lucrt"; fi) +DEFCFLAGS += $(shell if echo "" | cc -E -dM -xc - | grep __MINGW32__ > /dev/null 2>&1; then echo "-D_UCRT"; fi) all: $(OUT) @@ -15,7 +19,7 @@ $(OBJDIR)/platform: mkdir -p $(OBJDIR)/platform $(OBJDIR)/%.o: $(SRCDIR)/%.c - $(CC) -c -o -std=c11 $@ $< $(CFLAGS) $(INCLUDE) + $(CC) -c -std=c11 -o $@ $< $(CFLAGS) $(DEFCFLAGS) $(INCLUDE) $(OUT): $(OBJDIR)/platform $(OBJ) $(CC) -o $@ $(OBJ) $(LDFLAGS) $(DEFLDFLAGS) From bb55e690d0ff41af4980ce1793a9c1318d19ee11 Mon Sep 17 00:00:00 2001 From: Nakidai Date: Wed, 6 Mar 2024 23:50:27 +0300 Subject: [PATCH 2/3] Completely fix #3 --- CMakeLists.txt | 2 +- configure | 22 +++++++++++----------- include/config.h.in | 2 +- include/platform/screen.h | 2 +- src/main.c | 10 +++++----- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 23897ad..1fcb071 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,7 +27,7 @@ endif() target_include_directories(csnake PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include) -set(SIZE 10 CACHE STRING "Size of game field") +set(FIELD_SIZE 10 CACHE STRING "Size of game field") set(DEFX 0 CACHE STRING "Start x") set(DEFY 0 CACHE STRING "Start y") set(SLEEP 1000 CACHE STRING "Sleep between frames (ms)") diff --git a/configure b/configure index 58daf1f..24a587a 100755 --- a/configure +++ b/configure @@ -3,14 +3,14 @@ usage() { echo "Use environment variables to pass values: - CC - compiler (default: cc) - CFLAGS - flags for compiler - LDFLAGS - flags for linker - OUT - out file (default: csnake - SIZE - size of game field - DEFX - start x - DEFY - start y - SLEEP - sleep between frames (ms)" + CC - compiler (default: cc) + CFLAGS - flags for compiler + LDFLAGS - flags for linker + OUT - out file (default: csnake + FIELD_SIZE - size of game field + DEFX - start x + DEFY - start y + SLEEP - sleep between frames (ms)" exit 1 } @@ -28,7 +28,7 @@ CC=${CC:-cc} CFLAGS=${CFLAGS:-} LDFLAGS=${LDFLAGS:-} OUT=${OUT:-csnake} -SIZE=${SIZE:-10} +FIELD_SIZE=${FIELD_SIZE:-10} DEFX=${DEFX:-0} DEFY=${DEFY:-0} SLEEP=${SLEEP:-1000} @@ -40,7 +40,7 @@ echo "LDFLAGS: $LDFLAGS" echo "Out file: $OUT" echo echo "Code configuration:" -echo "Size: $SIZE" +echo "Field size: $FIELD_SIZE" echo "Start x: $DEFX" echo "Start y: $DEFY" echo "Sleep: $SLEEP" @@ -50,7 +50,7 @@ CFLAGS = $CFLAGS LDFLAGS = $LDFLAGS OUT = $OUT" > config.mk -echo "#define SIZE $SIZE +echo "#define FIELD_SIZE $FIELD_SIZE #define DEFX $DEFX #define DEFY $DEFY #define SLEEP $SLEEP" > include/config.h diff --git a/include/config.h.in b/include/config.h.in index 6e8c841..2bc5945 100644 --- a/include/config.h.in +++ b/include/config.h.in @@ -1,4 +1,4 @@ -#define SIZE ${SIZE} +#define FIELD_SIZE ${FIELD_SIZE} #define DEFX ${DEFX} #define DEFY ${DEFY} #define SLEEP ${SLEEP} diff --git a/include/platform/screen.h b/include/platform/screen.h index 7ee43b0..09a532f 100644 --- a/include/platform/screen.h +++ b/include/platform/screen.h @@ -2,7 +2,7 @@ #define __PLATFORM_SCREEN_H__ #ifdef _WIN32 -#include +#include #else #include #endif /* _WIN32 */ diff --git a/src/main.c b/src/main.c index 80ccd28..6bbb7ea 100644 --- a/src/main.c +++ b/src/main.c @@ -25,7 +25,7 @@ Food generateFood(Player player) Food food; do { - food = (Food){rand() % SIZE, rand() % SIZE}; + food = (Food){rand() % FIELD_SIZE, rand() % FIELD_SIZE}; } while (playerCheckFoodCollision(player, food)); return food; } @@ -36,7 +36,7 @@ int main(int argc, char **argv) platformGameInit(); Player player; playerCreate(&player, DOWN, DEFX, DEFY, 0); - Screen screen; screenCreate(&screen, SIZE, SIZE, ' '); + Screen screen; screenCreate(&screen, FIELD_SIZE, FIELD_SIZE, ' '); Food food = generateFood(player); int key = 0; @@ -53,7 +53,7 @@ int main(int argc, char **argv) resetCoordinates(); screenShow(screen); - for (int i = 0; i < SIZE*2; ++i) putchar('-'); + for (int i = 0; i < FIELD_SIZE*2; ++i) putchar('-'); printf("\nScore: %d\n", player.score); sleepMS(SLEEP); @@ -79,12 +79,12 @@ int main(int argc, char **argv) } key = 0; if (stopped) continue; - if (playerDoTick(&player, food) && player.score < SIZE*SIZE - 1) + if (playerDoTick(&player, food) && player.score < FIELD_SIZE*FIELD_SIZE - 1) food = generateFood(player); 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)) + if (head_x >= FIELD_SIZE || head_x < 0 || head_y >= FIELD_SIZE || head_y < 0 || playerCheckSelfCollision(player)) { running = false; break; From 9d6264d100d6600f9665726e21f0894389823451 Mon Sep 17 00:00:00 2001 From: Nakidai Date: Wed, 6 Mar 2024 23:56:20 +0300 Subject: [PATCH 3/3] Also register troubles between MinGW and MSVC --- include/platform/screen.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/include/platform/screen.h b/include/platform/screen.h index 09a532f..77913df 100644 --- a/include/platform/screen.h +++ b/include/platform/screen.h @@ -2,7 +2,13 @@ #define __PLATFORM_SCREEN_H__ #ifdef _WIN32 + +#ifdef __MINGW32__ #include +#else +#include +#endif /* __MINGW32__ */ + #else #include #endif /* _WIN32 */