Skip to content

Commit

Permalink
build: generate a new header file to hold and use the dynamic project…
Browse files Browse the repository at this point in the history
… version
  • Loading branch information
zEuS0390 committed Sep 7, 2024
1 parent 4fbcbb4 commit 499deff
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
29 changes: 17 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -40,31 +40,36 @@ FetchContent_MakeAvailable(SFML)
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
# Windows specific settings
add_executable(${PROJECT_NAME} WIN32 ${APP_SRC_FILES} $<$<PLATFORM_ID:Windows>:${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 $<TARGET_FILE_DIR:${PROJECT_NAME}>/gfx)

add_custom_command(
TARGET ${PROJECT_NAME}
TARGET ${PROJECT_NAME}
PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory ${PROJECT_SOURCE_DIR}/sfx $<TARGET_FILE_DIR:${PROJECT_NAME}>/sfx)

add_custom_command(
TARGET ${PROJECT_NAME}
TARGET ${PROJECT_NAME}
PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/november.ttf $<TARGET_FILE_DIR:${PROJECT_NAME}>)

Expand All @@ -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
$<$<PLATFORM_ID:Windows>:${SFML_SOURCE_DIR}/extlibs/bin/$<IF:$<EQUAL:${CMAKE_SIZEOF_VOID_P},8>,x64,x86>/openal32.dll>
$<$<PLATFORM_ID:Windows>:${SFML_SOURCE_DIR}/extlibs/bin/$<IF:$<EQUAL:${CMAKE_SIZEOF_VOID_P},8>,x64,x86>/openal32.dll>
DESTINATION ".")
set(CPACK_GENERATOR "NSIS")
set(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/LICENSE")
Expand Down
1 change: 1 addition & 0 deletions include/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version.hpp
4 changes: 3 additions & 1 deletion include/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
#define CONSTANTS_HPP

#include <string>
#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;
Expand Down
9 changes: 9 additions & 0 deletions include/version.hpp.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef VERSION_HPP
#define VERSION_HPP

namespace constants {
const std::string PROJECT_VERSION = "@PROJECT_VERSION@";
}

#endif

3 changes: 2 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 499deff

Please sign in to comment.