RmlUi_Backend_SDL_SDLrenderer.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #include <SDL.h>
  35. /**
  36. Global data used by this backend.
  37. Lifetime governed by the calls to Backend::Initialize() and Backend::Shutdown().
  38. */
  39. struct BackendData {
  40. BackendData(SDL_Renderer* renderer) : render_interface(renderer) {}
  41. SystemInterface_SDL system_interface;
  42. RenderInterface_SDL render_interface;
  43. SDL_Window* window = nullptr;
  44. SDL_Renderer* renderer = nullptr;
  45. bool running = true;
  46. };
  47. static Rml::UniquePtr<BackendData> data;
  48. bool Backend::Initialize(const char* window_name, int width, int height, bool allow_resize)
  49. {
  50. RMLUI_ASSERT(!data);
  51. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER) != 0)
  52. return false;
  53. // Submit click events when focusing the window.
  54. SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
  55. const Uint32 window_flags = (allow_resize ? SDL_WINDOW_RESIZABLE : 0);
  56. SDL_Window* window = SDL_CreateWindow(window_name, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, window_flags);
  57. if (!window)
  58. {
  59. Rml::Log::Message(Rml::Log::LT_ERROR, "SDL error on create window: %s\n", SDL_GetError());
  60. return false;
  61. }
  62. /*
  63. * Force a specific back-end
  64. SDL_SetHint(SDL_HINT_RENDER_BATCHING, "1");
  65. SDL_SetHint(SDL_HINT_RENDER_DRIVER, "software");
  66. SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengles2");
  67. SDL_SetHint(SDL_HINT_RENDER_DRIVER, "direct3d");
  68. SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
  69. */
  70. SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
  71. if (!renderer)
  72. return false;
  73. data = Rml::MakeUnique<BackendData>(renderer);
  74. data->window = window;
  75. data->renderer = renderer;
  76. data->system_interface.SetWindow(window);
  77. SDL_RendererInfo renderer_info;
  78. if (SDL_GetRendererInfo(renderer, &renderer_info) == 0)
  79. {
  80. data->system_interface.LogMessage(Rml::Log::LT_INFO, Rml::CreateString("Using SDL renderer: %s\n", renderer_info.name));
  81. }
  82. return true;
  83. }
  84. void Backend::Shutdown()
  85. {
  86. RMLUI_ASSERT(data);
  87. SDL_DestroyRenderer(data->renderer);
  88. SDL_DestroyWindow(data->window);
  89. data.reset();
  90. SDL_Quit();
  91. }
  92. Rml::SystemInterface* Backend::GetSystemInterface()
  93. {
  94. RMLUI_ASSERT(data);
  95. return &data->system_interface;
  96. }
  97. Rml::RenderInterface* Backend::GetRenderInterface()
  98. {
  99. RMLUI_ASSERT(data);
  100. return &data->render_interface;
  101. }
  102. bool Backend::ProcessEvents(Rml::Context* context, KeyDownCallback key_down_callback, bool power_save)
  103. {
  104. RMLUI_ASSERT(data && context);
  105. bool result = data->running;
  106. SDL_Event ev;
  107. int has_event = 0;
  108. if (power_save)
  109. has_event = SDL_WaitEventTimeout(&ev, static_cast<int>(Rml::Math::Min(context->GetNextUpdateDelay(), 10.0) * 1000));
  110. else
  111. has_event = SDL_PollEvent(&ev);
  112. while (has_event)
  113. {
  114. switch (ev.type)
  115. {
  116. case SDL_QUIT:
  117. {
  118. result = false;
  119. }
  120. break;
  121. case SDL_KEYDOWN:
  122. {
  123. const Rml::Input::KeyIdentifier key = RmlSDL::ConvertKey(ev.key.keysym.sym);
  124. const int key_modifier = RmlSDL::GetKeyModifierState();
  125. const float native_dp_ratio = 1.f;
  126. // See if we have any global shortcuts that take priority over the context.
  127. if (key_down_callback && !key_down_callback(context, key, key_modifier, native_dp_ratio, true))
  128. break;
  129. // Otherwise, hand the event over to the context by calling the input handler as normal.
  130. if (!RmlSDL::InputEventHandler(context, ev))
  131. break;
  132. // The key was not consumed by the context either, try keyboard shortcuts of lower priority.
  133. if (key_down_callback && !key_down_callback(context, key, key_modifier, native_dp_ratio, false))
  134. break;
  135. }
  136. break;
  137. default:
  138. {
  139. RmlSDL::InputEventHandler(context, ev);
  140. }
  141. break;
  142. }
  143. has_event = SDL_PollEvent(&ev);
  144. }
  145. return result;
  146. }
  147. void Backend::RequestExit()
  148. {
  149. RMLUI_ASSERT(data);
  150. data->running = false;
  151. }
  152. void Backend::BeginFrame()
  153. {
  154. RMLUI_ASSERT(data);
  155. SDL_SetRenderDrawColor(data->renderer, 0, 0, 0, 0);
  156. SDL_RenderClear(data->renderer);
  157. data->render_interface.BeginFrame();
  158. }
  159. void Backend::PresentFrame()
  160. {
  161. RMLUI_ASSERT(data);
  162. data->render_interface.EndFrame();
  163. SDL_RenderPresent(data->renderer);
  164. }