|
|
@@ -0,0 +1,132 @@
|
|
|
+#include "os.hpp"
|
|
|
+
|
|
|
+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;
|
|
|
+
|
|
|
+ ui_ctx = std::make_unique<ui::Context>(this->root_window_ctx);
|
|
|
+
|
|
|
+ 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;
|
|
|
+ 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!");
|
|
|
+ }
|
|
|
+
|
|
|
+ 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);
|
|
|
+
|
|
|
+ ui_ctx->init();
|
|
|
+
|
|
|
+ SDL_ShowWindow(this->root_window_ctx->handle);
|
|
|
+}
|
|
|
+
|
|
|
+Context::~Context() {
|
|
|
+ ui_ctx->deinit();
|
|
|
+
|
|
|
+ SDL_DestroyRenderer(this->root_window_ctx->renderer);
|
|
|
+ SDL_DestroyWindow(this->root_window_ctx->handle);
|
|
|
+ SDL_Quit();
|
|
|
+}
|
|
|
+
|
|
|
+void Context::draw_begin() { this->ui_ctx->begin(); }
|
|
|
+
|
|
|
+void Context::draw_end() { this->ui_ctx->end(); }
|
|
|
+
|
|
|
+void Context::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;
|
|
|
+}
|
|
|
+
|
|
|
+bool Context::update_events() {
|
|
|
+ SDL_Event event = {};
|
|
|
+ while (SDL_PollEvent(&event)) {
|
|
|
+ ui_ctx->update_events(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(this->root_window_ctx->handle)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+void Context::sleep(double ms) { SDL_Delay((Uint32)ms * 1000); }
|
|
|
+
|
|
|
+int Context::get_root_window_width() { return this->root_window_ctx->w; }
|
|
|
+
|
|
|
+int Context::get_root_window_height() { return this->root_window_ctx->h; }
|
|
|
+
|
|
|
+void Context::set_root_window_title(std::string title) {
|
|
|
+ SDL_SetWindowTitle(this->root_window_ctx.get()->handle, title.c_str());
|
|
|
+}
|
|
|
+
|
|
|
+double Context::get_performance_frequency() {
|
|
|
+ return (double)SDL_GetPerformanceFrequency();
|
|
|
+}
|
|
|
+
|
|
|
+double Context::get_performance_counter() {
|
|
|
+ return (double)SDL_GetPerformanceCounter();
|
|
|
+}
|
|
|
+
|
|
|
+std::string Context::get_safe_getenv(const std::string key) {
|
|
|
+ return SDL_getenv(key.c_str());
|
|
|
+}
|
|
|
+
|
|
|
+fs::path Context::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 Context::get_user_name() { return get_safe_getenv("USERNAME"); }
|
|
|
+} // namespace saura::os
|