NativeWindowSdl.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright (C) 2009-2021, 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);
  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. self->~NativeWindowSdl();
  36. alloc.getMemoryPool().free(self);
  37. }
  38. }
  39. void NativeWindow::setWindowTitle(CString title)
  40. {
  41. NativeWindowSdl* self = static_cast<NativeWindowSdl*>(this);
  42. SDL_SetWindowTitle(self->m_window, title.cstr());
  43. }
  44. NativeWindowSdl::~NativeWindowSdl()
  45. {
  46. if(m_window)
  47. {
  48. SDL_DestroyWindow(m_window);
  49. }
  50. SDL_QuitSubSystem(INIT_SUBSYSTEMS);
  51. SDL_Quit();
  52. }
  53. Error NativeWindowSdl::init(const NativeWindowInitInfo& init)
  54. {
  55. if(SDL_Init(INIT_SUBSYSTEMS) != 0)
  56. {
  57. ANKI_CORE_LOGE("SDL_Init() failed: %s", SDL_GetError());
  58. return Error::FUNCTION_FAILED;
  59. }
  60. #if ANKI_GR_BACKEND_VULKAN
  61. if(SDL_Vulkan_LoadLibrary(nullptr))
  62. {
  63. ANKI_CORE_LOGE("SDL_Vulkan_LoadLibrary() failed: %s", SDL_GetError());
  64. return Error::FUNCTION_FAILED;
  65. }
  66. #endif
  67. //
  68. // Set GL attributes
  69. //
  70. ANKI_CORE_LOGI("Creating SDL window. SDL version %u.%u", SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
  71. #if ANKI_GR_BACKEND_GL
  72. if(SDL_GL_SetAttribute(SDL_GL_RED_SIZE, init.m_rgbaBits[0])
  73. || SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, init.m_rgbaBits[1])
  74. || SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, init.m_rgbaBits[2])
  75. || SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, init.m_rgbaBits[3])
  76. || SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, init.m_depthBits)
  77. || SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, init.m_doubleBuffer))
  78. {
  79. ANKI_CORE_LOGE("SDL_GL_SetAttribute() failed");
  80. return Error::FUNCTION_FAILED;
  81. }
  82. #endif
  83. //
  84. // Create window
  85. //
  86. U32 flags = 0;
  87. #if ANKI_GR_BACKEND_GL
  88. flags |= SDL_WINDOW_OPENGL;
  89. #elif ANKI_GR_BACKEND_VULKAN
  90. flags |= SDL_WINDOW_VULKAN;
  91. #endif
  92. if(init.m_fullscreenDesktopRez)
  93. {
  94. flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
  95. // Alter the window size
  96. SDL_DisplayMode mode;
  97. if(SDL_GetDesktopDisplayMode(0, &mode))
  98. {
  99. ANKI_CORE_LOGE("SDL_GetDesktopDisplayMode() failed: %s", SDL_GetError());
  100. return Error::FUNCTION_FAILED;
  101. }
  102. m_width = mode.w;
  103. m_height = mode.h;
  104. }
  105. else
  106. {
  107. m_width = init.m_width;
  108. m_height = init.m_height;
  109. }
  110. m_window =
  111. SDL_CreateWindow(&init.m_title[0], SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, m_width, m_height, flags);
  112. if(m_window == nullptr)
  113. {
  114. ANKI_CORE_LOGE("SDL_CreateWindow() failed");
  115. return Error::FUNCTION_FAILED;
  116. }
  117. // Final check
  118. {
  119. int w, h;
  120. SDL_GetWindowSize(m_window, &w, &h);
  121. ANKI_ASSERT(m_width == U32(w) && m_height == U32(h));
  122. }
  123. ANKI_CORE_LOGI("SDL window created");
  124. return Error::NONE;
  125. }
  126. } // end namespace anki