NativeWindowSdl.cpp 3.3 KB

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