RmlUi_Backend_SDL_SDLrenderer.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "RmlUi_Backend.h"
  29. #include "RmlUi_Platform_SDL.h"
  30. #include "RmlUi_Renderer_SDL.h"
  31. #include <RmlUi/Core/Context.h>
  32. #include <RmlUi/Core/Core.h>
  33. #include <RmlUi/Core/Log.h>
  34. /**
  35. Global data used by this backend.
  36. Lifetime governed by the calls to Backend::Initialize() and Backend::Shutdown().
  37. */
  38. struct BackendData {
  39. BackendData(SDL_Renderer* renderer) : render_interface(renderer) {}
  40. SystemInterface_SDL system_interface;
  41. RenderInterface_SDL render_interface;
  42. SDL_Window* window = nullptr;
  43. SDL_Renderer* renderer = nullptr;
  44. bool running = true;
  45. };
  46. static Rml::UniquePtr<BackendData> data;
  47. bool Backend::Initialize(const char* window_name, int width, int height, bool allow_resize)
  48. {
  49. RMLUI_ASSERT(!data);
  50. #if SDL_MAJOR_VERSION >= 3
  51. if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS))
  52. return false;
  53. #else
  54. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER) != 0)
  55. return false;
  56. #endif
  57. // Submit click events when focusing the window.
  58. SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
  59. #if SDL_MAJOR_VERSION >= 3
  60. const float window_size_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay());
  61. SDL_PropertiesID props = SDL_CreateProperties();
  62. SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, window_name);
  63. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, SDL_WINDOWPOS_CENTERED);
  64. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, SDL_WINDOWPOS_CENTERED);
  65. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, int(width * window_size_scale));
  66. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, int(height * window_size_scale));
  67. SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_RESIZABLE_BOOLEAN, allow_resize);
  68. SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN, true);
  69. SDL_Window* window = SDL_CreateWindowWithProperties(props);
  70. SDL_DestroyProperties(props);
  71. #else
  72. const Uint32 window_flags = (allow_resize ? SDL_WINDOW_RESIZABLE : 0);
  73. SDL_Window* window = SDL_CreateWindow(window_name, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, window_flags);
  74. // SDL2 implicitly activates text input on window creation. Turn it off for now, it will be activated again e.g. when focusing a text input field.
  75. SDL_StopTextInput();
  76. #endif
  77. if (!window)
  78. {
  79. Rml::Log::Message(Rml::Log::LT_ERROR, "SDL error on create window: %s\n", SDL_GetError());
  80. return false;
  81. }
  82. // Force a specific SDL renderer
  83. // SDL_SetHint(SDL_HINT_RENDER_DRIVER, "software");
  84. // SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengles2");
  85. // SDL_SetHint(SDL_HINT_RENDER_DRIVER, "direct3d");
  86. // SDL_SetHint(SDL_HINT_RENDER_DRIVER, "direct3d12");
  87. // SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
  88. // SDL_SetHint(SDL_HINT_RENDER_DRIVER, "vulkan");
  89. #if SDL_MAJOR_VERSION >= 3
  90. SDL_Renderer* renderer = SDL_CreateRenderer(window, nullptr);
  91. #else
  92. SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
  93. #endif
  94. if (!renderer)
  95. return false;
  96. data = Rml::MakeUnique<BackendData>(renderer);
  97. data->window = window;
  98. data->renderer = renderer;
  99. data->system_interface.SetWindow(window);
  100. const char* renderer_name = nullptr;
  101. Rml::String available_renderers;
  102. #if SDL_MAJOR_VERSION >= 3
  103. SDL_SetRenderVSync(renderer, 1);
  104. renderer_name = SDL_GetRendererName(renderer);
  105. for (int i = 0; i < SDL_GetNumRenderDrivers(); i++)
  106. {
  107. if (i > 0)
  108. available_renderers += ", ";
  109. available_renderers += SDL_GetRenderDriver(i);
  110. }
  111. #else
  112. SDL_RendererInfo renderer_info;
  113. if (SDL_GetRendererInfo(renderer, &renderer_info) == 0)
  114. renderer_name = renderer_info.name;
  115. #endif
  116. if (!available_renderers.empty())
  117. data->system_interface.LogMessage(Rml::Log::LT_INFO, Rml::CreateString("Available SDL renderers: %s", available_renderers.c_str()));
  118. if (renderer_name)
  119. data->system_interface.LogMessage(Rml::Log::LT_INFO, Rml::CreateString("Using SDL renderer: %s", renderer_name));
  120. return true;
  121. }
  122. void Backend::Shutdown()
  123. {
  124. RMLUI_ASSERT(data);
  125. SDL_DestroyRenderer(data->renderer);
  126. SDL_DestroyWindow(data->window);
  127. data.reset();
  128. SDL_Quit();
  129. }
  130. Rml::SystemInterface* Backend::GetSystemInterface()
  131. {
  132. RMLUI_ASSERT(data);
  133. return &data->system_interface;
  134. }
  135. Rml::RenderInterface* Backend::GetRenderInterface()
  136. {
  137. RMLUI_ASSERT(data);
  138. return &data->render_interface;
  139. }
  140. bool Backend::ProcessEvents(Rml::Context* context, KeyDownCallback key_down_callback, bool power_save)
  141. {
  142. RMLUI_ASSERT(data && context);
  143. #if SDL_MAJOR_VERSION >= 3
  144. auto GetKey = [](const SDL_Event& event) { return event.key.key; };
  145. auto GetDisplayScale = []() { return SDL_GetWindowDisplayScale(data->window); };
  146. constexpr auto event_quit = SDL_EVENT_QUIT;
  147. constexpr auto event_key_down = SDL_EVENT_KEY_DOWN;
  148. bool has_event = false;
  149. #else
  150. auto GetKey = [](const SDL_Event& event) { return event.key.keysym.sym; };
  151. auto GetDisplayScale = []() { return 1.f; };
  152. constexpr auto event_quit = SDL_QUIT;
  153. constexpr auto event_key_down = SDL_KEYDOWN;
  154. int has_event = 0;
  155. #endif
  156. bool result = data->running;
  157. data->running = true;
  158. SDL_Event ev;
  159. if (power_save)
  160. has_event = SDL_WaitEventTimeout(&ev, static_cast<int>(Rml::Math::Min(context->GetNextUpdateDelay(), 10.0) * 1000));
  161. else
  162. has_event = SDL_PollEvent(&ev);
  163. while (has_event)
  164. {
  165. bool propagate_event = true;
  166. switch (ev.type)
  167. {
  168. case event_quit:
  169. {
  170. propagate_event = false;
  171. result = false;
  172. }
  173. break;
  174. case event_key_down:
  175. {
  176. propagate_event = false;
  177. const Rml::Input::KeyIdentifier key = RmlSDL::ConvertKey(GetKey(ev));
  178. const int key_modifier = RmlSDL::GetKeyModifierState();
  179. const float native_dp_ratio = GetDisplayScale();
  180. // See if we have any global shortcuts that take priority over the context.
  181. if (key_down_callback && !key_down_callback(context, key, key_modifier, native_dp_ratio, true))
  182. break;
  183. // Otherwise, hand the event over to the context by calling the input handler as normal.
  184. if (!RmlSDL::InputEventHandler(context, data->window, ev))
  185. break;
  186. // The key was not consumed by the context either, try keyboard shortcuts of lower priority.
  187. if (key_down_callback && !key_down_callback(context, key, key_modifier, native_dp_ratio, false))
  188. break;
  189. }
  190. break;
  191. default: break;
  192. }
  193. if (propagate_event)
  194. RmlSDL::InputEventHandler(context, data->window, ev);
  195. has_event = SDL_PollEvent(&ev);
  196. }
  197. return result;
  198. }
  199. void Backend::RequestExit()
  200. {
  201. RMLUI_ASSERT(data);
  202. data->running = false;
  203. }
  204. void Backend::BeginFrame()
  205. {
  206. RMLUI_ASSERT(data);
  207. data->render_interface.BeginFrame();
  208. }
  209. void Backend::PresentFrame()
  210. {
  211. RMLUI_ASSERT(data);
  212. data->render_interface.EndFrame();
  213. SDL_RenderPresent(data->renderer);
  214. }