0xc3 1 miesiąc temu
rodzic
commit
53b8dc883f

+ 5 - 5
CMakeLists.txt

@@ -1,5 +1,5 @@
 cmake_minimum_required(VERSION 3.15...4.0)
-project(meta_craft)
+project(metacraft)
 
 set(CMAKE_CXX_STANDARD 17)
 set(CMAKE_CXX_STANDARD_REQUIRED ON)
@@ -25,7 +25,7 @@ file(GLOB_RECURSE LIB_SOURCES
 )
 
 # Создаем библиотеку
-add_library(${PROJECT_NAME}_lib STATIC ${LIB_SOURCES})
+add_library(${PROJECT_NAME} SHARED ${LIB_SOURCES})
 
 # Пути
 set(SRC_RES_DIR "${CMAKE_SOURCE_DIR}/res")
@@ -38,17 +38,17 @@ find_package(glm CONFIG REQUIRED)
 find_package(GTest CONFIG REQUIRED)
 
 # Добавляем include-пути
-target_include_directories(${PROJECT_NAME}_lib PUBLIC
+target_include_directories(${PROJECT_NAME} PRIVATE
     ${PROJECT_SOURCE_DIR}
     ${PROJECT_SOURCE_DIR}/src
 )
 
-target_include_directories(${PROJECT_NAME}_lib PRIVATE
+target_include_directories(${PROJECT_NAME} PRIVATE
     ${PROJECT_SOURCE_DIR}
     ${PROJECT_SOURCE_DIR}/src
 )
 
-target_link_libraries(${PROJECT_NAME}_lib PUBLIC
+target_link_libraries(${PROJECT_NAME} PRIVATE
     SDL3::SDL3
     spdlog::spdlog
     imgui::imgui

+ 1 - 1
README.md

@@ -1 +1 @@
-# meta-craft
+# metacraft

+ 1 - 1
scripts/build-debug.sh

@@ -7,4 +7,4 @@ mkdir -p ./build/Debug
 ln -s $PWD/res $PWD/build/Debug/
 
 cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug -DCMAKE_TOOLCHAIN_FILE=$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake
-cmake --build build --config Debug -j $(nproc)
+cmake --build build --config Debug -j $(nproc --ignore=2)

+ 26 - 29
src/saura/core/platform/platform.cpp

@@ -1,10 +1,13 @@
 #include "platform.hpp"
 
+#include <SDL3/SDL.h>
+
 namespace saura {
-Platform::Platform(std::shared_ptr<Window> window_ctx) {
-  this->root_window_ctx = window_ctx;
 
-  ui_ctx = std::make_unique<UI>(this->root_window_ctx);
+Platform *Platform::instance = nullptr;
+
+void Platform::init(void *window_handle) {
+  window_ctx = new (Window);
 
   std::locale::global(std::locale("en_US.UTF-8"));
 
@@ -12,26 +15,20 @@ Platform::Platform(std::shared_ptr<Window> window_ctx) {
     throw std::runtime_error("Failed SDL_Init!");
   }
 
-  this->root_window_ctx->renderer =
-      SDL_CreateRenderer(this->root_window_ctx->handle, nullptr);
-  if (this->root_window_ctx->renderer == nullptr) {
-    throw std::runtime_error("Failed SDL_CreateRenderer!");
-  }
-  SDL_SetRenderVSync(this->root_window_ctx->renderer, SDL_RENDERER_VSYNC_DISABLED);
-  SDL_SetWindowPosition(this->root_window_ctx->handle, SDL_WINDOWPOS_CENTERED,
-                        SDL_WINDOWPOS_CENTERED);
+  // GL 3.0 + GLSL 130
+  SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
+  SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
+  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
+  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
 
-  ui_ctx->init();
+  SDL_PropertiesID props = SDL_CreateProperties();
+  SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X11_WINDOW_NUMBER,
+                        (Sint64)window_handle);
+  window_ctx->handle = SDL_CreateWindowWithProperties(props);
+  window_ctx->gl_ctx = SDL_GL_GetCurrentContext();
 
-  SDL_ShowWindow(this->root_window_ctx->handle);
-}
-
-Platform::~Platform() {
-  ui_ctx->deinit();
-
-  SDL_DestroyRenderer(this->root_window_ctx->renderer);
-  SDL_DestroyWindow(this->root_window_ctx->handle);
-  SDL_Quit();
+  ui_ctx = new (UI);
+  ui_ctx->init(window_ctx);
 }
 
 void Platform::draw_begin() { this->ui_ctx->begin(); }
@@ -40,9 +37,9 @@ void Platform::draw_end() { this->ui_ctx->end(); }
 
 void Platform::update() {
   int w, h = 0;
-  SDL_GetWindowSize(this->root_window_ctx->handle, &w, &h);
-  this->root_window_ctx->w = w;
-  this->root_window_ctx->h = h;
+  SDL_GetWindowSize(this->window_ctx->handle, &w, &h);
+  this->window_ctx->w = w;
+  this->window_ctx->h = h;
 }
 
 bool Platform::update_events() {
@@ -61,7 +58,7 @@ bool Platform::update_events() {
       return false;
     }
     case SDL_EVENT_WINDOW_CLOSE_REQUESTED: {
-      if (event.window.windowID == SDL_GetWindowID(this->root_window_ctx->handle)) {
+      if (event.window.windowID == SDL_GetWindowID(this->window_ctx->handle)) {
         return false;
       }
       break;
@@ -73,12 +70,12 @@ bool Platform::update_events() {
 
 void Platform::sleep(double ms) { SDL_Delay((Uint32)ms * 1000); }
 
-int Platform::get_root_window_width() { return this->root_window_ctx->w; }
+int Platform::get_root_window_width() { return this->window_ctx->w; }
 
-int Platform::get_root_window_height() { return this->root_window_ctx->h; }
+int Platform::get_root_window_height() { return this->window_ctx->h; }
 
 void Platform::set_root_window_title(std::string title) {
-  SDL_SetWindowTitle(this->root_window_ctx.get()->handle, title.c_str());
+  SDL_SetWindowTitle(this->window_ctx->handle, title.c_str());
 }
 
 double Platform::get_performance_frequency() {
@@ -118,4 +115,4 @@ fs::path Platform::get_home_config_path() {
 }
 
 std::string Platform::get_user_name() { return get_safe_getenv("USERNAME"); }
-}
+} // namespace saura

+ 15 - 9
src/saura/core/platform/platform.hpp

@@ -1,24 +1,30 @@
 #ifndef SAURA_PLATFORM_HPP_
 #define SAURA_PLATFORM_HPP_
 
-#include <SDL3/SDL.h>
-
-#include <filesystem>
-
 #include "saura/core/platform/platform_types.hpp"
 #include "saura/core/ui/ui.hpp"
 
+#include <filesystem>
+
 namespace fs = std::filesystem;
 
 namespace saura {
 class Platform {
 private:
-  std::shared_ptr<Window> root_window_ctx;
-  std::unique_ptr<UI> ui_ctx;
+  static saura::Platform *instance;
+
+  Window *window_ctx;
+  UI *ui_ctx;
 
 public:
-  Platform(std::shared_ptr<Window> window_ctx);
-  ~Platform();
+  static Platform *get() {
+    if (instance == nullptr) {
+      instance = new (Platform);
+    }
+    return instance;
+  }
+
+  void init(void *window_handle);
 
   void draw_begin();
   void draw_end();
@@ -39,6 +45,6 @@ public:
   static fs::path get_home_config_path();
   static std::string get_user_name();
 };
-} // namespace saura::os
+} // namespace saura
 
 #endif

+ 1 - 1
src/saura/core/platform/platform_types.hpp

@@ -12,7 +12,7 @@ struct Window {
   uint32_t w, h;
   std::string title;
   SDL_Window *handle;
-  SDL_Renderer *renderer;
+  SDL_GLContext gl_ctx;
 };
 } // namespace saura
 

+ 15 - 26
src/saura/core/ui/ui.cpp

@@ -1,35 +1,32 @@
 #include "ui.hpp"
 
 #include <imgui.h>
+#include <imgui_impl_opengl3.h>
 #include <imgui_impl_sdl3.h>
-#include <imgui_impl_sdlrenderer3.h>
+
+#include <SDL3/SDL_opengl.h>
 
 namespace saura {
-UI::UI(std::weak_ptr<Window> window_ctx) {
+
+void UI::init(Window *window_ctx) {
   this->window_ctx = window_ctx;
-}
 
-void UI::init() {
   IMGUI_CHECKVERSION();
   ImGui::CreateContext();
   ImGuiIO &io = ImGui::GetIO();
   (void)io;
   io.ConfigFlags |=
-      ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
-  io.ConfigFlags |=
-      ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
+      ImGuiConfigFlags_NavEnableKeyboard;
 
   // Setup Dear ImGui style
   ImGui::StyleColorsDark();
 
   // Setup Platform/Renderer backends
-  ImGui_ImplSDL3_InitForSDLRenderer(this->window_ctx.lock()->handle,
-                                    this->window_ctx.lock()->renderer);
-  ImGui_ImplSDLRenderer3_Init(this->window_ctx.lock()->renderer);
+  ImGui_ImplSDL3_InitForOpenGL(window_ctx->handle, window_ctx->gl_ctx);
+  ImGui_ImplOpenGL3_Init();
 }
 
 void UI::deinit() {
-  ImGui_ImplSDLRenderer3_Shutdown();
   ImGui_ImplSDL3_Shutdown();
   ImGui::DestroyContext();
 }
@@ -39,24 +36,16 @@ void UI::update_events(SDL_Event &event) {
 }
 
 void UI::begin() {
-  ImGui_ImplSDLRenderer3_NewFrame();
+  ImGui_ImplOpenGL3_NewFrame();
   ImGui_ImplSDL3_NewFrame();
   ImGui::NewFrame();
+
+  ImGui::ShowDemoWindow(nullptr);
 }
 
 void UI::end() {
-    ImVec4 clear_color = {0.15f, 0.15f, 0.15f, 0.0f};
-    ImGuiIO &io = ImGui::GetIO();
-    (void)io;
-    ImGui::Render();
-    SDL_SetRenderScale(this->window_ctx.lock()->renderer, io.DisplayFramebufferScale.x,
-                       io.DisplayFramebufferScale.y);
-    SDL_SetRenderDrawColorFloat(this->window_ctx.lock()->renderer, clear_color.x,
-                                clear_color.y, clear_color.z, clear_color.w);
-    SDL_RenderClear(this->window_ctx.lock()->renderer);
-    ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(),
-                                          this->window_ctx.lock()->renderer);
-
-    SDL_RenderPresent(this->window_ctx.lock()->renderer);
+  ImGui::Render();
+  ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
+  SDL_GL_SwapWindow(window_ctx->handle);
 }
-} // namespace saura::ui
+} // namespace saura

+ 2 - 6
src/saura/core/ui/ui.hpp

@@ -1,8 +1,6 @@
 #ifndef SAURA_UI_HPP_
 #define SAURA_UI_HPP_
 
-#include <memory>
-
 #include <SDL3/SDL_events.h>
 
 #include "saura/core/platform/platform_types.hpp"
@@ -10,12 +8,10 @@
 namespace saura {
 class UI {
 private:
-  std::weak_ptr<Window> window_ctx;
+  Window* window_ctx;
 
 public:
-  UI(std::weak_ptr<Window> window_ctx);
-
-  void init();
+  void init(Window* window_ctx);
   void deinit();
 
   void update_events(SDL_Event &event);

+ 1 - 1
vcpkg.json

@@ -5,7 +5,7 @@
   "dependencies": [
     {
       "name": "imgui",
-      "features": ["sdl3-binding", "sdl3-renderer-binding"]
+      "features": ["sdl3-binding", "opengl3-binding"]
     },
     "spdlog",
     "sdl3",