2
0

NativeWindowSdl.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright (C) 2009-2022, 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/Core/NativeWindowSdl.h>
  6. #include <AnKi/Util/Logger.h>
  7. #if ANKI_GR_BACKEND_VULKAN
  8. # include <SDL_vulkan.h>
  9. #endif
  10. namespace anki {
  11. Error NativeWindow::newInstance(const NativeWindowInitInfo& initInfo, NativeWindow*& nativeWindow)
  12. {
  13. HeapAllocator<U8> alloc(initInfo.m_allocCallback, initInfo.m_allocCallbackUserData, "NativeWindow");
  14. NativeWindowSdl* sdlwin = alloc.newInstance<NativeWindowSdl>();
  15. sdlwin->m_alloc = alloc;
  16. const Error err = sdlwin->init(initInfo);
  17. if(err)
  18. {
  19. alloc.deleteInstance(sdlwin);
  20. nativeWindow = nullptr;
  21. return err;
  22. }
  23. else
  24. {
  25. nativeWindow = sdlwin;
  26. return Error::NONE;
  27. }
  28. }
  29. void NativeWindow::deleteInstance(NativeWindow* window)
  30. {
  31. if(window)
  32. {
  33. NativeWindowSdl* self = static_cast<NativeWindowSdl*>(window);
  34. HeapAllocator<U8> alloc = self->m_alloc;
  35. alloc.deleteInstance(self);
  36. }
  37. }
  38. void NativeWindow::setWindowTitle(CString title)
  39. {
  40. NativeWindowSdl* self = static_cast<NativeWindowSdl*>(this);
  41. SDL_SetWindowTitle(self->m_window, title.cstr());
  42. }
  43. NativeWindowSdl::~NativeWindowSdl()
  44. {
  45. if(m_window)
  46. {
  47. SDL_DestroyWindow(m_window);
  48. }
  49. SDL_QuitSubSystem(INIT_SUBSYSTEMS);
  50. SDL_Quit();
  51. }
  52. Error NativeWindowSdl::init(const NativeWindowInitInfo& init)
  53. {
  54. if(SDL_Init(INIT_SUBSYSTEMS) != 0)
  55. {
  56. ANKI_CORE_LOGE("SDL_Init() failed: %s", SDL_GetError());
  57. return Error::FUNCTION_FAILED;
  58. }
  59. #if ANKI_GR_BACKEND_VULKAN
  60. if(SDL_Vulkan_LoadLibrary(nullptr))
  61. {
  62. ANKI_CORE_LOGE("SDL_Vulkan_LoadLibrary() failed: %s", SDL_GetError());
  63. return Error::FUNCTION_FAILED;
  64. }
  65. #endif
  66. //
  67. // Set GL attributes
  68. //
  69. ANKI_CORE_LOGI("Creating SDL window. SDL version %u.%u", SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
  70. //
  71. // Create window
  72. //
  73. U32 flags = 0;
  74. #if ANKI_GR_BACKEND_GL
  75. flags |= SDL_WINDOW_OPENGL;
  76. #elif ANKI_GR_BACKEND_VULKAN
  77. flags |= SDL_WINDOW_VULKAN;
  78. #endif
  79. SDL_SetHint(SDL_HINT_ALLOW_TOPMOST, "0");
  80. if(init.m_fullscreenDesktopRez)
  81. {
  82. #if ANKI_OS_WINDOWS
  83. if(init.m_exclusiveFullscreen)
  84. {
  85. flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
  86. }
  87. #elif ANKI_OS_LINUX
  88. flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
  89. #else
  90. # error See file
  91. #endif
  92. // Alter the window size
  93. SDL_DisplayMode mode;
  94. if(SDL_GetDesktopDisplayMode(0, &mode))
  95. {
  96. ANKI_CORE_LOGE("SDL_GetDesktopDisplayMode() failed: %s", SDL_GetError());
  97. return Error::FUNCTION_FAILED;
  98. }
  99. m_width = mode.w;
  100. m_height = mode.h;
  101. }
  102. else
  103. {
  104. m_width = init.m_width;
  105. m_height = init.m_height;
  106. }
  107. m_window =
  108. SDL_CreateWindow(&init.m_title[0], SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, m_width, m_height, flags);
  109. if(m_window == nullptr)
  110. {
  111. ANKI_CORE_LOGE("SDL_CreateWindow() failed");
  112. return Error::FUNCTION_FAILED;
  113. }
  114. // Final check
  115. {
  116. int w, h;
  117. SDL_GetWindowSize(m_window, &w, &h);
  118. ANKI_ASSERT(m_width == U32(w) && m_height == U32(h));
  119. }
  120. ANKI_CORE_LOGI("SDL window created");
  121. return Error::NONE;
  122. }
  123. } // end namespace anki