Forráskód Böngészése

:fire: got rid of boost dependency

Felix Bytow 1 éve
szülő
commit
8fd3f6fcbe

+ 1 - 11
CMakeLists.txt

@@ -30,16 +30,6 @@ FetchContent_Declare(
 )
 FetchContent_MakeAvailable(SDL2 SDL2_image SDL2_ttf)
 
-set(Boost_USE_MULTITHREADED ON)
-set(Boost_USE_STATIC_LIBS ON)
-set(Boost_USE_STATIC_RUNTIME OFF)
-if (CMAKE_BUILD_TYPE STREQUAL "Debug")
-  set(Boost_USE_DEBUG_LIBS ON)
-else ()
-  set(Boost_USE_DEBUG_LIBS OFF)
-endif ()
-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
@@ -66,6 +56,7 @@ add_executable(Snake WIN32 MACOSX_BUNDLE
     game/ui/UiColor.hxx game/ui/UiColor.cxx
     game/HighScoreManager.cxx game/HighScoreManager.hxx
     ${CMAKE_CURRENT_BINARY_DIR}/Config.hxx
+    NonCopyable.hxx
 )
 
 file(GLOB assets assets/*)
@@ -82,7 +73,6 @@ target_link_libraries(Snake PRIVATE
     SDL2::SDL2 SDL2::SDL2main
     SDL2_image::SDL2_image
     SDL2_ttf::SDL2_ttf
-    Boost::headers Boost::serialization Boost::program_options
 )
 
 if (WIN32)

+ 15 - 0
NonCopyable.hxx

@@ -0,0 +1,15 @@
+#pragma once
+
+#ifndef SNAKE_NONCOPYABLE_HXX
+#define SNAKE_NONCOPYABLE_HXX
+
+class NonCopyable {
+public:
+  NonCopyable() noexcept = default;
+
+  NonCopyable(NonCopyable const&) = delete;
+
+  NonCopyable& operator=(NonCopyable const&) = delete;
+};
+
+#endif // SNAKE_NONCOPYABLE_HXX

+ 2 - 2
SDL.hxx

@@ -10,7 +10,7 @@
 #include <stdexcept>
 #include <string_view>
 
-#include <boost/noncopyable.hpp>
+#include "NonCopyable.hxx"
 
 /**
  * @class SDLError
@@ -35,7 +35,7 @@ public:
  * This class provides a convenient way to initialize SDL and clean up resources when they are no longer needed.
  * Only one instance of this class can exist at a time.
  */
-class SDL final : private boost::noncopyable {
+class SDL final : private NonCopyable {
 public:
   explicit SDL(std::uint32_t flags = SDL_INIT_EVERYTHING);
 

+ 2 - 2
SDLRenderer.hxx

@@ -5,9 +5,9 @@
 
 #include "SDLWindow.hxx"
 
-#include <boost/noncopyable.hpp>
+#include "NonCopyable.hxx"
 
-class SDLRenderer final : private boost::noncopyable {
+class SDLRenderer final : private NonCopyable {
 public:
   explicit SDLRenderer(SDLWindow& window);
 

+ 2 - 2
SDLWindow.hxx

@@ -7,9 +7,9 @@
 
 #include <string_view>
 
-#include <boost/noncopyable.hpp>
+#include "NonCopyable.hxx"
 
-class SDLWindow final : private boost::noncopyable {
+class SDLWindow final : private NonCopyable {
 public:
   SDLWindow(std::string_view title, int x, int y, int w, int h, std::uint32_t flags = 0u);
 

+ 2 - 2
game/AssetManager.hxx

@@ -13,9 +13,9 @@
 
 #include <SDL_ttf.h>
 
-#include <boost/noncopyable.hpp>
+#include "../NonCopyable.hxx"
 
-class AssetManager final : private boost::noncopyable {
+class AssetManager final : private NonCopyable {
 
 public:
   explicit AssetManager(SDLRenderer& renderer);

+ 2 - 2
game/GameStateManager.hxx

@@ -13,7 +13,7 @@
 
 #include <stack>
 
-#include <boost/noncopyable.hpp>
+#include "../NonCopyable.hxx"
 
 class SDLWindow;
 
@@ -27,7 +27,7 @@ enum class GameStates {
   Credits,
 };
 
-class GameStateManager final : private boost::noncopyable {
+class GameStateManager final : private NonCopyable {
 public:
   explicit GameStateManager(SDLWindow& window);
 

+ 19 - 7
game/HighScoreManager.cxx

@@ -2,12 +2,10 @@
 
 #include <SDL.h>
 
-#include <boost/archive/binary_iarchive.hpp>
-#include <boost/archive/binary_oarchive.hpp>
-
 #include <algorithm>
 #include <filesystem>
 #include <fstream>
+#include <numeric>
 
 namespace fs = std::filesystem;
 
@@ -28,8 +26,16 @@ void HighScoreManager::load()
     return;
   }
 
-  boost::archive::binary_iarchive archive{input};
-  archive >> high_score_;
+  std::size_t count;
+  input.read(std::bit_cast<char*>(&count), sizeof(std::size_t));
+  high_score_.scores_.resize(count);
+  for (std::size_t n = 0; n<count; ++n) {
+    std::size_t len;
+    input.read(std::bit_cast<char*>(&len), sizeof(std::size_t));
+    high_score_.scores_[n].player_name_.resize(len);
+    input.read(high_score_.scores_[n].player_name_.data(), static_cast<std::streamsize>(len));
+    input.read(std::bit_cast<char*>(&(high_score_.scores_[n].points_)), sizeof(unsigned));
+  }
   std::ranges::stable_sort(high_score_.scores_, std::greater<>{}, &Score::points_);
 
   if (high_score_.scores_.empty()) {
@@ -52,8 +58,14 @@ void HighScoreManager::save() const
     return;
   }
 
-  boost::archive::binary_oarchive archive{output};
-  archive << high_score_;
+  auto const count = high_score_.scores_.size();
+  output.write(std::bit_cast<char const*>(&count), sizeof(std::size_t));
+  for (auto const& score: high_score_.scores_) {
+    auto const len = score.player_name_.length();
+    output.write(std::bit_cast<char const*>(&len), sizeof(std::size_t));
+    output.write(score.player_name_.data(), static_cast<std::streamsize>(score.player_name_.length()));
+    output.write(std::bit_cast<char const*>(&score.points_), sizeof(unsigned));
+  }
   if (high_score_.scores_.empty()) {
     SDL_Log("No high scores saved.");
   }

+ 2 - 23
game/HighScoreManager.hxx

@@ -3,11 +3,7 @@
 #ifndef SNAKE_HIGHSCOREMANAGER_HXX
 #define SNAKE_HIGHSCOREMANAGER_HXX
 
-#include <boost/noncopyable.hpp>
-
-#include <boost/serialization/string.hpp>
-#include <boost/serialization/vector.hpp>
-#include <boost/serialization/version.hpp>
+#include "../NonCopyable.hxx"
 
 #include <optional>
 #include <string>
@@ -16,30 +12,13 @@
 struct Score final {
   std::string player_name_;
   unsigned points_;
-
-  template<typename Archive>
-  void serialize(Archive& archive, unsigned const version)
-  {
-    (void) version;
-    archive & player_name_;
-    archive & points_;
-  }
 };
-BOOST_CLASS_VERSION(Score, 0);
 
 struct HighScores final {
   std::vector<Score> scores_;
-
-  template<typename Archive>
-  void serialize(Archive& archive, unsigned const version)
-  {
-    (void) version;
-    archive & scores_;
-  }
 };
-BOOST_CLASS_VERSION(HighScores, 0);
 
-class HighScoreManager final : private boost::noncopyable {
+class HighScoreManager final : private NonCopyable {
 public:
   static int constexpr MAX_SCORES = 10;
 

+ 3 - 6
game/PlayingState.cxx

@@ -9,17 +9,14 @@
 #include <random>
 #include <unordered_set>
 
-#include <boost/functional/hash.hpp>
-
 namespace std {
   template<>
   struct hash<SDL_Point> {
     std::size_t operator()(SDL_Point const& p) const noexcept
     {
-      std::size_t seed{0u};
-      boost::hash_combine(seed, p.x);
-      boost::hash_combine(seed, p.y);
-      return seed;
+      static_assert(sizeof(std::size_t)==8u);
+      static_assert(sizeof(int)==4u);
+      return (static_cast<std::size_t>(p.x) << 32u) | static_cast<std::size_t>(p.y);
     }
   };
 }

+ 2 - 2
game/ui/Button.hxx

@@ -9,12 +9,12 @@
 #include <functional>
 #include <string>
 
-#include <boost/noncopyable.hpp>
+#include "../../NonCopyable.hxx"
 
 #include "UiColor.hxx"
 #include "../AssetManager.hxx"
 
-class Button final : private boost::noncopyable {
+class Button final : private NonCopyable {
 public:
   static int constexpr MIN_WIDTH = 12;
   static int constexpr MIN_HEIGHT = 14;

+ 2 - 2
game/ui/LineInput.hxx

@@ -5,11 +5,11 @@
 
 #include <chrono>
 
-#include <boost/noncopyable.hpp>
+#include "../../NonCopyable.hxx"
 
 #include "../AssetManager.hxx"
 
-class LineInput final : private boost::noncopyable {
+class LineInput final : private NonCopyable {
 public:
   static int constexpr MIN_WIDTH = 16;
   static int constexpr MIN_HEIGHT = 15;