2
0

RmlUi_Backend_BackwardCompatible_GLFW_GL3.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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_GLFW.h"
  30. #include "RmlUi_Renderer_BackwardCompatible_GL3.h"
  31. #include <RmlUi/Core/Context.h>
  32. #include <RmlUi/Core/Input.h>
  33. #include <RmlUi/Core/Profiling.h>
  34. #include <GLFW/glfw3.h>
  35. static void SetupCallbacks(GLFWwindow* window);
  36. static void LogErrorFromGLFW(int error, const char* description)
  37. {
  38. Rml::Log::Message(Rml::Log::LT_ERROR, "GLFW error (0x%x): %s", error, description);
  39. }
  40. /**
  41. Global data used by this backend.
  42. Lifetime governed by the calls to Backend::Initialize() and Backend::Shutdown().
  43. */
  44. struct BackendData {
  45. SystemInterface_GLFW system_interface;
  46. RenderInterface_BackwardCompatible_GL3 render_interface;
  47. GLFWwindow* window = nullptr;
  48. int glfw_active_modifiers = 0;
  49. bool context_dimensions_dirty = true;
  50. // Arguments set during event processing and nulled otherwise.
  51. Rml::Context* context = nullptr;
  52. KeyDownCallback key_down_callback = nullptr;
  53. };
  54. static Rml::UniquePtr<BackendData> data;
  55. bool Backend::Initialize(const char* name, int width, int height, bool allow_resize)
  56. {
  57. RMLUI_ASSERT(!data);
  58. glfwSetErrorCallback(LogErrorFromGLFW);
  59. if (!glfwInit())
  60. return false;
  61. // Set window hints for OpenGL 3.3 Core context creation.
  62. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  63. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  64. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  65. glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_TRUE);
  66. // Request stencil buffer of at least 8-bit size to supporting clipping on transformed elements.
  67. glfwWindowHint(GLFW_STENCIL_BITS, 8);
  68. // Enable MSAA for better-looking visuals, especially when transforms are applied.
  69. glfwWindowHint(GLFW_SAMPLES, 2);
  70. // Apply window properties and create it.
  71. glfwWindowHint(GLFW_RESIZABLE, allow_resize ? GLFW_TRUE : GLFW_FALSE);
  72. glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE);
  73. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  74. GLFWwindow* window = glfwCreateWindow(width, height, name, nullptr, nullptr);
  75. if (!window)
  76. return false;
  77. glfwMakeContextCurrent(window);
  78. glfwSwapInterval(1);
  79. // Load the OpenGL functions.
  80. Rml::String renderer_message;
  81. if (!RmlGL3::Initialize(&renderer_message))
  82. return false;
  83. // Construct the system and render interface, this includes compiling all the shaders. If this fails, it is likely an error in the shader code.
  84. data = Rml::MakeUnique<BackendData>();
  85. if (!data || !data->render_interface)
  86. return false;
  87. data->window = window;
  88. data->system_interface.SetWindow(window);
  89. data->system_interface.LogMessage(Rml::Log::LT_INFO, renderer_message);
  90. // The window size may have been scaled by DPI settings, get the actual pixel size.
  91. glfwGetFramebufferSize(window, &width, &height);
  92. data->render_interface.SetViewport(width, height);
  93. // Receive num lock and caps lock modifiers for proper handling of numpad inputs in text fields.
  94. glfwSetInputMode(window, GLFW_LOCK_KEY_MODS, GLFW_TRUE);
  95. // Setup the input and window event callback functions.
  96. SetupCallbacks(window);
  97. return true;
  98. }
  99. void Backend::Shutdown()
  100. {
  101. RMLUI_ASSERT(data);
  102. glfwDestroyWindow(data->window);
  103. data.reset();
  104. RmlGL3::Shutdown();
  105. glfwTerminate();
  106. }
  107. Rml::SystemInterface* Backend::GetSystemInterface()
  108. {
  109. RMLUI_ASSERT(data);
  110. return &data->system_interface;
  111. }
  112. Rml::RenderInterface* Backend::GetRenderInterface()
  113. {
  114. RMLUI_ASSERT(data);
  115. return data->render_interface.GetAdaptedInterface();
  116. }
  117. bool Backend::ProcessEvents(Rml::Context* context, KeyDownCallback key_down_callback, bool power_save)
  118. {
  119. RMLUI_ASSERT(data && context);
  120. // The initial window size may have been affected by system DPI settings, apply the actual pixel size and dp-ratio to the context.
  121. if (data->context_dimensions_dirty)
  122. {
  123. data->context_dimensions_dirty = false;
  124. Rml::Vector2i window_size;
  125. float dp_ratio = 1.f;
  126. glfwGetFramebufferSize(data->window, &window_size.x, &window_size.y);
  127. glfwGetWindowContentScale(data->window, &dp_ratio, nullptr);
  128. context->SetDimensions(window_size);
  129. context->SetDensityIndependentPixelRatio(dp_ratio);
  130. }
  131. data->context = context;
  132. data->key_down_callback = key_down_callback;
  133. if (power_save)
  134. glfwWaitEventsTimeout(Rml::Math::Min(context->GetNextUpdateDelay(), 10.0));
  135. else
  136. glfwPollEvents();
  137. data->context = nullptr;
  138. data->key_down_callback = nullptr;
  139. const bool result = !glfwWindowShouldClose(data->window);
  140. glfwSetWindowShouldClose(data->window, GLFW_FALSE);
  141. return result;
  142. }
  143. void Backend::RequestExit()
  144. {
  145. RMLUI_ASSERT(data);
  146. glfwSetWindowShouldClose(data->window, GLFW_TRUE);
  147. }
  148. void Backend::BeginFrame()
  149. {
  150. RMLUI_ASSERT(data);
  151. data->render_interface.BeginFrame();
  152. data->render_interface.Clear();
  153. }
  154. void Backend::PresentFrame()
  155. {
  156. RMLUI_ASSERT(data);
  157. data->render_interface.EndFrame();
  158. glfwSwapBuffers(data->window);
  159. // Optional, used to mark frames during performance profiling.
  160. RMLUI_FrameMark;
  161. }
  162. static void SetupCallbacks(GLFWwindow* window)
  163. {
  164. RMLUI_ASSERT(data);
  165. // Key input
  166. glfwSetKeyCallback(window, [](GLFWwindow* /*window*/, int glfw_key, int /*scancode*/, int glfw_action, int glfw_mods) {
  167. if (!data->context)
  168. return;
  169. // Store the active modifiers for later because GLFW doesn't provide them in the callbacks to the mouse input events.
  170. data->glfw_active_modifiers = glfw_mods;
  171. // Override the default key event callback to add global shortcuts for the samples.
  172. Rml::Context* context = data->context;
  173. KeyDownCallback key_down_callback = data->key_down_callback;
  174. switch (glfw_action)
  175. {
  176. case GLFW_PRESS:
  177. case GLFW_REPEAT:
  178. {
  179. const Rml::Input::KeyIdentifier key = RmlGLFW::ConvertKey(glfw_key);
  180. const int key_modifier = RmlGLFW::ConvertKeyModifiers(glfw_mods);
  181. float dp_ratio = 1.f;
  182. glfwGetWindowContentScale(data->window, &dp_ratio, nullptr);
  183. // See if we have any global shortcuts that take priority over the context.
  184. if (key_down_callback && !key_down_callback(context, key, key_modifier, dp_ratio, true))
  185. break;
  186. // Otherwise, hand the event over to the context by calling the input handler as normal.
  187. if (!RmlGLFW::ProcessKeyCallback(context, glfw_key, glfw_action, glfw_mods))
  188. break;
  189. // The key was not consumed by the context either, try keyboard shortcuts of lower priority.
  190. if (key_down_callback && !key_down_callback(context, key, key_modifier, dp_ratio, false))
  191. break;
  192. }
  193. break;
  194. case GLFW_RELEASE: RmlGLFW::ProcessKeyCallback(context, glfw_key, glfw_action, glfw_mods); break;
  195. }
  196. });
  197. glfwSetCharCallback(window, [](GLFWwindow* /*window*/, unsigned int codepoint) { RmlGLFW::ProcessCharCallback(data->context, codepoint); });
  198. glfwSetCursorEnterCallback(window, [](GLFWwindow* /*window*/, int entered) { RmlGLFW::ProcessCursorEnterCallback(data->context, entered); });
  199. // Mouse input
  200. glfwSetCursorPosCallback(window, [](GLFWwindow* window, double xpos, double ypos) {
  201. RmlGLFW::ProcessCursorPosCallback(data->context, window, xpos, ypos, data->glfw_active_modifiers);
  202. });
  203. glfwSetMouseButtonCallback(window, [](GLFWwindow* /*window*/, int button, int action, int mods) {
  204. data->glfw_active_modifiers = mods;
  205. RmlGLFW::ProcessMouseButtonCallback(data->context, button, action, mods);
  206. });
  207. glfwSetScrollCallback(window, [](GLFWwindow* /*window*/, double /*xoffset*/, double yoffset) {
  208. RmlGLFW::ProcessScrollCallback(data->context, yoffset, data->glfw_active_modifiers);
  209. });
  210. // Window events
  211. glfwSetFramebufferSizeCallback(window, [](GLFWwindow* /*window*/, int width, int height) {
  212. data->render_interface.SetViewport(width, height);
  213. RmlGLFW::ProcessFramebufferSizeCallback(data->context, width, height);
  214. });
  215. glfwSetWindowContentScaleCallback(window,
  216. [](GLFWwindow* /*window*/, float xscale, float /*yscale*/) { RmlGLFW::ProcessContentScaleCallback(data->context, xscale); });
  217. }