RmlUi_Backend_SFML_GL2.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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_SFML.h"
  30. #include "RmlUi_Renderer_GL2.h"
  31. #include <RmlUi/Core/Context.h>
  32. #include <RmlUi/Core/Core.h>
  33. #include <RmlUi/Core/FileInterface.h>
  34. #include <RmlUi/Core/Profiling.h>
  35. #include <RmlUi/Debugger/Debugger.h>
  36. #include <SFML/Graphics.hpp>
  37. #include <SFML/Window.hpp>
  38. /**
  39. Custom render interface example for the SFML/GL2 backend.
  40. Overloads the OpenGL2 render interface to load textures through SFML's built-in texture loading functionality.
  41. */
  42. class RenderInterface_GL2_SFML : public RenderInterface_GL2 {
  43. public:
  44. // -- Inherited from Rml::RenderInterface --
  45. void RenderGeometry(Rml::Vertex* vertices, int num_vertices, int* indices, int num_indices, Rml::TextureHandle texture,
  46. const Rml::Vector2f& translation) override
  47. {
  48. if (texture)
  49. {
  50. sf::Texture::bind((sf::Texture*)texture);
  51. texture = RenderInterface_GL2::TextureEnableWithoutBinding;
  52. }
  53. RenderInterface_GL2::RenderGeometry(vertices, num_vertices, indices, num_indices, texture, translation);
  54. }
  55. bool LoadTexture(Rml::TextureHandle& texture_handle, Rml::Vector2i& texture_dimensions, const Rml::String& source) override
  56. {
  57. Rml::FileInterface* file_interface = Rml::GetFileInterface();
  58. Rml::FileHandle file_handle = file_interface->Open(source);
  59. if (!file_handle)
  60. return false;
  61. file_interface->Seek(file_handle, 0, SEEK_END);
  62. size_t buffer_size = file_interface->Tell(file_handle);
  63. file_interface->Seek(file_handle, 0, SEEK_SET);
  64. char* buffer = new char[buffer_size];
  65. file_interface->Read(buffer, buffer_size, file_handle);
  66. file_interface->Close(file_handle);
  67. sf::Texture* texture = new sf::Texture();
  68. texture->setSmooth(true);
  69. bool success = texture->loadFromMemory(buffer, buffer_size);
  70. delete[] buffer;
  71. if (success)
  72. {
  73. texture_handle = (Rml::TextureHandle)texture;
  74. texture_dimensions = Rml::Vector2i(texture->getSize().x, texture->getSize().y);
  75. }
  76. else
  77. {
  78. delete texture;
  79. }
  80. return success;
  81. }
  82. bool GenerateTexture(Rml::TextureHandle& texture_handle, const Rml::byte* source, const Rml::Vector2i& source_dimensions) override
  83. {
  84. sf::Texture* texture = new sf::Texture();
  85. texture->setSmooth(true);
  86. if (!texture->create(source_dimensions.x, source_dimensions.y))
  87. {
  88. delete texture;
  89. return false;
  90. }
  91. texture->update(source, source_dimensions.x, source_dimensions.y, 0, 0);
  92. texture_handle = (Rml::TextureHandle)texture;
  93. return true;
  94. }
  95. void ReleaseTexture(Rml::TextureHandle texture_handle) override { delete (sf::Texture*)texture_handle; }
  96. };
  97. // Updates the viewport and context dimensions, should be called whenever the window size changes.
  98. static void UpdateWindowDimensions(sf::RenderWindow& window, RenderInterface_GL2_SFML& render_interface, Rml::Context* context)
  99. {
  100. const int width = (int)window.getSize().x;
  101. const int height = (int)window.getSize().y;
  102. if (context)
  103. context->SetDimensions(Rml::Vector2i(width, height));
  104. sf::View view(sf::FloatRect(0.f, 0.f, (float)width, (float)height));
  105. window.setView(view);
  106. render_interface.SetViewport(width, height);
  107. }
  108. /**
  109. Global data used by this backend.
  110. Lifetime governed by the calls to Backend::Initialize() and Backend::Shutdown().
  111. */
  112. struct BackendData {
  113. SystemInterface_SFML system_interface;
  114. RenderInterface_GL2_SFML render_interface;
  115. sf::RenderWindow window;
  116. bool running = true;
  117. };
  118. static Rml::UniquePtr<BackendData> data;
  119. bool Backend::Initialize(const char* window_name, int width, int height, bool allow_resize)
  120. {
  121. RMLUI_ASSERT(!data);
  122. data = Rml::MakeUnique<BackendData>();
  123. // Create the window.
  124. sf::RenderWindow out_window;
  125. sf::ContextSettings context_settings;
  126. context_settings.stencilBits = 8;
  127. context_settings.antialiasingLevel = 2;
  128. const sf::Uint32 style = (allow_resize ? sf::Style::Default : (sf::Style::Titlebar | sf::Style::Close));
  129. data->window.create(sf::VideoMode(width, height), window_name, style, context_settings);
  130. data->window.setVerticalSyncEnabled(true);
  131. if (!data->window.isOpen())
  132. {
  133. data.reset();
  134. return false;
  135. }
  136. // Optionally apply the SFML window to the system interface so that it can change its mouse cursor.
  137. data->system_interface.SetWindow(&data->window);
  138. UpdateWindowDimensions(data->window, data->render_interface, nullptr);
  139. return true;
  140. }
  141. void Backend::Shutdown()
  142. {
  143. data.reset();
  144. }
  145. Rml::SystemInterface* Backend::GetSystemInterface()
  146. {
  147. RMLUI_ASSERT(data);
  148. return &data->system_interface;
  149. }
  150. Rml::RenderInterface* Backend::GetRenderInterface()
  151. {
  152. RMLUI_ASSERT(data);
  153. return &data->render_interface;
  154. }
  155. bool Backend::ProcessEvents(Rml::Context* context, KeyDownCallback key_down_callback)
  156. {
  157. RMLUI_ASSERT(data && context);
  158. // The contents of this function is intended to be copied directly into your main loop.
  159. bool result = data->running;
  160. sf::Event ev;
  161. while (data->window.pollEvent(ev))
  162. {
  163. switch (ev.type)
  164. {
  165. case sf::Event::Resized:
  166. UpdateWindowDimensions(data->window, data->render_interface, context);
  167. break;
  168. case sf::Event::KeyPressed:
  169. {
  170. const Rml::Input::KeyIdentifier key = RmlSFML::ConvertKey(ev.key.code);
  171. const int key_modifier = RmlSFML::GetKeyModifierState();
  172. const float native_dp_ratio = 1.f;
  173. // See if we have any global shortcuts that take priority over the context.
  174. if (key_down_callback && !key_down_callback(context, key, key_modifier, native_dp_ratio, true))
  175. break;
  176. // Otherwise, hand the event over to the context by calling the input handler as normal.
  177. if (!RmlSFML::InputHandler(context, ev))
  178. break;
  179. // The key was not consumed by the context either, try keyboard shortcuts of lower priority.
  180. if (key_down_callback && !key_down_callback(context, key, key_modifier, native_dp_ratio, false))
  181. break;
  182. }
  183. break;
  184. case sf::Event::Closed:
  185. result = false;
  186. break;
  187. default:
  188. RmlSFML::InputHandler(context, ev);
  189. break;
  190. }
  191. }
  192. return result;
  193. }
  194. void Backend::RequestExit()
  195. {
  196. RMLUI_ASSERT(data);
  197. data->running = false;
  198. }
  199. void Backend::BeginFrame()
  200. {
  201. RMLUI_ASSERT(data);
  202. sf::RenderWindow& window = data->window;
  203. window.resetGLStates();
  204. window.clear();
  205. data->render_interface.BeginFrame();
  206. #if 0
  207. // Draw a simple shape with SFML for demonstration purposes. Make sure to push and pop GL states as appropriate.
  208. sf::Vector2f circle_position(100.f, 100.f);
  209. window.pushGLStates();
  210. sf::CircleShape circle(50.f);
  211. circle.setPosition(circle_position);
  212. circle.setFillColor(sf::Color::Blue);
  213. circle.setOutlineColor(sf::Color::Red);
  214. circle.setOutlineThickness(10.f);
  215. window.draw(circle);
  216. window.popGLStates();
  217. #endif
  218. }
  219. void Backend::PresentFrame()
  220. {
  221. RMLUI_ASSERT(data);
  222. data->render_interface.EndFrame();
  223. data->window.display();
  224. // Optional, used to mark frames during performance profiling.
  225. RMLUI_FrameMark;
  226. }