|
|
@@ -0,0 +1,173 @@
|
|
|
+#include "os.hpp"
|
|
|
+
|
|
|
+#include <SDL3/SDL.h>
|
|
|
+#include <SDL3/SDL_video.h>
|
|
|
+
|
|
|
+#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;
|
|
|
+
|
|
|
+ std::locale::global(std::locale("en_US.UTF-8"));
|
|
|
+
|
|
|
+ if (!SDL_Init(SDL_INIT_VIDEO)) {
|
|
|
+ throw std::runtime_error("Failed SDL_Init!");
|
|
|
+ }
|
|
|
+
|
|
|
+ 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) {
|
|
|
+ throw std::runtime_error("Failed SDL_CreateWindow!");
|
|
|
+ }
|
|
|
+
|
|
|
+ root_window_ctx->renderer =
|
|
|
+ SDL_CreateRenderer(root_window_ctx->handle, nullptr);
|
|
|
+ if (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_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);
|
|
|
+}
|
|
|
+
|
|
|
+OS::~OS() {
|
|
|
+ ImGui_ImplSDLRenderer3_Shutdown();
|
|
|
+ ImGui_ImplSDL3_Shutdown();
|
|
|
+ ImGui::DestroyContext();
|
|
|
+
|
|
|
+ SDL_DestroyRenderer(root_window_ctx->renderer);
|
|
|
+ SDL_DestroyWindow(root_window_ctx->handle);
|
|
|
+ SDL_Quit();
|
|
|
+}
|
|
|
+
|
|
|
+void OS::draw_begin() {
|
|
|
+ ImGui_ImplSDLRenderer3_NewFrame();
|
|
|
+ ImGui_ImplSDL3_NewFrame();
|
|
|
+ ImGui::NewFrame();
|
|
|
+}
|
|
|
+
|
|
|
+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 OS::update() {
|
|
|
+ int w, h = 0;
|
|
|
+ SDL_GetWindowSize(root_window_ctx->handle, &w, &h);
|
|
|
+ root_window_ctx->w = w;
|
|
|
+ root_window_ctx->h = h;
|
|
|
+}
|
|
|
+
|
|
|
+bool OS::update_events() {
|
|
|
+ SDL_Event event = {};
|
|
|
+ while (SDL_PollEvent(&event)) {
|
|
|
+
|
|
|
+ ImGui_ImplSDL3_ProcessEvent(&event);
|
|
|
+
|
|
|
+ switch (event.type) {
|
|
|
+ case SDL_EVENT_KEY_UP: {
|
|
|
+ if (event.key.key == SDLK_ESCAPE) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case SDL_EVENT_QUIT: {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ case SDL_EVENT_WINDOW_CLOSE_REQUESTED: {
|
|
|
+ if (event.window.windowID == SDL_GetWindowID(root_window_ctx->handle)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+void OS::sleep(double ms) { SDL_Delay((Uint32)ms * 1000); }
|
|
|
+
|
|
|
+int OS::get_root_window_width() { return root_window_ctx->w; }
|
|
|
+
|
|
|
+int OS::get_root_window_height() { return root_window_ctx->h; }
|
|
|
+
|
|
|
+double OS::get_performance_frequency() {
|
|
|
+ return (double)SDL_GetPerformanceFrequency();
|
|
|
+}
|
|
|
+
|
|
|
+double OS::get_performance_counter() {
|
|
|
+ return (double)SDL_GetPerformanceCounter();
|
|
|
+}
|
|
|
+
|
|
|
+std::string OS::get_safe_getenv(const std::string key) {
|
|
|
+ return SDL_getenv(key.c_str());
|
|
|
+}
|
|
|
+
|
|
|
+fs::path OS::get_home_config_path() {
|
|
|
+ fs::path res = {};
|
|
|
+
|
|
|
+#if defined(_WIN32)
|
|
|
+ auto config = get_safe_getenv("APPDATA");
|
|
|
+
|
|
|
+ if (config.empty()) {
|
|
|
+ throw std::runtime_error("APPDATA environment variable not found");
|
|
|
+ }
|
|
|
+
|
|
|
+ res = fs::path(config);
|
|
|
+#elif defined(__linux__)
|
|
|
+ auto config = get_safe_getenv("HOME");
|
|
|
+
|
|
|
+ if (config.empty()) {
|
|
|
+ throw std::runtime_error("HOME environment variable not found");
|
|
|
+ }
|
|
|
+
|
|
|
+ res = fs::path(config) / ".config";
|
|
|
+#endif
|
|
|
+
|
|
|
+ return res;
|
|
|
+}
|
|
|
+
|
|
|
+std::string OS::get_user_name() { return get_safe_getenv("USERNAME"); }
|
|
|
+} // namespace saura
|