RenderInterfaceSDL2.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 <SDL_image.h>
  29. #include "RenderInterfaceSDL2.h"
  30. #if !(SDL_VIDEO_RENDER_OGL)
  31. #error "Only the opengl sdl backend is supported. To add support for others, see http://mdqinc.com/blog/2013/01/integrating-librocket-with-sdl-2/"
  32. #endif
  33. RocketSDL2Renderer::RocketSDL2Renderer(SDL_Renderer* renderer, SDL_Window* screen)
  34. {
  35. mRenderer = renderer;
  36. mScreen = screen;
  37. }
  38. // Called by Rocket when it wants to render geometry that it does not wish to optimise.
  39. void RocketSDL2Renderer::RenderGeometry(Rocket::Core::Vertex* vertices, int num_vertices, int* indices, int num_indices, const Rocket::Core::TextureHandle texture, const Rocket::Core::Vector2f& translation)
  40. {
  41. // SDL uses shaders that we need to disable here
  42. glUseProgramObjectARB(0);
  43. glPushMatrix();
  44. glTranslatef(translation.x, translation.y, 0);
  45. std::vector<Rocket::Core::Vector2f> Positions(num_vertices);
  46. std::vector<Rocket::Core::Colourb> Colors(num_vertices);
  47. std::vector<Rocket::Core::Vector2f> TexCoords(num_vertices);
  48. float texw, texh;
  49. SDL_Texture* sdl_texture = NULL;
  50. if(texture)
  51. {
  52. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  53. sdl_texture = (SDL_Texture *) texture;
  54. SDL_GL_BindTexture(sdl_texture, &texw, &texh);
  55. }
  56. for(int i = 0; i < num_vertices; i++) {
  57. Positions[i] = vertices[i].position;
  58. Colors[i] = vertices[i].colour;
  59. if (sdl_texture) {
  60. TexCoords[i].x = vertices[i].tex_coord.x * texw;
  61. TexCoords[i].y = vertices[i].tex_coord.y * texh;
  62. }
  63. else TexCoords[i] = vertices[i].tex_coord;
  64. };
  65. glEnableClientState(GL_VERTEX_ARRAY);
  66. glEnableClientState(GL_COLOR_ARRAY);
  67. glVertexPointer(2, GL_FLOAT, 0, &Positions[0]);
  68. glColorPointer(4, GL_UNSIGNED_BYTE, 0, &Colors[0]);
  69. glTexCoordPointer(2, GL_FLOAT, 0, &TexCoords[0]);
  70. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  71. glEnable(GL_BLEND);
  72. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  73. glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_INT, indices);
  74. glDisableClientState(GL_VERTEX_ARRAY);
  75. glDisableClientState(GL_COLOR_ARRAY);
  76. if (sdl_texture) {
  77. SDL_GL_UnbindTexture(sdl_texture);
  78. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  79. }
  80. glColor4f(1.0, 1.0, 1.0, 1.0);
  81. glPopMatrix();
  82. /* Reset blending and draw a fake point just outside the screen to let SDL know that it needs to reset its state in case it wants to render a texture */
  83. glDisable(GL_BLEND);
  84. SDL_SetRenderDrawBlendMode(mRenderer, SDL_BLENDMODE_NONE);
  85. SDL_RenderDrawPoint(mRenderer, -1, -1);
  86. }
  87. // Called by Rocket when it wants to enable or disable scissoring to clip content.
  88. void RocketSDL2Renderer::EnableScissorRegion(bool enable)
  89. {
  90. if (enable)
  91. glEnable(GL_SCISSOR_TEST);
  92. else
  93. glDisable(GL_SCISSOR_TEST);
  94. }
  95. // Called by Rocket when it wants to change the scissor region.
  96. void RocketSDL2Renderer::SetScissorRegion(int x, int y, int width, int height)
  97. {
  98. int w_width, w_height;
  99. SDL_GetWindowSize(mScreen, &w_width, &w_height);
  100. glScissor(x, w_height - (y + height), width, height);
  101. }
  102. // Called by Rocket when a texture is required by the library.
  103. bool RocketSDL2Renderer::LoadTexture(Rocket::Core::TextureHandle& texture_handle, Rocket::Core::Vector2i& texture_dimensions, const Rocket::Core::String& source)
  104. {
  105. Rocket::Core::FileInterface* file_interface = Rocket::Core::GetFileInterface();
  106. Rocket::Core::FileHandle file_handle = file_interface->Open(source);
  107. if (!file_handle)
  108. return false;
  109. file_interface->Seek(file_handle, 0, SEEK_END);
  110. size_t buffer_size = file_interface->Tell(file_handle);
  111. file_interface->Seek(file_handle, 0, SEEK_SET);
  112. char* buffer = new char[buffer_size];
  113. file_interface->Read(buffer, buffer_size, file_handle);
  114. file_interface->Close(file_handle);
  115. size_t i;
  116. for(i = source.Length() - 1; i > 0; i--)
  117. {
  118. if(source[i] == '.')
  119. break;
  120. }
  121. Rocket::Core::String extension = source.Substring(i+1, source.Length()-i);
  122. SDL_Surface* surface = IMG_LoadTyped_RW(SDL_RWFromMem(buffer, buffer_size), 1, extension.CString());
  123. if (surface) {
  124. SDL_Texture *texture = SDL_CreateTextureFromSurface(mRenderer, surface);
  125. if (texture) {
  126. texture_handle = (Rocket::Core::TextureHandle) texture;
  127. texture_dimensions = Rocket::Core::Vector2i(surface->w, surface->h);
  128. SDL_FreeSurface(surface);
  129. }
  130. else
  131. {
  132. return false;
  133. }
  134. return true;
  135. }
  136. return false;
  137. }
  138. // Called by Rocket when a texture is required to be built from an internally-generated sequence of pixels.
  139. bool RocketSDL2Renderer::GenerateTexture(Rocket::Core::TextureHandle& texture_handle, const Rocket::Core::byte* source, const Rocket::Core::Vector2i& source_dimensions)
  140. {
  141. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  142. Uint32 rmask = 0xff000000;
  143. Uint32 gmask = 0x00ff0000;
  144. Uint32 bmask = 0x0000ff00;
  145. Uint32 amask = 0x000000ff;
  146. #else
  147. Uint32 rmask = 0x000000ff;
  148. Uint32 gmask = 0x0000ff00;
  149. Uint32 bmask = 0x00ff0000;
  150. Uint32 amask = 0xff000000;
  151. #endif
  152. SDL_Surface *surface = SDL_CreateRGBSurfaceFrom ((void*) source, source_dimensions.x, source_dimensions.y, 32, source_dimensions.x*4, rmask, gmask, bmask, amask);
  153. SDL_Texture *texture = SDL_CreateTextureFromSurface(mRenderer, surface);
  154. SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
  155. SDL_FreeSurface(surface);
  156. texture_handle = (Rocket::Core::TextureHandle) texture;
  157. return true;
  158. }
  159. // Called by Rocket when a loaded texture is no longer required.
  160. void RocketSDL2Renderer::ReleaseTexture(Rocket::Core::TextureHandle texture_handle)
  161. {
  162. SDL_DestroyTexture((SDL_Texture*) texture_handle);
  163. }