RmlUi_Renderer_SDL.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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_Renderer_SDL.h"
  29. #include <RmlUi/Core/Core.h>
  30. #include <RmlUi/Core/FileInterface.h>
  31. #include <RmlUi/Core/Types.h>
  32. #include <SDL.h>
  33. #include <SDL_image.h>
  34. RenderInterface_SDL::RenderInterface_SDL(SDL_Renderer* renderer) : renderer(renderer)
  35. {
  36. // RmlUi serves vertex colors and textures with premultiplied alpha, set the blend mode accordingly.
  37. // Equivalent to glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA).
  38. blend_mode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD, SDL_BLENDFACTOR_ONE,
  39. SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD);
  40. }
  41. void RenderInterface_SDL::BeginFrame()
  42. {
  43. SDL_RenderSetViewport(renderer, nullptr);
  44. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  45. SDL_RenderClear(renderer);
  46. SDL_SetRenderDrawBlendMode(renderer, blend_mode);
  47. }
  48. void RenderInterface_SDL::EndFrame() {}
  49. Rml::CompiledGeometryHandle RenderInterface_SDL::CompileGeometry(Rml::Span<const Rml::Vertex> vertices, Rml::Span<const int> indices)
  50. {
  51. GeometryView* data = new GeometryView{vertices, indices};
  52. return reinterpret_cast<Rml::CompiledGeometryHandle>(data);
  53. }
  54. void RenderInterface_SDL::ReleaseGeometry(Rml::CompiledGeometryHandle geometry)
  55. {
  56. delete reinterpret_cast<GeometryView*>(geometry);
  57. }
  58. void RenderInterface_SDL::RenderGeometry(Rml::CompiledGeometryHandle handle, Rml::Vector2f translation, Rml::TextureHandle texture)
  59. {
  60. const GeometryView* geometry = reinterpret_cast<GeometryView*>(handle);
  61. const Rml::Vertex* vertices = geometry->vertices.data();
  62. const size_t num_vertices = geometry->vertices.size();
  63. const int* indices = geometry->indices.data();
  64. const size_t num_indices = geometry->indices.size();
  65. SDL_FPoint* positions = new SDL_FPoint[num_vertices];
  66. for (size_t i = 0; i < num_vertices; i++)
  67. {
  68. positions[i].x = vertices[i].position.x + translation.x;
  69. positions[i].y = vertices[i].position.y + translation.y;
  70. }
  71. SDL_Texture* sdl_texture = (SDL_Texture*)texture;
  72. SDL_RenderGeometryRaw(renderer, sdl_texture, &positions[0].x, sizeof(SDL_FPoint), (const SDL_Color*)&vertices->colour, sizeof(Rml::Vertex),
  73. &vertices->tex_coord.x, sizeof(Rml::Vertex), (int)num_vertices, indices, (int)num_indices, 4);
  74. delete[] positions;
  75. }
  76. void RenderInterface_SDL::EnableScissorRegion(bool enable)
  77. {
  78. if (enable)
  79. SDL_RenderSetClipRect(renderer, &rect_scissor);
  80. else
  81. SDL_RenderSetClipRect(renderer, nullptr);
  82. scissor_region_enabled = enable;
  83. }
  84. void RenderInterface_SDL::SetScissorRegion(Rml::Rectanglei region)
  85. {
  86. rect_scissor.x = region.Left();
  87. rect_scissor.y = region.Top();
  88. rect_scissor.w = region.Width();
  89. rect_scissor.h = region.Height();
  90. if (scissor_region_enabled)
  91. SDL_RenderSetClipRect(renderer, &rect_scissor);
  92. }
  93. Rml::TextureHandle RenderInterface_SDL::LoadTexture(Rml::Vector2i& texture_dimensions, const Rml::String& source)
  94. {
  95. Rml::FileInterface* file_interface = Rml::GetFileInterface();
  96. Rml::FileHandle file_handle = file_interface->Open(source);
  97. if (!file_handle)
  98. return {};
  99. file_interface->Seek(file_handle, 0, SEEK_END);
  100. size_t buffer_size = file_interface->Tell(file_handle);
  101. file_interface->Seek(file_handle, 0, SEEK_SET);
  102. using Rml::byte;
  103. Rml::UniquePtr<byte[]> buffer(new byte[buffer_size]);
  104. file_interface->Read(buffer.get(), buffer_size, file_handle);
  105. file_interface->Close(file_handle);
  106. const size_t i_ext = source.rfind('.');
  107. Rml::String extension = (i_ext == Rml::String::npos ? Rml::String() : source.substr(i_ext + 1));
  108. SDL_Surface* surface = IMG_LoadTyped_RW(SDL_RWFromMem(buffer.get(), int(buffer_size)), 1, extension.c_str());
  109. if (!surface)
  110. return {};
  111. if (surface->format->format != SDL_PIXELFORMAT_RGBA32 && surface->format->format != SDL_PIXELFORMAT_BGRA32)
  112. {
  113. SDL_Surface* converted_surface = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_RGBA32, 0);
  114. SDL_FreeSurface(surface);
  115. if (!converted_surface)
  116. return {};
  117. surface = converted_surface;
  118. }
  119. // Convert colors to premultiplied alpha, which is necessary for correct alpha compositing.
  120. byte* pixels = static_cast<byte*>(surface->pixels);
  121. for (int i = 0; i < surface->w * surface->h * 4; i += 4)
  122. {
  123. const byte alpha = pixels[i + 3];
  124. for (int j = 0; j < 3; ++j)
  125. pixels[i + j] = byte(int(pixels[i + j]) * int(alpha) / 255);
  126. }
  127. SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
  128. texture_dimensions = Rml::Vector2i(surface->w, surface->h);
  129. SDL_FreeSurface(surface);
  130. if (texture)
  131. SDL_SetTextureBlendMode(texture, blend_mode);
  132. return (Rml::TextureHandle)texture;
  133. }
  134. Rml::TextureHandle RenderInterface_SDL::GenerateTexture(Rml::Span<const Rml::byte> source, Rml::Vector2i source_dimensions)
  135. {
  136. SDL_Surface* surface = SDL_CreateRGBSurfaceWithFormatFrom((void*)source.data(), source_dimensions.x, source_dimensions.y, 32,
  137. source_dimensions.x * 4, SDL_PIXELFORMAT_RGBA32);
  138. SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
  139. SDL_SetTextureBlendMode(texture, blend_mode);
  140. SDL_FreeSurface(surface);
  141. return (Rml::TextureHandle)texture;
  142. }
  143. void RenderInterface_SDL::ReleaseTexture(Rml::TextureHandle texture_handle)
  144. {
  145. SDL_DestroyTexture((SDL_Texture*)texture_handle);
  146. }