From 499defffe83478f98b94dbfe7a6655c1d0238c2f Mon Sep 17 00:00:00 2001 From: zEuS0390 Date: Thu, 29 Aug 2024 14:24:00 +0800 Subject: [PATCH] build: generate a new header file to hold and use the dynamic project version --- CMakeLists.txt | 29 +++++++++++++++++------------ include/.gitignore | 1 + include/constants.hpp | 4 +++- include/version.hpp.in | 9 +++++++++ src/main.cpp | 3 ++- 5 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 include/.gitignore create mode 100644 include/version.hpp.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 393e0b1..666a55a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,10 +2,10 @@ cmake_minimum_required(VERSION 3.21) # Project name and version -project(snake_game_by_zEuS0390 VERSION 1.4.1) +project(CyberSerpent VERSION 1.4.1) # Options for the project -option(SNAKE_GENERATE_INSTALLER "Generate Installer (NSIS)" FALSE) +option(CYBERSERPENT_GENERATE_INSTALLER "Generate Installer (NSIS)" FALSE) option(BUILD_SHARED_LIBS "Build Shared Libs" OFF) # Set C++ standard @@ -40,31 +40,36 @@ FetchContent_MakeAvailable(SFML) if (CMAKE_SYSTEM_NAME MATCHES "Windows") # Windows specific settings add_executable(${PROJECT_NAME} WIN32 ${APP_SRC_FILES} $<$:${APP_ICON_RC}>) - target_link_libraries(${PROJECT_NAME} sfml-audio sfml-graphics sfml-window sfml-system + target_link_libraries(${PROJECT_NAME} sfml-audio sfml-graphics sfml-window sfml-system -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic) else() # Other platforms (assuming Unix-like) add_executable(${PROJECT_NAME} ${APP_SRC_FILES}) target_link_libraries(${PROJECT_NAME} sfml-audio sfml-graphics sfml-window sfml-system) -endif() +endif() + +configure_file( + "${CMAKE_SOURCE_DIR}/include/version.hpp.in" + "${CMAKE_SOURCE_DIR}/include/version.hpp" +) # Set include directories -target_include_directories(${PROJECT_NAME} PRIVATE include) +target_include_directories(${PROJECT_NAME} PRIVATE "${CMAKE_SOURCE_DIR}/include") # Copy directories and files before build add_custom_command( - TARGET ${PROJECT_NAME} + TARGET ${PROJECT_NAME} PRE_BUILD - COMMAND ${CMAKE_COMMAND} + COMMAND ${CMAKE_COMMAND} -E copy_directory ${PROJECT_SOURCE_DIR}/gfx $/gfx) add_custom_command( - TARGET ${PROJECT_NAME} + TARGET ${PROJECT_NAME} PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory ${PROJECT_SOURCE_DIR}/sfx $/sfx) add_custom_command( - TARGET ${PROJECT_NAME} + TARGET ${PROJECT_NAME} PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/november.ttf $) @@ -78,14 +83,14 @@ if (CMAKE_SYSTEM_NAME MATCHES "Windows") endif() # Generate NSIS installer if enabled -if (SNAKE_GENERATE_INSTALLER) +if (CYBERSERPENT_GENERATE_INSTALLER) install(TARGETS ${PROJECT_NAME} DESTINATION ".") install(DIRECTORY ${PROJECT_SOURCE_DIR}/gfx DESTINATION ".") install(DIRECTORY ${PROJECT_SOURCE_DIR}/sfx DESTINATION ".") - install(FILES + install(FILES ${PROJECT_SOURCE_DIR}/november.ttf ${PROJECT_SOURCE_DIR}/LICENSE - $<$:${SFML_SOURCE_DIR}/extlibs/bin/$,x64,x86>/openal32.dll> + $<$:${SFML_SOURCE_DIR}/extlibs/bin/$,x64,x86>/openal32.dll> DESTINATION ".") set(CPACK_GENERATOR "NSIS") set(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/LICENSE") diff --git a/include/.gitignore b/include/.gitignore new file mode 100644 index 0000000..d8ccf69 --- /dev/null +++ b/include/.gitignore @@ -0,0 +1 @@ +version.hpp diff --git a/include/constants.hpp b/include/constants.hpp index c01a3b1..28e28b9 100644 --- a/include/constants.hpp +++ b/include/constants.hpp @@ -2,10 +2,12 @@ #define CONSTANTS_HPP #include +#include "version.hpp" namespace constants { - constexpr const char* WINDOW_TITLE = "Snake Game"; + const std::string WINDOW_TITLE = "CyberSerpent"; + const std::string GAME_VERSION = constants::PROJECT_VERSION; constexpr unsigned int WINDOW_SIZEX = 640; constexpr unsigned int WINDOW_SIZEY = 480; constexpr unsigned int GRID_SCALE = 20; diff --git a/include/version.hpp.in b/include/version.hpp.in new file mode 100644 index 0000000..03cfe27 --- /dev/null +++ b/include/version.hpp.in @@ -0,0 +1,9 @@ +#ifndef VERSION_HPP +#define VERSION_HPP + +namespace constants { + const std::string PROJECT_VERSION = "@PROJECT_VERSION@"; +} + +#endif + diff --git a/src/main.cpp b/src/main.cpp index 49cf547..5071bd0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,10 +12,11 @@ using constants::WINDOW_TITLE; using constants::WINDOW_SIZEX; using constants::WINDOW_SIZEY; +using constants::GAME_VERSION; // Main execution of the program int main () { - SnakeGame snakeGame(WINDOW_SIZEX, WINDOW_SIZEY, WINDOW_TITLE); + SnakeGame snakeGame(WINDOW_SIZEX, WINDOW_SIZEY, WINDOW_TITLE + " " + GAME_VERSION); snakeGame.run(); return 0; }