NativeWindowSdl.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. const U32 INIT_SUBSYSTEMS = SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_EVENTS | SDL_INIT_GAMECONTROLLER;
  13. Error NativeWindow::init(NativeWindowInitInfo& init, HeapAllocator<U8>& alloc)
  14. {
  15. m_alloc = alloc;
  16. m_impl = m_alloc.newInstance<NativeWindowImpl>();
  17. if(SDL_Init(INIT_SUBSYSTEMS) != 0)
  18. {
  19. ANKI_CORE_LOGE("SDL_Init() failed: %s", SDL_GetError());
  20. return Error::FUNCTION_FAILED;
  21. }
  22. #if ANKI_GR_BACKEND_VULKAN
  23. if(SDL_Vulkan_LoadLibrary(nullptr))
  24. {
  25. ANKI_CORE_LOGE("SDL_Vulkan_LoadLibrary() failed: %s", SDL_GetError());
  26. return Error::FUNCTION_FAILED;
  27. }
  28. #endif
  29. //
  30. // Set GL attributes
  31. //
  32. ANKI_CORE_LOGI("Creating SDL window. SDL version %u.%u", SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
  33. #if ANKI_GR_BACKEND_GL
  34. if(SDL_GL_SetAttribute(SDL_GL_RED_SIZE, init.m_rgbaBits[0])
  35. || SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, init.m_rgbaBits[1])
  36. || SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, init.m_rgbaBits[2])
  37. || SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, init.m_rgbaBits[3])
  38. || SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, init.m_depthBits)
  39. || SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, init.m_doubleBuffer))
  40. {
  41. ANKI_CORE_LOGE("SDL_GL_SetAttribute() failed");
  42. return Error::FUNCTION_FAILED;
  43. }
  44. #endif
  45. //
  46. // Create window
  47. //
  48. U32 flags = 0;
  49. #if ANKI_GR_BACKEND_GL
  50. flags |= SDL_WINDOW_OPENGL;
  51. #elif ANKI_GR_BACKEND_VULKAN
  52. flags |= SDL_WINDOW_VULKAN;
  53. #endif
  54. if(init.m_fullscreenDesktopRez)
  55. {
  56. flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
  57. // Alter the window size
  58. SDL_DisplayMode mode;
  59. if(SDL_GetDesktopDisplayMode(0, &mode))
  60. {
  61. ANKI_CORE_LOGE("SDL_GetDesktopDisplayMode() failed: %s", SDL_GetError());
  62. return Error::FUNCTION_FAILED;
  63. }
  64. init.m_width = mode.w;
  65. init.m_height = mode.h;
  66. }
  67. m_impl->m_window = SDL_CreateWindow(&init.m_title[0], SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
  68. init.m_width, init.m_height, flags);
  69. if(m_impl->m_window == nullptr)
  70. {
  71. ANKI_CORE_LOGE("SDL_CreateWindow() failed");
  72. return Error::FUNCTION_FAILED;
  73. }
  74. // Set the size after loading a fullscreen window
  75. if(init.m_fullscreenDesktopRez)
  76. {
  77. int w, h;
  78. SDL_GetWindowSize(m_impl->m_window, &w, &h);
  79. m_width = w;
  80. m_height = h;
  81. }
  82. else
  83. {
  84. m_width = init.m_width;
  85. m_height = init.m_height;
  86. }
  87. ANKI_CORE_LOGI("SDL window created");
  88. return Error::NONE;
  89. }
  90. void NativeWindow::destroy()
  91. {
  92. if(m_impl != nullptr)
  93. {
  94. if(m_impl->m_window)
  95. {
  96. SDL_DestroyWindow(m_impl->m_window);
  97. }
  98. SDL_QuitSubSystem(INIT_SUBSYSTEMS);
  99. SDL_Quit();
  100. }
  101. m_alloc.deleteInstance(m_impl);
  102. }
  103. } // end namespace anki