RenderInterfaceSFML.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 Nuno Silva
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include <Rocket/Core/Core.h>
  28. #include "RenderInterfaceSFML.h"
  29. #ifdef ENABLE_GLEW
  30. #include <gl/glew.h>
  31. #endif
  32. #ifndef GL_CLAMP_TO_EDGE
  33. #define GL_CLAMP_TO_EDGE 0x812F
  34. #endif
  35. // If built with the GL Easy Extension library we can compile geometry to VBO's
  36. // http://www.opengl.org/sdk/libs/GLee/
  37. #ifdef ENABLE_GLEW
  38. class RocketSFMLRendererGeometryHandler
  39. {
  40. public:
  41. GLuint VertexID, IndexID;
  42. int NumVertices;
  43. Rocket::Core::TextureHandle Texture;
  44. RocketSFMLRendererGeometryHandler() : VertexID(0), IndexID(0), Texture(0), NumVertices(0)
  45. {
  46. };
  47. ~RocketSFMLRendererGeometryHandler()
  48. {
  49. if(VertexID)
  50. glDeleteBuffers(1, &VertexID);
  51. if(IndexID)
  52. glDeleteBuffers(1, &IndexID);
  53. VertexID = IndexID = 0;
  54. };
  55. };
  56. #endif
  57. struct RocketSFMLRendererVertex
  58. {
  59. sf::Vector2f Position, TexCoord;
  60. sf::Color Color;
  61. };
  62. RocketSFMLRenderer::RocketSFMLRenderer()
  63. {
  64. }
  65. void RocketSFMLRenderer::SetWindow(sf::RenderWindow *Window)
  66. {
  67. MyWindow = Window;
  68. Resize();
  69. };
  70. sf::RenderWindow *RocketSFMLRenderer::GetWindow()
  71. {
  72. return MyWindow;
  73. };
  74. void RocketSFMLRenderer::Resize()
  75. {
  76. MyWindow->SetActive(true);
  77. //MyWindow->SaveGLStates();
  78. static sf::View View;
  79. //View.SetFromRect(sf::FloatRect(0, (float)MyWindow->GetWidth(), (float)MyWindow->GetHeight(), 0));
  80. View.SetViewport(sf::FloatRect(0, (float)MyWindow->GetWidth(), (float)MyWindow->GetHeight(), 0));
  81. MyWindow->SetView(View);
  82. glMatrixMode(GL_PROJECTION);
  83. glLoadIdentity();
  84. glOrtho(0, MyWindow->GetWidth(), MyWindow->GetHeight(), 0, -1, 1);
  85. glMatrixMode(GL_MODELVIEW);
  86. glViewport(0, 0, MyWindow->GetWidth(), MyWindow->GetHeight());
  87. //MyWindow->RestoreGLStates();
  88. };
  89. // Called by Rocket when it wants to render geometry that it does not wish to optimise.
  90. void RocketSFMLRenderer::RenderGeometry(Rocket::Core::Vertex* vertices, int num_vertices, int* indices, int num_indices, const Rocket::Core::TextureHandle texture, const Rocket::Core::Vector2f& translation)
  91. {
  92. MyWindow->SetActive();
  93. glPushMatrix();
  94. glTranslatef(translation.x, translation.y, 0);
  95. std::vector<Rocket::Core::Vector2f> Positions(num_vertices);
  96. std::vector<Rocket::Core::Colourb> Colors(num_vertices);
  97. std::vector<Rocket::Core::Vector2f> TexCoords(num_vertices);
  98. for(int i = 0; i < num_vertices; i++)
  99. {
  100. Positions[i] = vertices[i].position;
  101. Colors[i] = vertices[i].colour;
  102. TexCoords[i] = vertices[i].tex_coord;
  103. };
  104. glEnableClientState(GL_VERTEX_ARRAY);
  105. glEnableClientState(GL_COLOR_ARRAY);
  106. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  107. glVertexPointer(2, GL_FLOAT, 0, &Positions[0]);
  108. glColorPointer(4, GL_UNSIGNED_BYTE, 0, &Colors[0]);
  109. glTexCoordPointer(2, GL_FLOAT, 0, &TexCoords[0]);
  110. glEnable(GL_BLEND);
  111. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  112. sf::Image *image = (sf::Image *)texture;
  113. if(image)
  114. {
  115. image->Bind();
  116. }
  117. else
  118. {
  119. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  120. glBindTexture(GL_TEXTURE_2D, 0);
  121. };
  122. glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_INT, indices);
  123. glDisableClientState(GL_VERTEX_ARRAY);
  124. glDisableClientState(GL_COLOR_ARRAY);
  125. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  126. glColor4f(1, 1, 1, 1);
  127. glPopMatrix();
  128. }
  129. // Called by Rocket when it wants to compile geometry it believes will be static for the forseeable future.
  130. Rocket::Core::CompiledGeometryHandle RocketSFMLRenderer::CompileGeometry(Rocket::Core::Vertex* vertices,
  131. int num_vertices, int* indices,
  132. int num_indices,
  133. const Rocket::Core::TextureHandle texture)
  134. {
  135. #ifdef ENABLE_GLEW
  136. MyWindow->SetActive();
  137. //if(!GLEE_VERSION_2_0)
  138. //return (Rocket::Core::CompiledGeometryHandle) NULL;
  139. std::vector<RocketSFMLRendererVertex> Data(num_vertices);
  140. for(unsigned long i = 0; i < Data.size(); i++)
  141. {
  142. Data[i].Position = *(sf::Vector2f*)&vertices[i].position;
  143. Data[i].TexCoord = *(sf::Vector2f*)&vertices[i].tex_coord;
  144. Data[i].Color = sf::Color(vertices[i].colour.red, vertices[i].colour.green,
  145. vertices[i].colour.blue, vertices[i].colour.alpha);
  146. };
  147. RocketSFMLRendererGeometryHandler *Geometry = new RocketSFMLRendererGeometryHandler();
  148. Geometry->NumVertices = num_indices;
  149. glGenBuffers(1, &Geometry->VertexID);
  150. glBindBuffer(GL_ARRAY_BUFFER, Geometry->VertexID);
  151. glBufferData(GL_ARRAY_BUFFER, sizeof(RocketSFMLRendererVertex) * num_vertices, &Data[0],
  152. GL_STATIC_DRAW);
  153. glGenBuffers(1, &Geometry->IndexID);
  154. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, Geometry->IndexID);
  155. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * num_indices, indices, GL_STATIC_DRAW);
  156. glBindBuffer(GL_ARRAY_BUFFER, 0);
  157. Geometry->Texture = texture;
  158. return (Rocket::Core::CompiledGeometryHandle)Geometry;
  159. #else
  160. return NULL;
  161. #endif
  162. }
  163. // Called by Rocket when it wants to render application-compiled geometry.
  164. void RocketSFMLRenderer::RenderCompiledGeometry(Rocket::Core::CompiledGeometryHandle geometry, const Rocket::Core::Vector2f& translation)
  165. {
  166. #ifdef ENABLE_GLEW
  167. MyWindow->SetActive();
  168. RocketSFMLRendererGeometryHandler *RealGeometry = (RocketSFMLRendererGeometryHandler *)geometry;
  169. glPushMatrix();
  170. glTranslatef(translation.x, translation.y, 0);
  171. glEnable(GL_BLEND);
  172. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  173. sf::Image *image = (sf::Image *)RealGeometry->Texture;
  174. if(image)
  175. {
  176. image->Bind();
  177. }
  178. else
  179. {
  180. glBindTexture(GL_TEXTURE_2D, 0);
  181. };
  182. glEnable(GL_VERTEX_ARRAY);
  183. glEnable(GL_TEXTURE_COORD_ARRAY);
  184. glEnable(GL_COLOR_ARRAY);
  185. #define BUFFER_OFFSET(x) ((char*)0 + x)
  186. glBindBuffer(GL_ARRAY_BUFFER, RealGeometry->VertexID);
  187. glVertexPointer(2, GL_FLOAT, sizeof(RocketSFMLRendererVertex), BUFFER_OFFSET(0));
  188. glTexCoordPointer(2, GL_FLOAT, sizeof(RocketSFMLRendererVertex), BUFFER_OFFSET(sizeof(sf::Vector2f)));
  189. glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(RocketSFMLRendererVertex), BUFFER_OFFSET(sizeof(sf::Vector2f[2])));
  190. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, RealGeometry->IndexID);
  191. glDrawElements(GL_TRIANGLES, RealGeometry->NumVertices, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
  192. glBindBuffer(GL_ARRAY_BUFFER, 0);
  193. glDisable(GL_COLOR_ARRAY);
  194. glDisable(GL_TEXTURE_COORD_ARRAY);
  195. glDisable(GL_VERTEX_ARRAY);
  196. glColor4f(1, 1, 1, 1);
  197. glPopMatrix();
  198. #else
  199. ROCKET_ASSERT(false /*& "Not Implemented"*/);
  200. #endif
  201. }
  202. // Called by Rocket when it wants to release application-compiled geometry.
  203. void RocketSFMLRenderer::ReleaseCompiledGeometry(Rocket::Core::CompiledGeometryHandle geometry)
  204. {
  205. #ifdef ENABLE_GLEW
  206. MyWindow->SetActive();
  207. delete (RocketSFMLRendererGeometryHandler *)geometry;
  208. #else
  209. ROCKET_ASSERT(false /*& "Not Implemented"*/);
  210. #endif
  211. }
  212. // Called by Rocket when it wants to enable or disable scissoring to clip content.
  213. void RocketSFMLRenderer::EnableScissorRegion(bool enable)
  214. {
  215. MyWindow->SetActive();
  216. if (enable)
  217. glEnable(GL_SCISSOR_TEST);
  218. else
  219. glDisable(GL_SCISSOR_TEST);
  220. }
  221. // Called by Rocket when it wants to change the scissor region.
  222. void RocketSFMLRenderer::SetScissorRegion(int x, int y, int width, int height)
  223. {
  224. MyWindow->SetActive();
  225. glScissor(x, MyWindow->GetHeight() - (y + height), width, height);
  226. }
  227. // Called by Rocket when a texture is required by the library.
  228. bool RocketSFMLRenderer::LoadTexture(Rocket::Core::TextureHandle& texture_handle, Rocket::Core::Vector2i& texture_dimensions, const Rocket::Core::String& source)
  229. {
  230. MyWindow->SetActive();
  231. Rocket::Core::FileInterface* file_interface = Rocket::Core::GetFileInterface();
  232. Rocket::Core::FileHandle file_handle = file_interface->Open(source);
  233. if (file_handle == NULL)
  234. return false;
  235. file_interface->Seek(file_handle, 0, SEEK_END);
  236. size_t buffer_size = file_interface->Tell(file_handle);
  237. file_interface->Seek(file_handle, 0, SEEK_SET);
  238. char* buffer = new char[buffer_size];
  239. file_interface->Read(buffer, buffer_size, file_handle);
  240. file_interface->Close(file_handle);
  241. sf::Image *image = new sf::Image();
  242. if(!image->LoadFromMemory(buffer, buffer_size))
  243. {
  244. delete buffer;
  245. delete image;
  246. return false;
  247. };
  248. delete buffer;
  249. texture_handle = (Rocket::Core::TextureHandle) image;
  250. texture_dimensions = Rocket::Core::Vector2i(image->GetWidth(), image->GetHeight());
  251. return true;
  252. }
  253. // Called by Rocket when a texture is required to be built from an internally-generated sequence of pixels.
  254. bool RocketSFMLRenderer::GenerateTexture(Rocket::Core::TextureHandle& texture_handle, const Rocket::Core::byte* source, const Rocket::Core::Vector2i& source_dimensions)
  255. {
  256. MyWindow->SetActive();
  257. sf::Image *image = new sf::Image();
  258. if(!image->LoadFromPixels(source_dimensions.x, source_dimensions.y, source))
  259. {
  260. delete image;
  261. return false;
  262. };
  263. texture_handle = (Rocket::Core::TextureHandle)image;
  264. return true;
  265. }
  266. // Called by Rocket when a loaded texture is no longer required.
  267. void RocketSFMLRenderer::ReleaseTexture(Rocket::Core::TextureHandle texture_handle)
  268. {
  269. MyWindow->SetActive();
  270. delete (sf::Image *)texture_handle;
  271. }