RmlUi_Backend_SDL_SDLrenderer.cpp 5.1 KB

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