| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- #include "platform.hpp"
- #include <SDL3/SDL.h>
- namespace saura {
- Platform *Platform::instance = nullptr;
- void Platform::init(void *window_handle) {
- window_ctx = new (Window);
- std::locale::global(std::locale("en_US.UTF-8"));
- if (!SDL_Init(SDL_INIT_VIDEO)) {
- throw std::runtime_error("Failed SDL_Init!");
- }
- // 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);
- 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();
- ui_ctx = new (UI);
- ui_ctx->init(window_ctx);
- }
- void Platform::draw_begin() { this->ui_ctx->begin(); }
- void Platform::draw_end() { this->ui_ctx->end(); }
- void Platform::update() {
- int w, h = 0;
- SDL_GetWindowSize(this->window_ctx->handle, &w, &h);
- this->window_ctx->w = w;
- this->window_ctx->h = h;
- }
- bool Platform::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->window_ctx->handle)) {
- return false;
- }
- break;
- }
- }
- }
- return true;
- }
- void Platform::sleep(double ms) { SDL_Delay((Uint32)ms * 1000); }
- int Platform::get_root_window_width() { return this->window_ctx->w; }
- int Platform::get_root_window_height() { return this->window_ctx->h; }
- void Platform::set_root_window_title(std::string title) {
- SDL_SetWindowTitle(this->window_ctx->handle, title.c_str());
- }
- double Platform::get_performance_frequency() {
- return (double)SDL_GetPerformanceFrequency();
- }
- double Platform::get_performance_counter() {
- return (double)SDL_GetPerformanceCounter();
- }
- std::string Platform::get_safe_getenv(const std::string key) {
- return SDL_getenv(key.c_str());
- }
- fs::path Platform::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 Platform::get_user_name() { return get_safe_getenv("USERNAME"); }
- } // namespace saura
|