From 29e63d64eea73f17161217ba8b5be2a541370440 Mon Sep 17 00:00:00 2001 From: Neirokan Date: Tue, 31 Oct 2023 06:07:03 +0300 Subject: [PATCH] cmake, config, rand, srand --- CMakeLists.txt | 23 +++++++++++++++++++++++ include/config.h.in | 3 +++ src/main.c | 4 ++-- 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 CMakeLists.txt create mode 100644 include/config.h.in diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..09c536c --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.5) +project(csnake VERSION 0.1.0 LANGUAGES C) + +if(PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR) + message(FATAL_ERROR "In-source builds are not allowed.") +endif() + +add_executable(csnake + src/main.c + src/screen.c + src/input.c + src/player.c +) + +set_target_properties(csnake PROPERTIES C_STANDARD 11) +set_target_properties(csnake PROPERTIES C_EXTENSIONS FALSE) +target_include_directories(csnake PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include) + +set(SIZE 10 CACHE STRING "Size of game field") +set(DEFX 0 CACHE STRING "Start x") +set(DEFY 0 CACHE STRING "Start y") + +configure_file(include/config.h.in ${CMAKE_CURRENT_SOURCE_DIR}/include/config.h) diff --git a/include/config.h.in b/include/config.h.in new file mode 100644 index 0000000..06a7e35 --- /dev/null +++ b/include/config.h.in @@ -0,0 +1,3 @@ +#define SIZE ${SIZE} +#define DEFX ${DEFX} +#define DEFY ${DEFY} diff --git a/src/main.c b/src/main.c index 7403ed4..944aaa8 100644 --- a/src/main.c +++ b/src/main.c @@ -22,7 +22,7 @@ Food generateFood(Player *player) Food food; do { - food = (Food){random() % SIZE, random() % SIZE}; + food = (Food){rand() % SIZE, rand() % SIZE}; } while (playerCheckFoodCollision(player, food)); return food; } @@ -34,7 +34,7 @@ void resetCoordinates(void) int main(int argc, char **argv) { - srandom(time(NULL)); + srand(time(NULL)); Player *player = playerCreate(DOWN, DEFX, DEFY, 0); Screen *screen = screenCreate(SIZE, SIZE, ' '); PlayerNode *node;