NativeWindowSdl.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Window/NativeWindowSdl.h>
  6. #include <AnKi/Util/Logger.h>
  7. #if ANKI_GR_BACKEND_VULKAN
  8. # include <SDL3/SDL_vulkan.h>
  9. #endif
  10. namespace anki {
  11. template<>
  12. template<>
  13. NativeWindow& MakeSingletonPtr<NativeWindow>::allocateSingleton<>()
  14. {
  15. ANKI_ASSERT(m_global == nullptr);
  16. m_global = new NativeWindowSdl();
  17. #if ANKI_ASSERTIONS_ENABLED
  18. ++g_singletonsAllocated;
  19. #endif
  20. return *m_global;
  21. }
  22. template<>
  23. void MakeSingletonPtr<NativeWindow>::freeSingleton()
  24. {
  25. if(m_global)
  26. {
  27. delete static_cast<NativeWindowSdl*>(m_global);
  28. m_global = nullptr;
  29. #if ANKI_ASSERTIONS_ENABLED
  30. --g_singletonsAllocated;
  31. #endif
  32. }
  33. }
  34. Error NativeWindow::init(const NativeWindowInitInfo& inf)
  35. {
  36. return static_cast<NativeWindowSdl*>(this)->initSdl(inf);
  37. }
  38. void NativeWindow::setWindowTitle(CString title)
  39. {
  40. NativeWindowSdl* self = static_cast<NativeWindowSdl*>(this);
  41. SDL_SetWindowTitle(self->m_sdlWindow, title.cstr());
  42. }
  43. NativeWindowSdl::~NativeWindowSdl()
  44. {
  45. if(m_sdlWindow)
  46. {
  47. SDL_DestroyWindow(m_sdlWindow);
  48. }
  49. SDL_QuitSubSystem(kInitSubsystems);
  50. SDL_Quit();
  51. }
  52. Error NativeWindowSdl::initSdl(const NativeWindowInitInfo& init)
  53. {
  54. #if ANKI_OS_WINDOWS
  55. // Tell windows that the app will handle scaling. Otherwise SDL_GetDesktopDisplayMode will return a resolution that has the scaling applied
  56. if(!SetProcessDPIAware())
  57. {
  58. ANKI_WIND_LOGE("SetProcessDPIAware() failed");
  59. }
  60. #endif
  61. if(!SDL_Init(kInitSubsystems))
  62. {
  63. ANKI_WIND_LOGE("SDL_Init() failed: %s", SDL_GetError());
  64. return Error::kFunctionFailed;
  65. }
  66. #if ANKI_GR_BACKEND_VULKAN
  67. if(!SDL_Vulkan_LoadLibrary(nullptr))
  68. {
  69. ANKI_WIND_LOGE("SDL_Vulkan_LoadLibrary() failed: %s", SDL_GetError());
  70. return Error::kFunctionFailed;
  71. }
  72. #endif
  73. //
  74. // Set GL attributes
  75. //
  76. ANKI_WIND_LOGI("Creating SDL window. SDL version %u.%u", SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
  77. //
  78. // Create window
  79. //
  80. U32 flags = SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS | SDL_WINDOW_HIGH_PIXEL_DENSITY;
  81. #if ANKI_GR_BACKEND_VULKAN
  82. flags |= SDL_WINDOW_VULKAN;
  83. #endif
  84. if(init.m_fullscreenDesktopRez)
  85. {
  86. flags |= SDL_WINDOW_FULLSCREEN;
  87. SDL_DisplayID display = SDL_GetPrimaryDisplay();
  88. if(!display)
  89. {
  90. ANKI_WIND_LOGE("SDL_GetPrimaryDisplay() failed: %s", SDL_GetError());
  91. return Error::kFunctionFailed;
  92. }
  93. // Alter the window size
  94. const SDL_DisplayMode* mode = SDL_GetCurrentDisplayMode(display);
  95. if(!mode)
  96. {
  97. ANKI_WIND_LOGE("SDL_GetCurrentDisplayMode() failed: %s", SDL_GetError());
  98. return Error::kFunctionFailed;
  99. }
  100. m_width = mode->w;
  101. m_height = mode->h;
  102. }
  103. else
  104. {
  105. m_width = init.m_width;
  106. m_height = init.m_height;
  107. }
  108. m_sdlWindow = SDL_CreateWindow(&init.m_title[0], m_width, m_height, flags);
  109. if(m_sdlWindow == nullptr)
  110. {
  111. ANKI_WIND_LOGE("SDL_CreateWindow() failed");
  112. return Error::kFunctionFailed;
  113. }
  114. // Final check
  115. {
  116. int w, h;
  117. SDL_GetWindowSize(m_sdlWindow, &w, &h);
  118. ANKI_ASSERT(m_width == U32(w) && m_height == U32(h));
  119. }
  120. ANKI_WIND_LOGI("SDL window created");
  121. return Error::kNone;
  122. }
  123. } // end namespace anki