0xc3 пре 1 месец
родитељ
комит
b41f4fa88c

+ 8 - 7
CMakeLists.txt

@@ -24,8 +24,9 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_DIR})
 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${OUTPUT_DIR})
 
 file(GLOB_RECURSE LIB_SOURCES
+    "${PROJECT_SOURCE_DIR}/src/saura/app/*/*.cpp"
+    "${PROJECT_SOURCE_DIR}/src/saura/core/*/*.cpp"
     "${PROJECT_SOURCE_DIR}/src/saura/main/main.cpp"
-    "${PROJECT_SOURCE_DIR}/src/saura/**/*.cpp"
 )
 
 # Создаем библиотеку
@@ -40,6 +41,8 @@ find_package(spdlog CONFIG REQUIRED)
 find_package(imgui CONFIG REQUIRED)
 find_package(glm CONFIG REQUIRED)
 find_package(cpr CONFIG REQUIRED)
+find_package(nlohmann_json CONFIG REQUIRED)
+find_package(GTest CONFIG REQUIRED)
 
 # Добавляем include-пути
 target_include_directories(${PROJECT_NAME}_lib PUBLIC
@@ -58,11 +61,9 @@ target_link_libraries(${PROJECT_NAME}_lib PUBLIC
     imgui::imgui
     glm::glm
     cpr::cpr
+    nlohmann_json::nlohmann_json
+    GTest::gtest GTest::gtest_main
 )
 
-file(GLOB CMD_PROJECTS "src/saura/cmd/*")
-foreach(cmd_path ${CMD_PROJECTS})
-    if(IS_DIRECTORY ${cmd_path} AND EXISTS ${cmd_path}/CMakeLists.txt)
-        add_subdirectory(${cmd_path})
-    endif()
-endforeach()
+add_subdirectory(src/saura/cmd/sandbox)
+add_subdirectory(src/saura/cmd/tests)

+ 1 - 1
scripts/run-debug.sh

@@ -1,3 +1,3 @@
 #!/bin/sh
 
-./build/Debug/sandbox
+./build/Debug/$1

+ 1 - 1
scripts/run-release.sh

@@ -1,3 +1,3 @@
 #!/bin/sh
 
-./build/Release/sandbox
+./build/Release/$1

+ 0 - 25
src/saura/app/app.hpp

@@ -1,25 +0,0 @@
-#ifndef SAURA_APP_HPP_
-#define SAURA_APP_HPP_
-
-#include "saura/core/os/os.hpp"
-
-#include <SDL3/SDL_render.h>
-#include <SDL3/SDL_video.h>
-#include <memory>
-
-namespace saura {
-class App {
-private:
-  std::unique_ptr<os::Context> os_ctx;
-
-  bool is_running;
-
-public:
-  App();
-  ~App();
-
-  void run();
-};
-} // namespace saura
-
-#endif

+ 4 - 1
src/saura/cmd/sandbox/CMakeLists.txt

@@ -1,6 +1,9 @@
 get_filename_component(TARGET_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME)
 
-file(GLOB SOURCES "*.cpp")
+file(GLOB SOURCES
+ "${CMAKE_CURRENT_SOURCE_DIR}/*/*.cpp"
+ "${CMAKE_CURRENT_SOURCE_DIR}/sandbox.cpp"
+)
 
 add_executable(${TARGET_NAME} ${SOURCES})
 

+ 10 - 6
src/saura/cmd/sandbox/sandbox.cpp

@@ -84,7 +84,9 @@ struct SandboxApp : AppBase {
     dep = 0;
   }
 
-  void update(double dt) override {
+  void update(double dt) override {}
+
+  void draw() override {
     ImGui::SetNextWindowPos({0.0f, 0.0f});
     ImGui::SetNextWindowSize(ImGui::GetIO().DisplaySize);
 
@@ -92,7 +94,7 @@ struct SandboxApp : AppBase {
                  ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
                      ImGuiWindowFlags_NoFocusOnAppearing |
                      ImGuiWindowFlags_NoSavedSettings);
-    ImGui::Text("%s", this->get_title().c_str());
+    // ImGui::Text("%s", this->get_title().c_str());
 
     if (ImGui::BeginTabBar("Tabs")) {
       if (ImGui::BeginTabItem("Главная")) {
@@ -457,9 +459,11 @@ struct SandboxApp : AppBase {
   }
 
   void stop() override { spdlog::info("Sandbox stop!"); }
-
-  const std::string get_title() const override { return "Sandbox (Dev)"; }
 };
-
-AppBase *g_app_base_instance = new SandboxApp();
 } // namespace saura
+
+int main() {
+  auto app = std::make_unique<saura::SandboxApp>();
+  app->run();
+  return 0;
+}

+ 3 - 3
src/saura/cmd/sandbox/server/server.cpp

@@ -17,12 +17,12 @@
 
 namespace saura {
 ServerAPI::ServerAPI() {
-  worker = std::thread([this]() { run(); });
+  // worker = std::thread([this]() { run(); });
 }
 
 ServerAPI::~ServerAPI() {
-  if (worker.joinable())
-    worker.join();
+  // if (worker.joinable())
+  //   worker.join();
 }
 
 void ServerAPI::run() {

+ 7 - 0
src/saura/cmd/tests/CMakeLists.txt

@@ -0,0 +1,7 @@
+get_filename_component(TARGET_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME)
+
+file(GLOB SOURCES "*.cpp")
+
+add_executable(${TARGET_NAME} ${SOURCES})
+
+target_link_libraries(${TARGET_NAME} PRIVATE ${PROJECT_NAME}_lib)

+ 18 - 0
src/saura/cmd/tests/tests.cpp

@@ -0,0 +1,18 @@
+#include <gtest/gtest.h>
+
+// Function to be tested
+int add(int a, int b) { return a + b; }
+
+// Define a test case (suite name) and a specific test name
+TEST(AdditionTest, PositiveNumbers) {
+  ASSERT_EQ(3, add(1, 2)); // Assertion to check for equality
+}
+
+TEST(AdditionTest, NegativeNumbers) { ASSERT_EQ(-3, add(-1, -2)); }
+
+// Main function to run all tests (often handled by the test framework in modern
+// IDE setups)
+int main(int argc, char **argv) {
+  ::testing::InitGoogleTest(&argc, argv);
+  return RUN_ALL_TESTS();
+}

+ 9 - 16
src/saura/app/app.cpp → src/saura/core/app_base/app_base.cpp

@@ -1,18 +1,12 @@
-#include "app.hpp"
-
-#include "saura/core/app_base/app_base.hpp"
+#include "app_base.hpp"
+#include <memory>
 
 namespace saura {
-App::App() {
-  is_running = true;
-
+void AppBase::run() {
   os_ctx = std::make_unique<os::Context>();
-  os_ctx->set_root_window_title(get_app_base()->get_title());
-}
 
-App::~App() {}
+  this->start();
 
-void App::run() {
   const double fps_max = 60.0;
   const double period_max = 1.0 / fps_max;
   const double perf_frequency = os_ctx->get_performance_frequency();
@@ -21,9 +15,8 @@ void App::run() {
   double begin_counter = 0.0;
   double end_counter = 0.0;
 
-  get_app_base()->start();
+  while (!is_quit) {
 
-  while (is_running) {
     double counter_elapsed = begin_counter - end_counter;
     double dt = counter_elapsed / perf_frequency;
     double fps = perf_frequency / counter_elapsed;
@@ -36,11 +29,12 @@ void App::run() {
       }
 
       if (!os_ctx->update_events()) {
-        is_running = false;
+        is_quit = true;
       }
       os_ctx->update();
+      this->update(dt);
       os_ctx->draw_begin();
-      get_app_base()->update(dt);
+      this->draw();
       os_ctx->draw_end();
 
       end_counter = begin_counter;
@@ -48,7 +42,6 @@ void App::run() {
 
     os_ctx->sleep(period_max);
   }
-
-  get_app_base()->stop();
+  this->stop();
 }
 } // namespace saura

+ 14 - 14
src/saura/core/app_base/app_base.hpp

@@ -1,33 +1,33 @@
 #ifndef SAURA_APP_BASE_HPP_
 #define SAURA_APP_BASE_HPP_
 
+#include "saura/core/os/os.hpp"
+#include <SDL3/SDL_render.h>
+#include <SDL3/SDL_video.h>
+#include <memory>
 #include <spdlog/spdlog.h>
+#include <string>
 
 namespace saura {
 struct AppBase;
 
-// Эта глобальная переменная будет переопределена линковщиком в каждом
-// cmd-проекте
-extern AppBase *g_app_base_instance;
-
 struct AppBase {
+private:
+  std::string title;
+  bool is_quit;
+  std::unique_ptr<os::Context> os_ctx;
+
+public:
   virtual ~AppBase() = default;
 
   virtual void start() {}
   virtual void update(double dt) {}
+  virtual void draw() {}
   virtual void stop() {}
-  virtual const std::string get_title() const {
-    return g_app_base_instance->get_title();
-  }
+
+  void run();
 };
 
-inline AppBase *get_app_base() {
-  if (!g_app_base_instance) {
-    spdlog::error("g_app_instance не определён!");
-    std::terminate();
-  }
-  return g_app_base_instance;
-}
 } // namespace saura
 
 #endif

+ 10 - 3
src/saura/core/memory/arena.cpp

@@ -6,18 +6,25 @@
 
 namespace saura {
 MemoryArena::MemoryArena() {
-  this->data = (uint8_t *)SDL_malloc(DEFAULT_ARENA_SIZE);
+  this->data = (uint8_t *)std::malloc(DEFAULT_ARENA_SIZE);
+  if (this->data == nullptr) {
+   spdlog::critical("Failed malloc!");
+  }
+
   this->size = DEFAULT_ARENA_SIZE;
   this->offset = 0;
 }
 
 MemoryArena::MemoryArena(uint64_t size) {
-  this->data = (uint8_t *)SDL_malloc(size);
+  this->data = (uint8_t *)std::malloc(size);
+  if (this->data == nullptr) {
+   spdlog::critical("Failed malloc!");
+  }
   this->size = size;
   this->offset = 0;
 }
 
-MemoryArena::~MemoryArena() { std::free(this->data); }
+MemoryArena::~MemoryArena() { }
 
 uint8_t *MemoryArena::push(uint64_t size) {
   if (this->offset + size > this->size) {

+ 0 - 9
src/saura/main/main.cpp

@@ -1,9 +0,0 @@
-#include "saura/app/app.hpp"
-
-int main() {
-  auto *app_ctx = new (saura::App);
-  app_ctx->run();
-  // TODO: вызывает вылет!
-  // delete app_ctx;
-  return 0;
-}

+ 15 - 14
vcpkg.json

@@ -1,16 +1,17 @@
 {
-  "$schema": "https://raw.githubusercontent.com/microsoft/vcpkg-tool/main/docs/vcpkg.schema.json",
-  "name": "bookmarks",
-  "version": "0.1.0",
-  "dependencies": [
-    {
-      "name": "imgui",
-      "features": ["sdl3-binding", "sdl3-renderer-binding"]
-    },
-    "spdlog",
-    "sdl3",
-    "glm",
-    "cpr",
-    "nlohmann-json"
-  ]
+ "$schema": "https://raw.githubusercontent.com/microsoft/vcpkg-tool/main/docs/vcpkg.schema.json",
+ "name": "bookmarks",
+ "version": "0.1.0",
+ "dependencies": [
+  {
+   "name": "imgui",
+   "features": ["sdl3-binding", "sdl3-renderer-binding"]
+  },
+  "spdlog",
+  "sdl3",
+  "glm",
+  "cpr",
+  "nlohmann-json",
+  "gtest"
+ ]
 }