Pārlūkot izejas kodu

:sparkles: added "make install" support and finding installed assets

Felix Bytow 1 gadu atpakaļ
vecāks
revīzija
6ab22a9a2a
3 mainītis faili ar 26 papildinājumiem un 1 dzēšanām
  1. 12 1
      CMakeLists.txt
  2. 11 0
      Config.hxx.in
  3. 3 0
      game/AssetManager.cxx

+ 12 - 1
CMakeLists.txt

@@ -4,6 +4,7 @@ project(Snake)
 set(CMAKE_CXX_STANDARD 23)
 set(CMAKE_CXX_STANDARD_REQUIRED ON)
 set(CMAKE_CXX_EXTENSIONS OFF)
+set(CMAKE_INCLUDE_CURRENT_DIR ON)
 
 include(FetchContent)
 FetchContent_Declare(
@@ -31,6 +32,12 @@ set(Boost_USE_STATIC_LIBS ON)
 set(Boost_USE_STATIC_RUNTIME OFF)
 find_package(Boost 1.74.0 REQUIRED COMPONENTS serialization program_options)
 
+configure_file(
+    ${CMAKE_CURRENT_SOURCE_DIR}/Config.hxx.in
+    ${CMAKE_CURRENT_BINARY_DIR}/Config.hxx
+    @ONLY
+)
+
 add_executable(Snake WIN32 MACOSX_BUNDLE
     main.cxx
     SDL.cxx SDL.hxx
@@ -50,6 +57,7 @@ add_executable(Snake WIN32 MACOSX_BUNDLE
     game/ui/LineInput.cxx game/ui/LineInput.hxx
     game/ui/UiColor.hxx game/ui/UiColor.cxx
     game/HighScoreManager.cxx game/HighScoreManager.hxx
+    ${CMAKE_CURRENT_BINARY_DIR}/Config.hxx
 )
 
 file(GLOB assets assets/*)
@@ -61,4 +69,7 @@ target_link_libraries(Snake PRIVATE
     SDL2_image::SDL2_image
     SDL2_ttf::SDL2_ttf
     Boost::headers Boost::serialization Boost::program_options
-)
+)
+
+install(TARGETS Snake DESTINATION bin)
+install(FILES ${assets} DESTINATION share/snake/assets)

+ 11 - 0
Config.hxx.in

@@ -0,0 +1,11 @@
+#pragma once
+
+#ifndef SNAKE_CONFIG_HXX
+#define SNAKE_CONFIG_HXX
+
+#include <filesystem>
+
+static std::filesystem::path const INSTALLED_ASSETS_PATH =
+    std::filesystem::path{"@CMAKE_INSTALL_PREFIX@"}/"share"/"snake"/"assets";
+
+#endif // SNAKE_CONFIG_HXX

+ 3 - 0
game/AssetManager.cxx

@@ -1,4 +1,5 @@
 #include "AssetManager.hxx"
+#include "Config.hxx"
 
 #include <algorithm>
 #include <array>
@@ -21,6 +22,7 @@ AssetManager::AssetManager(SDLRenderer& renderer)
     current_path/"assets",
     current_path.parent_path()/"assets",
     current_path.parent_path()/"Resources"/"assets", // MacOS bundle
+    INSTALLED_ASSETS_PATH,
   };
 
   auto const it = std::ranges::find_if(asset_paths, [](fs::path const& p){
@@ -31,6 +33,7 @@ AssetManager::AssetManager(SDLRenderer& renderer)
   }
 
   auto const asset_directory = *it;
+  SDL_Log("Loading assets from %s.", asset_directory.c_str());
   total_assets_ = std::distance(fs::directory_iterator(asset_directory),
       fs::directory_iterator());