RmlUi_Backend_SDL_SDLrenderer.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. // Touch events are handled natively, no need to generate synthetic mouse events for touch devices.
  60. SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "0");
  61. #if defined RMLUI_BACKEND_SIMULATE_TOUCH
  62. // Simulate touch events from mouse events for testing touch behavior on a desktop machine.
  63. SDL_SetHint(SDL_HINT_MOUSE_TOUCH_EVENTS, "1");
  64. #endif
  65. #if SDL_MAJOR_VERSION >= 3
  66. const float window_size_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay());
  67. SDL_PropertiesID props = SDL_CreateProperties();
  68. SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, window_name);
  69. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, SDL_WINDOWPOS_CENTERED);
  70. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, SDL_WINDOWPOS_CENTERED);
  71. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, int(width * window_size_scale));
  72. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, int(height * window_size_scale));
  73. SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_RESIZABLE_BOOLEAN, allow_resize);
  74. SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN, true);
  75. SDL_Window* window = SDL_CreateWindowWithProperties(props);
  76. SDL_DestroyProperties(props);
  77. #else
  78. const Uint32 window_flags = (allow_resize ? SDL_WINDOW_RESIZABLE : 0);
  79. SDL_Window* window = SDL_CreateWindow(window_name, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, window_flags);
  80. // 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.
  81. SDL_StopTextInput();
  82. #endif
  83. if (!window)
  84. {
  85. Rml::Log::Message(Rml::Log::LT_ERROR, "SDL error on create window: %s\n", SDL_GetError());
  86. return false;
  87. }
  88. // Force a specific SDL renderer
  89. // SDL_SetHint(SDL_HINT_RENDER_DRIVER, "software");
  90. // SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengles2");
  91. // SDL_SetHint(SDL_HINT_RENDER_DRIVER, "direct3d");
  92. // SDL_SetHint(SDL_HINT_RENDER_DRIVER, "direct3d12");
  93. // SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
  94. // SDL_SetHint(SDL_HINT_RENDER_DRIVER, "vulkan");
  95. #if SDL_MAJOR_VERSION >= 3
  96. SDL_Renderer* renderer = SDL_CreateRenderer(window, nullptr);
  97. #else
  98. SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
  99. #endif
  100. if (!renderer)
  101. return false;
  102. data = Rml::MakeUnique<BackendData>(renderer);
  103. data->window = window;
  104. data->renderer = renderer;
  105. data->system_interface.SetWindow(window);
  106. const char* renderer_name = nullptr;
  107. Rml::String available_renderers;
  108. #if SDL_MAJOR_VERSION >= 3
  109. SDL_SetRenderVSync(renderer, 1);
  110. renderer_name = SDL_GetRendererName(renderer);
  111. for (int i = 0; i < SDL_GetNumRenderDrivers(); i++)
  112. {
  113. if (i > 0)
  114. available_renderers += ", ";
  115. available_renderers += SDL_GetRenderDriver(i);
  116. }
  117. #else
  118. SDL_RendererInfo renderer_info;
  119. if (SDL_GetRendererInfo(renderer, &renderer_info) == 0)
  120. renderer_name = renderer_info.name;
  121. #endif
  122. if (!available_renderers.empty())
  123. data->system_interface.LogMessage(Rml::Log::LT_INFO, Rml::CreateString("Available SDL renderers: %s", available_renderers.c_str()));
  124. if (renderer_name)
  125. data->system_interface.LogMessage(Rml::Log::LT_INFO, Rml::CreateString("Using SDL renderer: %s", renderer_name));
  126. return true;
  127. }
  128. void Backend::Shutdown()
  129. {
  130. RMLUI_ASSERT(data);
  131. SDL_DestroyRenderer(data->renderer);
  132. SDL_DestroyWindow(data->window);
  133. data.reset();
  134. SDL_Quit();
  135. }
  136. Rml::SystemInterface* Backend::GetSystemInterface()
  137. {
  138. RMLUI_ASSERT(data);
  139. return &data->system_interface;
  140. }
  141. Rml::RenderInterface* Backend::GetRenderInterface()
  142. {
  143. RMLUI_ASSERT(data);
  144. return &data->render_interface;
  145. }
  146. bool Backend::ProcessEvents(Rml::Context* context, KeyDownCallback key_down_callback, bool power_save)
  147. {
  148. RMLUI_ASSERT(data && context);
  149. #if SDL_MAJOR_VERSION >= 3
  150. auto GetKey = [](const SDL_Event& event) { return event.key.key; };
  151. auto GetDisplayScale = []() { return SDL_GetWindowDisplayScale(data->window); };
  152. constexpr auto event_quit = SDL_EVENT_QUIT;
  153. constexpr auto event_key_down = SDL_EVENT_KEY_DOWN;
  154. bool has_event = false;
  155. #else
  156. auto GetKey = [](const SDL_Event& event) { return event.key.keysym.sym; };
  157. auto GetDisplayScale = []() { return 1.f; };
  158. constexpr auto event_quit = SDL_QUIT;
  159. constexpr auto event_key_down = SDL_KEYDOWN;
  160. int has_event = 0;
  161. #endif
  162. bool result = data->running;
  163. data->running = true;
  164. SDL_Event ev;
  165. if (power_save)
  166. has_event = SDL_WaitEventTimeout(&ev, static_cast<int>(Rml::Math::Min(context->GetNextUpdateDelay(), 10.0) * 1000));
  167. else
  168. has_event = SDL_PollEvent(&ev);
  169. while (has_event)
  170. {
  171. bool propagate_event = true;
  172. switch (ev.type)
  173. {
  174. case event_quit:
  175. {
  176. propagate_event = false;
  177. result = false;
  178. }
  179. break;
  180. case event_key_down:
  181. {
  182. propagate_event = false;
  183. const Rml::Input::KeyIdentifier key = RmlSDL::ConvertKey(GetKey(ev));
  184. const int key_modifier = RmlSDL::GetKeyModifierState();
  185. const float native_dp_ratio = GetDisplayScale();
  186. // See if we have any global shortcuts that take priority over the context.
  187. if (key_down_callback && !key_down_callback(context, key, key_modifier, native_dp_ratio, true))
  188. break;
  189. // Otherwise, hand the event over to the context by calling the input handler as normal.
  190. if (!RmlSDL::InputEventHandler(context, data->window, ev))
  191. break;
  192. // The key was not consumed by the context either, try keyboard shortcuts of lower priority.
  193. if (key_down_callback && !key_down_callback(context, key, key_modifier, native_dp_ratio, false))
  194. break;
  195. }
  196. break;
  197. default: break;
  198. }
  199. if (propagate_event)
  200. RmlSDL::InputEventHandler(context, data->window, ev);
  201. has_event = SDL_PollEvent(&ev);
  202. }
  203. return result;
  204. }
  205. void Backend::RequestExit()
  206. {
  207. RMLUI_ASSERT(data);
  208. data->running = false;
  209. }
  210. void Backend::BeginFrame()
  211. {
  212. RMLUI_ASSERT(data);
  213. data->render_interface.BeginFrame();
  214. }
  215. void Backend::PresentFrame()
  216. {
  217. RMLUI_ASSERT(data);
  218. data->render_interface.EndFrame();
  219. SDL_RenderPresent(data->renderer);
  220. }