Ver código fonte

:sparkles: added main window

Felix Bytow 1 ano atrás
pai
commit
de50f1a119
6 arquivos alterados com 97 adições e 3 exclusões
  1. 5 3
      CMakeLists.txt
  2. 8 0
      SDL.cxx
  3. 2 0
      SDL.hxx
  4. 40 0
      SDLWindow.cxx
  5. 35 0
      SDLWindow.hxx
  6. 7 0
      main.cxx

+ 5 - 3
CMakeLists.txt

@@ -26,9 +26,11 @@ FetchContent_Declare(
 )
 FetchContent_MakeAvailable(SDL2 SDL2_image SDL2_ttf)
 
-add_executable(Snake WIN32 main.cxx
-    SDL.cxx
-    SDL.hxx)
+add_executable(Snake WIN32
+    main.cxx
+    SDL.cxx SDL.hxx
+    SDLWindow.cxx SDLWindow.hxx
+)
 
 target_link_libraries(Snake PRIVATE
     SDL2::SDL2 SDL2::SDL2main

+ 8 - 0
SDL.cxx

@@ -37,3 +37,11 @@ SDL& SDL::instance() noexcept
   assert(instance_!=nullptr);
   return *instance_;
 }
+
+SDL& SDL::require(std::uint32_t const flags) noexcept
+{
+  assert((SDL_WasInit(flags) | SDL_INIT_NOPARACHUTE)==(flags | SDL_INIT_NOPARACHUTE));
+  return instance();
+}
+
+

+ 2 - 0
SDL.hxx

@@ -45,6 +45,8 @@ public:
 
   static SDL& instance() noexcept;
 
+  static SDL& require(std::uint32_t flags) noexcept;
+
 private:
   static SDL* instance_;
 };

+ 40 - 0
SDLWindow.cxx

@@ -0,0 +1,40 @@
+#include "SDLWindow.hxx"
+
+#include <algorithm>
+
+SDLWindow::SDLWindow(std::string_view title, int x, int y, int w, int h, std::uint32_t flags)
+{
+  SDL::require(SDL_INIT_VIDEO);
+  window_ = SDL_CreateWindow(title.data(), x, y, w, h, flags);
+  if (window_==nullptr) {
+    throw SDLError("Failed to create window");
+  }
+  SDL_Log("Created window successfully.");
+}
+
+SDLWindow::~SDLWindow()
+{
+  destroy();
+}
+
+void SDLWindow::destroy() noexcept
+{
+  if (window_!=nullptr) {
+    SDL_DestroyWindow(window_);
+    SDL_Log("Destroyed window successfully.");
+    window_ = nullptr;
+  }
+}
+
+SDLWindow::SDLWindow(SDLWindow&& src) noexcept
+    :window_{src.window_}
+{
+  src.window_ = nullptr;
+}
+
+SDLWindow& SDLWindow::operator=(SDLWindow&& src) noexcept
+{
+  destroy();
+  std::swap(window_, src.window_);
+  return *this;
+}

+ 35 - 0
SDLWindow.hxx

@@ -0,0 +1,35 @@
+#pragma once
+
+#ifndef SNAKE_SDLWINDOW_HXX
+#define SNAKE_SDLWINDOW_HXX
+
+#include "SDL.hxx"
+
+#include <string_view>
+
+class SDLWindow final {
+public:
+  SDLWindow(std::string_view title, int x, int y, int w, int h, std::uint32_t flags = 0u);
+
+  ~SDLWindow();
+
+  SDLWindow(SDLWindow const&) = delete;
+
+  SDLWindow& operator=(SDLWindow const&) = delete;
+
+  SDLWindow(SDLWindow&& src) noexcept;
+
+  SDLWindow& operator=(SDLWindow&& src) noexcept;
+
+  void destroy() noexcept;
+
+  operator SDL_Window*() const noexcept // NOLINT(*-explicit-constructor)
+  {
+    return window_;
+  }
+
+private:
+  SDL_Window* window_;
+};
+
+#endif // SNAKE_SDLWINDOW_HXX

+ 7 - 0
main.cxx

@@ -1,10 +1,17 @@
 #include "SDL.hxx"
+#include "SDLWindow.hxx"
 
 #include <cstdlib>
 
 int main(int argc, char** argv) try
 {
   SDL sdl{};
+  SDLWindow window{
+      "Snake",
+      SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
+      0, 0,
+      SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_BORDERLESS
+  };
 
   return EXIT_SUCCESS;
 } catch (std::exception const& ex) {