|
|
@@ -1,23 +1,13 @@
|
|
|
#include "os.hpp"
|
|
|
|
|
|
-#include <SDL3/SDL.h>
|
|
|
-#include <SDL3/SDL_oldnames.h>
|
|
|
-#include <SDL3/SDL_video.h>
|
|
|
+namespace saura::os {
|
|
|
+Context::Context() {
|
|
|
+ this->root_window_ctx = std::make_shared<os::Window_Context>();
|
|
|
+ this->root_window_ctx->title = "(Saura Studios) [Dev]";
|
|
|
+ this->root_window_ctx->w = 1280;
|
|
|
+ this->root_window_ctx->h = 720;
|
|
|
|
|
|
-#include <imgui.h>
|
|
|
-#include <imgui_impl_sdl3.h>
|
|
|
-#include <imgui_impl_sdlrenderer3.h>
|
|
|
-
|
|
|
-#include <filesystem>
|
|
|
-#include <locale>
|
|
|
-#include <string>
|
|
|
-
|
|
|
-namespace saura {
|
|
|
-OS::OS() {
|
|
|
- root_window_ctx = std::make_unique<Window_Context>();
|
|
|
- root_window_ctx->title = "(Saura Studios) [Dev]";
|
|
|
- root_window_ctx->w = 1280;
|
|
|
- root_window_ctx->h = 720;
|
|
|
+ ui_ctx = std::make_unique<ui::Context>(this->root_window_ctx);
|
|
|
|
|
|
std::locale::global(std::locale("en_US.UTF-8"));
|
|
|
|
|
|
@@ -26,86 +16,50 @@ OS::OS() {
|
|
|
}
|
|
|
|
|
|
SDL_WindowFlags window_flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN;
|
|
|
- root_window_ctx->handle =
|
|
|
- SDL_CreateWindow(root_window_ctx->title.c_str(), root_window_ctx->w,
|
|
|
- root_window_ctx->h, window_flags);
|
|
|
- if (root_window_ctx->handle == nullptr) {
|
|
|
+ this->root_window_ctx->handle =
|
|
|
+ SDL_CreateWindow(this->root_window_ctx->title.c_str(), this->root_window_ctx->w,
|
|
|
+ this->root_window_ctx->h, window_flags);
|
|
|
+ if (this->root_window_ctx->handle == nullptr) {
|
|
|
throw std::runtime_error("Failed SDL_CreateWindow!");
|
|
|
}
|
|
|
|
|
|
- root_window_ctx->renderer =
|
|
|
- SDL_CreateRenderer(root_window_ctx->handle, nullptr);
|
|
|
- if (root_window_ctx->renderer == nullptr) {
|
|
|
+ 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(root_window_ctx->renderer, SDL_RENDERER_VSYNC_DISABLED);
|
|
|
- SDL_SetWindowPosition(root_window_ctx->handle, SDL_WINDOWPOS_CENTERED,
|
|
|
+ SDL_SetRenderVSync(this->root_window_ctx->renderer, SDL_RENDERER_VSYNC_DISABLED);
|
|
|
+ SDL_SetWindowPosition(this->root_window_ctx->handle, SDL_WINDOWPOS_CENTERED,
|
|
|
SDL_WINDOWPOS_CENTERED);
|
|
|
- SDL_ShowWindow(root_window_ctx->handle);
|
|
|
-
|
|
|
- // Setup imgui
|
|
|
- IMGUI_CHECKVERSION();
|
|
|
- ImGui::CreateContext();
|
|
|
- ImGuiIO &io = ImGui::GetIO();
|
|
|
- (void)io;
|
|
|
- io.ConfigFlags |=
|
|
|
- ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
|
|
|
- io.ConfigFlags |=
|
|
|
- ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
|
|
|
-
|
|
|
- // Setup Dear ImGui style
|
|
|
- ImGui::StyleColorsDark();
|
|
|
-
|
|
|
- // Setup Platform/Renderer backends
|
|
|
- ImGui_ImplSDL3_InitForSDLRenderer(root_window_ctx->handle,
|
|
|
- root_window_ctx->renderer);
|
|
|
- ImGui_ImplSDLRenderer3_Init(root_window_ctx->renderer);
|
|
|
+
|
|
|
+ ui_ctx->init();
|
|
|
+
|
|
|
+ SDL_ShowWindow(this->root_window_ctx->handle);
|
|
|
}
|
|
|
|
|
|
-OS::~OS() {
|
|
|
- ImGui_ImplSDLRenderer3_Shutdown();
|
|
|
- ImGui_ImplSDL3_Shutdown();
|
|
|
- ImGui::DestroyContext();
|
|
|
+Context::~Context() {
|
|
|
+ ui_ctx->deinit();
|
|
|
|
|
|
- SDL_DestroyRenderer(root_window_ctx->renderer);
|
|
|
- SDL_DestroyWindow(root_window_ctx->handle);
|
|
|
+ SDL_DestroyRenderer(this->root_window_ctx->renderer);
|
|
|
+ SDL_DestroyWindow(this->root_window_ctx->handle);
|
|
|
SDL_Quit();
|
|
|
}
|
|
|
|
|
|
-void OS::draw_begin() {
|
|
|
- ImGui_ImplSDLRenderer3_NewFrame();
|
|
|
- ImGui_ImplSDL3_NewFrame();
|
|
|
- ImGui::NewFrame();
|
|
|
-}
|
|
|
+void Context::draw_begin() { this->ui_ctx->begin(); }
|
|
|
|
|
|
-void OS::draw_end() {
|
|
|
- ImVec4 clear_color = {0.15f, 0.15f, 0.15f, 0.0f};
|
|
|
- ImGuiIO &io = ImGui::GetIO();
|
|
|
- (void)io;
|
|
|
- ImGui::Render();
|
|
|
- SDL_SetRenderScale(root_window_ctx->renderer, io.DisplayFramebufferScale.x,
|
|
|
- io.DisplayFramebufferScale.y);
|
|
|
- SDL_SetRenderDrawColorFloat(root_window_ctx->renderer, clear_color.x,
|
|
|
- clear_color.y, clear_color.z, clear_color.w);
|
|
|
- SDL_RenderClear(root_window_ctx->renderer);
|
|
|
- ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(),
|
|
|
- root_window_ctx->renderer);
|
|
|
-
|
|
|
- SDL_RenderPresent(root_window_ctx->renderer);
|
|
|
-}
|
|
|
+void Context::draw_end() { this->ui_ctx->end(); }
|
|
|
|
|
|
-void OS::update() {
|
|
|
+void Context::update() {
|
|
|
int w, h = 0;
|
|
|
- SDL_GetWindowSize(root_window_ctx->handle, &w, &h);
|
|
|
- root_window_ctx->w = w;
|
|
|
- root_window_ctx->h = h;
|
|
|
+ SDL_GetWindowSize(this->root_window_ctx->handle, &w, &h);
|
|
|
+ this->root_window_ctx->w = w;
|
|
|
+ this->root_window_ctx->h = h;
|
|
|
}
|
|
|
|
|
|
-bool OS::update_events() {
|
|
|
+bool Context::update_events() {
|
|
|
SDL_Event event = {};
|
|
|
while (SDL_PollEvent(&event)) {
|
|
|
-
|
|
|
- ImGui_ImplSDL3_ProcessEvent(&event);
|
|
|
+ ui_ctx->update_events(event);
|
|
|
|
|
|
switch (event.type) {
|
|
|
case SDL_EVENT_KEY_UP: {
|
|
|
@@ -118,7 +72,7 @@ bool OS::update_events() {
|
|
|
return false;
|
|
|
}
|
|
|
case SDL_EVENT_WINDOW_CLOSE_REQUESTED: {
|
|
|
- if (event.window.windowID == SDL_GetWindowID(root_window_ctx->handle)) {
|
|
|
+ if (event.window.windowID == SDL_GetWindowID(this->root_window_ctx->handle)) {
|
|
|
return false;
|
|
|
}
|
|
|
break;
|
|
|
@@ -128,29 +82,29 @@ bool OS::update_events() {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-void OS::sleep(double ms) { SDL_Delay((Uint32)ms * 1000); }
|
|
|
+void Context::sleep(double ms) { SDL_Delay((Uint32)ms * 1000); }
|
|
|
|
|
|
-int OS::get_root_window_width() { return root_window_ctx->w; }
|
|
|
+int Context::get_root_window_width() { return this->root_window_ctx->w; }
|
|
|
|
|
|
-int OS::get_root_window_height() { return root_window_ctx->h; }
|
|
|
+int Context::get_root_window_height() { return this->root_window_ctx->h; }
|
|
|
|
|
|
-void OS::set_root_window_title(std::string title) {
|
|
|
- SDL_SetWindowTitle(root_window_ctx.get()->handle, title.c_str());
|
|
|
+void Context::set_root_window_title(std::string title) {
|
|
|
+ SDL_SetWindowTitle(this->root_window_ctx.get()->handle, title.c_str());
|
|
|
}
|
|
|
|
|
|
-double OS::get_performance_frequency() {
|
|
|
+double Context::get_performance_frequency() {
|
|
|
return (double)SDL_GetPerformanceFrequency();
|
|
|
}
|
|
|
|
|
|
-double OS::get_performance_counter() {
|
|
|
+double Context::get_performance_counter() {
|
|
|
return (double)SDL_GetPerformanceCounter();
|
|
|
}
|
|
|
|
|
|
-std::string OS::get_safe_getenv(const std::string key) {
|
|
|
+std::string Context::get_safe_getenv(const std::string key) {
|
|
|
return SDL_getenv(key.c_str());
|
|
|
}
|
|
|
|
|
|
-fs::path OS::get_home_config_path() {
|
|
|
+fs::path Context::get_home_config_path() {
|
|
|
fs::path res = {};
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
@@ -174,5 +128,5 @@ fs::path OS::get_home_config_path() {
|
|
|
return res;
|
|
|
}
|
|
|
|
|
|
-std::string OS::get_user_name() { return get_safe_getenv("USERNAME"); }
|
|
|
-} // namespace saura
|
|
|
+std::string Context::get_user_name() { return get_safe_getenv("USERNAME"); }
|
|
|
+} // namespace saura::os
|