platform.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "platform.hpp"
  2. #include <SDL3/SDL.h>
  3. namespace saura {
  4. Platform *Platform::instance = nullptr;
  5. void Platform::init(void *window_handle) {
  6. window_ctx = new (Window);
  7. std::locale::global(std::locale("en_US.UTF-8"));
  8. if (!SDL_Init(SDL_INIT_VIDEO)) {
  9. throw std::runtime_error("Failed SDL_Init!");
  10. }
  11. // GL 3.0 + GLSL 130
  12. SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
  13. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  14. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
  15. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
  16. SDL_PropertiesID props = SDL_CreateProperties();
  17. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X11_WINDOW_NUMBER,
  18. (Sint64)window_handle);
  19. window_ctx->handle = SDL_CreateWindowWithProperties(props);
  20. window_ctx->gl_ctx = SDL_GL_GetCurrentContext();
  21. ui_ctx = new (UI);
  22. ui_ctx->init(window_ctx);
  23. }
  24. void Platform::draw_begin() { this->ui_ctx->begin(); }
  25. void Platform::draw_end() { this->ui_ctx->end(); }
  26. void Platform::update() {
  27. int w, h = 0;
  28. SDL_GetWindowSize(this->window_ctx->handle, &w, &h);
  29. this->window_ctx->w = w;
  30. this->window_ctx->h = h;
  31. }
  32. bool Platform::update_events() {
  33. SDL_Event event = {};
  34. while (SDL_PollEvent(&event)) {
  35. ui_ctx->update_events(event);
  36. switch (event.type) {
  37. case SDL_EVENT_KEY_UP: {
  38. if (event.key.key == SDLK_ESCAPE) {
  39. return false;
  40. }
  41. break;
  42. }
  43. case SDL_EVENT_QUIT: {
  44. return false;
  45. }
  46. case SDL_EVENT_WINDOW_CLOSE_REQUESTED: {
  47. if (event.window.windowID == SDL_GetWindowID(this->window_ctx->handle)) {
  48. return false;
  49. }
  50. break;
  51. }
  52. }
  53. }
  54. return true;
  55. }
  56. void Platform::sleep(double ms) { SDL_Delay((Uint32)ms * 1000); }
  57. int Platform::get_root_window_width() { return this->window_ctx->w; }
  58. int Platform::get_root_window_height() { return this->window_ctx->h; }
  59. void Platform::set_root_window_title(std::string title) {
  60. SDL_SetWindowTitle(this->window_ctx->handle, title.c_str());
  61. }
  62. double Platform::get_performance_frequency() {
  63. return (double)SDL_GetPerformanceFrequency();
  64. }
  65. double Platform::get_performance_counter() {
  66. return (double)SDL_GetPerformanceCounter();
  67. }
  68. std::string Platform::get_safe_getenv(const std::string key) {
  69. return SDL_getenv(key.c_str());
  70. }
  71. fs::path Platform::get_home_config_path() {
  72. fs::path res = {};
  73. #if defined(_WIN32)
  74. auto config = get_safe_getenv("APPDATA");
  75. if (config.empty()) {
  76. throw std::runtime_error("APPDATA environment variable not found");
  77. }
  78. res = fs::path(config);
  79. #elif defined(__linux__)
  80. auto config = get_safe_getenv("HOME");
  81. if (config.empty()) {
  82. throw std::runtime_error("HOME environment variable not found");
  83. }
  84. res = fs::path(config) / ".config";
  85. #endif
  86. return res;
  87. }
  88. std::string Platform::get_user_name() { return get_safe_getenv("USERNAME"); }
  89. } // namespace saura