NativeWindowSdl.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. Display server %s", SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_GetCurrentVideoDriver());
  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_borderless)
  85. {
  86. flags |= SDL_WINDOW_BORDERLESS;
  87. }
  88. if(init.m_fullscreenDesktopRez)
  89. {
  90. if(init.m_exclusiveFullscreen)
  91. {
  92. flags |= SDL_WINDOW_FULLSCREEN;
  93. }
  94. SDL_DisplayID display = SDL_GetPrimaryDisplay();
  95. if(!display)
  96. {
  97. ANKI_WIND_LOGE("SDL_GetPrimaryDisplay() failed: %s", SDL_GetError());
  98. return Error::kFunctionFailed;
  99. }
  100. // Alter the window size
  101. const SDL_DisplayMode* mode = SDL_GetCurrentDisplayMode(display);
  102. if(!mode)
  103. {
  104. ANKI_WIND_LOGE("SDL_GetCurrentDisplayMode() failed: %s", SDL_GetError());
  105. return Error::kFunctionFailed;
  106. }
  107. m_width = U32(F32(mode->w) * mode->pixel_density);
  108. m_height = U32(F32(mode->h) * mode->pixel_density);
  109. }
  110. else
  111. {
  112. m_width = init.m_width;
  113. m_height = init.m_height;
  114. }
  115. m_sdlWindow = SDL_CreateWindow(&init.m_title[0], m_width, m_height, flags);
  116. if(m_sdlWindow == nullptr)
  117. {
  118. ANKI_WIND_LOGE("SDL_CreateWindow() failed");
  119. return Error::kFunctionFailed;
  120. }
  121. if(!SDL_ShowWindow(m_sdlWindow))
  122. {
  123. ANKI_WIND_LOGE("SDL_ShowWindow() failed: %s", SDL_GetError());
  124. }
  125. // Final check
  126. {
  127. int w, h;
  128. SDL_GetWindowSize(m_sdlWindow, &w, &h);
  129. ANKI_ASSERT(m_width == U32(w) && m_height == U32(h));
  130. }
  131. ANKI_WIND_LOGI("SDL window created");
  132. return Error::kNone;
  133. }
  134. } // end namespace anki