Ver Fonte

:sparkles: assets are now placed in MacOS application bundle

Felix Bytow há 1 ano atrás
pai
commit
b749c5f07e
2 ficheiros alterados com 20 adições e 10 exclusões
  1. 7 3
      CMakeLists.txt
  2. 13 7
      game/AssetManager.cxx

+ 7 - 3
CMakeLists.txt

@@ -52,9 +52,13 @@ add_executable(Snake WIN32 MACOSX_BUNDLE
     game/HighScoreManager.cxx game/HighScoreManager.hxx
 )
 
+file(GLOB assets assets/*)
+target_sources(Snake PRIVATE ${assets})
+set_source_files_properties(${assets} PROPERTIES MACOSX_PACKAGE_LOCATION Resources/assets)
+
 target_link_libraries(Snake PRIVATE
-    SDL2::SDL2-static SDL2::SDL2main
-    SDL2_image::SDL2_image-static
-    SDL2_ttf::SDL2_ttf-static
+    SDL2::SDL2 SDL2::SDL2main
+    SDL2_image::SDL2_image
+    SDL2_ttf::SDL2_ttf
     Boost::headers Boost::serialization Boost::program_options
 )

+ 13 - 7
game/AssetManager.cxx

@@ -1,5 +1,7 @@
 #include "AssetManager.hxx"
 
+#include <algorithm>
+#include <array>
 #include <cassert>
 #include <filesystem>
 
@@ -15,16 +17,20 @@ AssetManager::AssetManager(SDLRenderer& renderer)
   assert(instance_==nullptr);
 
   auto const current_path = fs::current_path();
-  auto const asset_directory =
-      fs::exists(current_path/"assets")
-      ? current_path/"assets"
-      : fs::exists(current_path.parent_path()/"assets")
-        ? current_path.parent_path()/"assets"
-        : fs::path{};
-  if (asset_directory.empty()) {
+  std::array const asset_paths{
+    current_path/"assets",
+    current_path.parent_path()/"assets",
+    current_path.parent_path()/"Resources"/"assets", // MacOS bundle
+  };
+
+  auto const it = std::ranges::find_if(asset_paths, [](fs::path const& p){
+    return fs::exists(p);
+  });
+  if (it == std::end(asset_paths)) {
     throw std::runtime_error("Assets directory not found.");
   }
 
+  auto const asset_directory = *it;
   total_assets_ = std::distance(fs::directory_iterator(asset_directory),
       fs::directory_iterator());