Bladeren bron

:sparkles: setup and teardown SDL2

Felix Bytow 1 jaar geleden
bovenliggende
commit
ee6003a5ae
4 gewijzigde bestanden met toevoegingen van 105 en 4 verwijderingen
  1. 3 2
      CMakeLists.txt
  2. 39 0
      SDL.cxx
  3. 52 0
      SDL.hxx
  4. 11 2
      main.cxx

+ 3 - 2
CMakeLists.txt

@@ -6,7 +6,6 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
 set(CMAKE_CXX_EXTENSIONS OFF)
 
 include(FetchContent)
-
 FetchContent_Declare(
     SDL2
     GIT_REPOSITORY https://github.com/libsdl-org/SDL.git
@@ -27,7 +26,9 @@ FetchContent_Declare(
 )
 FetchContent_MakeAvailable(SDL2 SDL2_image SDL2_ttf)
 
-add_executable(Snake WIN32 main.cxx)
+add_executable(Snake WIN32 main.cxx
+    SDL.cxx
+    SDL.hxx)
 
 target_link_libraries(Snake PRIVATE
     SDL2::SDL2 SDL2::SDL2main

+ 39 - 0
SDL.cxx

@@ -0,0 +1,39 @@
+#include "SDL.hxx"
+
+#include <cassert>
+#include <sstream>
+
+SDLError::SDLError(std::string_view const context, std::source_location location)
+    :std::runtime_error{(std::ostringstream{}
+    << context << " at "
+    << location.file_name() << ":" << location.line() << ":" << location.column()
+    << " - " << SDL_GetError()
+                        ).str()}
+{
+}
+
+SDL* SDL::instance_ = nullptr;
+
+SDL::SDL(std::uint32_t const flags)
+{
+  assert(instance_==nullptr);
+  if (SDL_Init(flags)!=0) {
+    throw SDLError("Failed to initialize SDL");
+  }
+  SDL_Log("Initialized SDL successfully.");
+  instance_ = this;
+}
+
+SDL::~SDL() noexcept
+{
+  assert(instance_!=nullptr);
+  SDL_Quit();
+  SDL_Log("Shut down SDL successfully.");
+  instance_ = nullptr;
+}
+
+SDL& SDL::instance() noexcept
+{
+  assert(instance_!=nullptr);
+  return *instance_;
+}

+ 52 - 0
SDL.hxx

@@ -0,0 +1,52 @@
+#pragma once
+
+#ifndef SNAKE_SDL_HXX
+#define SNAKE_SDL_HXX
+
+#include <SDL.h>
+
+#include <cstdint>
+#include <source_location>
+#include <stdexcept>
+#include <string_view>
+
+/**
+ * @class SDLError
+ * @brief Represents an SDL error.
+ *
+ * This class is a subclass of std::runtime_error and represents an error that occurred
+ * during an SDL operation. It provides additional information such as the context where
+ * the error occurred and the source location.
+ */
+class SDLError final : public std::runtime_error {
+public:
+  explicit SDLError(
+      std::string_view context,
+      std::source_location location = std::source_location::current()
+  );
+};
+
+/**
+ * @class SDL
+ * @brief The SDL class is used to initialize and clean up the SDL library.
+ *
+ * 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 {
+public:
+  explicit SDL(std::uint32_t flags = SDL_INIT_EVERYTHING);
+
+  ~SDL() noexcept;
+
+  SDL(SDL const&) = delete;
+
+  SDL& operator=(SDL const&) = delete;
+
+  static SDL& instance() noexcept;
+
+private:
+  static SDL* instance_;
+};
+
+#endif // SNAKE_SDL_HXX

+ 11 - 2
main.cxx

@@ -1,4 +1,13 @@
-int main()
+#include "SDL.hxx"
+
+#include <cstdlib>
+
+int main(int argc, char** argv) try
 {
-  return 0;
+  SDL sdl{};
+
+  return EXIT_SUCCESS;
+} catch (std::exception const& ex) {
+  SDL_LogError(SDL_LOG_CATEGORY_ERROR, "%s", ex.what());
+  return EXIT_FAILURE;
 }