RenderInterfaceSFML.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 Nuno Silva
  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/Core/Core.h>
  29. #include "RenderInterfaceSFML.h"
  30. #ifndef GL_CLAMP_TO_EDGE
  31. #define GL_CLAMP_TO_EDGE 0x812F
  32. #endif
  33. #ifdef ENABLE_GLEW
  34. class RmlUiSFMLRendererGeometryHandler
  35. {
  36. public:
  37. GLuint VertexID, IndexID;
  38. int NumVertices;
  39. Rml::Core::TextureHandle Texture;
  40. RmlUiSFMLRendererGeometryHandler() : VertexID(0), IndexID(0), Texture(0), NumVertices(0)
  41. {
  42. };
  43. ~RmlUiSFMLRendererGeometryHandler()
  44. {
  45. if(VertexID)
  46. glDeleteBuffers(1, &VertexID);
  47. if(IndexID)
  48. glDeleteBuffers(1, &IndexID);
  49. VertexID = IndexID = 0;
  50. };
  51. };
  52. #endif
  53. struct RmlUiSFMLRendererVertex
  54. {
  55. sf::Vector2f Position, TexCoord;
  56. sf::Color Color;
  57. };
  58. RmlUiSFMLRenderer::RmlUiSFMLRenderer()
  59. {
  60. }
  61. void RmlUiSFMLRenderer::SetWindow(sf::RenderWindow *Window)
  62. {
  63. MyWindow = Window;
  64. Resize();
  65. };
  66. sf::RenderWindow *RmlUiSFMLRenderer::GetWindow()
  67. {
  68. return MyWindow;
  69. };
  70. void RmlUiSFMLRenderer::Resize()
  71. {
  72. static sf::View View;
  73. View.setViewport(sf::FloatRect(0, (float)MyWindow->getSize().x, (float)MyWindow->getSize().y, 0));
  74. MyWindow->setView(View);
  75. glMatrixMode(GL_PROJECTION);
  76. glLoadIdentity();
  77. glOrtho(0, MyWindow->getSize().x, MyWindow->getSize().y, 0, -1, 1);
  78. glMatrixMode(GL_MODELVIEW);
  79. glViewport(0, 0, MyWindow->getSize().x, MyWindow->getSize().y);
  80. };
  81. // Called by RmlUi when it wants to render geometry that it does not wish to optimise.
  82. void RmlUiSFMLRenderer::RenderGeometry(Rml::Core::Vertex* vertices, int num_vertices, int* indices, int num_indices, const Rml::Core::TextureHandle texture, const Rml::Core::Vector2f& translation)
  83. {
  84. MyWindow->pushGLStates();
  85. glTranslatef(translation.x, translation.y, 0);
  86. std::vector<Rml::Core::Vector2f> Positions(num_vertices);
  87. std::vector<Rml::Core::Colourb> Colors(num_vertices);
  88. std::vector<Rml::Core::Vector2f> TexCoords(num_vertices);
  89. for(int i = 0; i < num_vertices; i++)
  90. {
  91. Positions[i] = vertices[i].position;
  92. Colors[i] = vertices[i].colour;
  93. TexCoords[i] = vertices[i].tex_coord;
  94. };
  95. glEnableClientState(GL_VERTEX_ARRAY);
  96. glEnableClientState(GL_COLOR_ARRAY);
  97. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  98. glVertexPointer(2, GL_FLOAT, 0, &Positions[0]);
  99. glColorPointer(4, GL_UNSIGNED_BYTE, 0, &Colors[0]);
  100. glTexCoordPointer(2, GL_FLOAT, 0, &TexCoords[0]);
  101. glEnable(GL_BLEND);
  102. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  103. sf::Texture *sfTexture = (sf::Texture *)texture;
  104. if(sfTexture)
  105. {
  106. sf::Texture::bind(sfTexture);
  107. }
  108. else
  109. {
  110. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  111. glBindTexture(GL_TEXTURE_2D, 0);
  112. };
  113. glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_INT, indices);
  114. glDisableClientState(GL_VERTEX_ARRAY);
  115. glDisableClientState(GL_COLOR_ARRAY);
  116. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  117. glColor4f(1, 1, 1, 1);
  118. MyWindow->popGLStates();
  119. }
  120. // Called by RmlUi when it wants to compile geometry it believes will be static for the forseeable future.
  121. Rml::Core::CompiledGeometryHandle RmlUiSFMLRenderer::CompileGeometry(Rml::Core::Vertex* vertices, int num_vertices, int* indices, int num_indices, const Rml::Core::TextureHandle texture)
  122. {
  123. #ifdef ENABLE_GLEW
  124. std::vector<RmlUiSFMLRendererVertex> Data(num_vertices);
  125. for(unsigned long i = 0; i < Data.size(); i++)
  126. {
  127. Data[i].Position = *(sf::Vector2f*)&vertices[i].position;
  128. Data[i].TexCoord = *(sf::Vector2f*)&vertices[i].tex_coord;
  129. Data[i].Color = sf::Color(vertices[i].colour.red, vertices[i].colour.green,
  130. vertices[i].colour.blue, vertices[i].colour.alpha);
  131. };
  132. RmlUiSFMLRendererGeometryHandler *Geometry = new RmlUiSFMLRendererGeometryHandler();
  133. Geometry->NumVertices = num_indices;
  134. glGenBuffers(1, &Geometry->VertexID);
  135. glBindBuffer(GL_ARRAY_BUFFER, Geometry->VertexID);
  136. glBufferData(GL_ARRAY_BUFFER, sizeof(RmlUiSFMLRendererVertex) * num_vertices, &Data[0],
  137. GL_STATIC_DRAW);
  138. glGenBuffers(1, &Geometry->IndexID);
  139. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, Geometry->IndexID);
  140. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * num_indices, indices, GL_STATIC_DRAW);
  141. glBindBuffer(GL_ARRAY_BUFFER, 0);
  142. Geometry->Texture = texture;
  143. return (Rml::Core::CompiledGeometryHandle)Geometry;
  144. #else
  145. return (Rml::Core::CompiledGeometryHandle)nullptr;
  146. #endif
  147. }
  148. // Called by RmlUi when it wants to render application-compiled geometry.
  149. void RmlUiSFMLRenderer::RenderCompiledGeometry(Rml::Core::CompiledGeometryHandle geometry, const Rml::Core::Vector2f& translation)
  150. {
  151. #ifdef ENABLE_GLEW
  152. RmlUiSFMLRendererGeometryHandler *RealGeometry = (RmlUiSFMLRendererGeometryHandler *)geometry;
  153. MyWindow->pushGLStates();
  154. glTranslatef(translation.x, translation.y, 0);
  155. glEnable(GL_BLEND);
  156. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  157. sf::Texture *texture = (sf::Texture *)RealGeometry->Texture;
  158. if(texture)
  159. {
  160. sf::Texture::bind(texture);
  161. }
  162. else
  163. {
  164. glBindTexture(GL_TEXTURE_2D, 0);
  165. };
  166. glEnable(GL_VERTEX_ARRAY);
  167. glEnable(GL_TEXTURE_COORD_ARRAY);
  168. glEnable(GL_COLOR_ARRAY);
  169. #define BUFFER_OFFSET(x) ((char*)0 + x)
  170. glBindBuffer(GL_ARRAY_BUFFER, RealGeometry->VertexID);
  171. glVertexPointer(2, GL_FLOAT, sizeof(RmlUiSFMLRendererVertex), BUFFER_OFFSET(0));
  172. glTexCoordPointer(2, GL_FLOAT, sizeof(RmlUiSFMLRendererVertex), BUFFER_OFFSET(sizeof(sf::Vector2f)));
  173. glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(RmlUiSFMLRendererVertex), BUFFER_OFFSET(sizeof(sf::Vector2f[2])));
  174. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, RealGeometry->IndexID);
  175. glDrawElements(GL_TRIANGLES, RealGeometry->NumVertices, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
  176. glBindBuffer(GL_ARRAY_BUFFER, 0);
  177. glDisable(GL_COLOR_ARRAY);
  178. glDisable(GL_TEXTURE_COORD_ARRAY);
  179. glDisable(GL_VERTEX_ARRAY);
  180. glColor4f(1, 1, 1, 1);
  181. MyWindow->popGLStates();
  182. #else
  183. RMLUI_ASSERT(false);
  184. #endif
  185. }
  186. // Called by RmlUi when it wants to release application-compiled geometry.
  187. void RmlUiSFMLRenderer::ReleaseCompiledGeometry(Rml::Core::CompiledGeometryHandle geometry)
  188. {
  189. #ifdef ENABLE_GLEW
  190. delete (RmlUiSFMLRendererGeometryHandler *)geometry;
  191. #else
  192. RMLUI_ASSERT(false);
  193. #endif
  194. }
  195. // Called by RmlUi when it wants to enable or disable scissoring to clip content.
  196. void RmlUiSFMLRenderer::EnableScissorRegion(bool enable)
  197. {
  198. if (enable)
  199. glEnable(GL_SCISSOR_TEST);
  200. else
  201. glDisable(GL_SCISSOR_TEST);
  202. }
  203. // Called by RmlUi when it wants to change the scissor region.
  204. void RmlUiSFMLRenderer::SetScissorRegion(int x, int y, int width, int height)
  205. {
  206. glScissor(x, MyWindow->getSize().y - (y + height), width, height);
  207. }
  208. // Called by RmlUi when a texture is required by the library.
  209. bool RmlUiSFMLRenderer::LoadTexture(Rml::Core::TextureHandle& texture_handle, Rml::Core::Vector2i& texture_dimensions, const Rml::Core::String& source)
  210. {
  211. Rml::Core::FileInterface* file_interface = Rml::Core::GetFileInterface();
  212. Rml::Core::FileHandle file_handle = file_interface->Open(source);
  213. if (!file_handle)
  214. return false;
  215. file_interface->Seek(file_handle, 0, SEEK_END);
  216. size_t buffer_size = file_interface->Tell(file_handle);
  217. file_interface->Seek(file_handle, 0, SEEK_SET);
  218. char* buffer = new char[buffer_size];
  219. file_interface->Read(buffer, buffer_size, file_handle);
  220. file_interface->Close(file_handle);
  221. sf::Texture *texture = new sf::Texture();
  222. if(!texture->loadFromMemory(buffer, buffer_size))
  223. {
  224. delete[] buffer;
  225. delete texture;
  226. return false;
  227. };
  228. delete[] buffer;
  229. texture_handle = (Rml::Core::TextureHandle) texture;
  230. texture_dimensions = Rml::Core::Vector2i(texture->getSize().x, texture->getSize().y);
  231. return true;
  232. }
  233. // Called by RmlUi when a texture is required to be built from an internally-generated sequence of pixels.
  234. bool RmlUiSFMLRenderer::GenerateTexture(Rml::Core::TextureHandle& texture_handle, const Rml::Core::byte* source, const Rml::Core::Vector2i& source_dimensions)
  235. {
  236. sf::Texture *texture = new sf::Texture();
  237. if (!texture->create(source_dimensions.x, source_dimensions.y)) {
  238. delete texture;
  239. return false;
  240. }
  241. texture->update(source, source_dimensions.x, source_dimensions.y, 0, 0);
  242. texture_handle = (Rml::Core::TextureHandle)texture;
  243. return true;
  244. }
  245. // Called by RmlUi when a loaded texture is no longer required.
  246. void RmlUiSFMLRenderer::ReleaseTexture(Rml::Core::TextureHandle texture_handle)
  247. {
  248. delete (sf::Texture *)texture_handle;
  249. }