RmlUi_Renderer_SDL.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. void RenderInterface_SDL::BeginFrame()
  36. {
  37. SDL_RenderSetViewport(renderer, nullptr);
  38. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  39. SDL_RenderClear(renderer);
  40. SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
  41. }
  42. void RenderInterface_SDL::EndFrame() {}
  43. void RenderInterface_SDL::RenderGeometry(Rml::Vertex* vertices, int num_vertices, int* indices, int num_indices, const Rml::TextureHandle texture,
  44. const Rml::Vector2f& translation)
  45. {
  46. SDL_FPoint* positions = new SDL_FPoint[num_vertices];
  47. for (int i = 0; i < num_vertices; i++)
  48. {
  49. positions[i].x = vertices[i].position.x + translation.x;
  50. positions[i].y = vertices[i].position.y + translation.y;
  51. }
  52. SDL_Texture* sdl_texture = (SDL_Texture*)texture;
  53. SDL_RenderGeometryRaw(renderer, sdl_texture, &positions[0].x, sizeof(SDL_FPoint), (const SDL_Color*)&vertices->colour, sizeof(Rml::Vertex),
  54. &vertices->tex_coord.x, sizeof(Rml::Vertex), num_vertices, indices, num_indices, 4);
  55. delete[] positions;
  56. }
  57. void RenderInterface_SDL::EnableScissorRegion(bool enable)
  58. {
  59. if (enable)
  60. SDL_RenderSetClipRect(renderer, &rect_scissor);
  61. else
  62. SDL_RenderSetClipRect(renderer, nullptr);
  63. scissor_region_enabled = enable;
  64. }
  65. void RenderInterface_SDL::SetScissorRegion(int x, int y, int width, int height)
  66. {
  67. rect_scissor.x = x;
  68. rect_scissor.y = y;
  69. rect_scissor.w = width;
  70. rect_scissor.h = height;
  71. if (scissor_region_enabled)
  72. SDL_RenderSetClipRect(renderer, &rect_scissor);
  73. }
  74. bool RenderInterface_SDL::LoadTexture(Rml::TextureHandle& texture_handle, Rml::Vector2i& texture_dimensions, const Rml::String& source)
  75. {
  76. Rml::FileInterface* file_interface = Rml::GetFileInterface();
  77. Rml::FileHandle file_handle = file_interface->Open(source);
  78. if (!file_handle)
  79. return false;
  80. file_interface->Seek(file_handle, 0, SEEK_END);
  81. size_t buffer_size = file_interface->Tell(file_handle);
  82. file_interface->Seek(file_handle, 0, SEEK_SET);
  83. char* buffer = new char[buffer_size];
  84. file_interface->Read(buffer, buffer_size, file_handle);
  85. file_interface->Close(file_handle);
  86. const size_t i = source.rfind('.');
  87. Rml::String extension = (i == Rml::String::npos ? Rml::String() : source.substr(i + 1));
  88. SDL_Surface* surface = IMG_LoadTyped_RW(SDL_RWFromMem(buffer, int(buffer_size)), 1, extension.c_str());
  89. bool success = false;
  90. if (surface)
  91. {
  92. SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
  93. if (texture)
  94. {
  95. texture_handle = (Rml::TextureHandle)texture;
  96. texture_dimensions = Rml::Vector2i(surface->w, surface->h);
  97. success = true;
  98. }
  99. SDL_FreeSurface(surface);
  100. }
  101. delete[] buffer;
  102. return success;
  103. }
  104. bool RenderInterface_SDL::GenerateTexture(Rml::TextureHandle& texture_handle, const Rml::byte* source, const Rml::Vector2i& source_dimensions)
  105. {
  106. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  107. Uint32 rmask = 0xff000000;
  108. Uint32 gmask = 0x00ff0000;
  109. Uint32 bmask = 0x0000ff00;
  110. Uint32 amask = 0x000000ff;
  111. #else
  112. Uint32 rmask = 0x000000ff;
  113. Uint32 gmask = 0x0000ff00;
  114. Uint32 bmask = 0x00ff0000;
  115. Uint32 amask = 0xff000000;
  116. #endif
  117. SDL_Surface* surface =
  118. SDL_CreateRGBSurfaceFrom((void*)source, source_dimensions.x, source_dimensions.y, 32, source_dimensions.x * 4, rmask, gmask, bmask, amask);
  119. SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
  120. SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
  121. SDL_FreeSurface(surface);
  122. texture_handle = (Rml::TextureHandle)texture;
  123. return true;
  124. }
  125. void RenderInterface_SDL::ReleaseTexture(Rml::TextureHandle texture_handle)
  126. {
  127. SDL_DestroyTexture((SDL_Texture*)texture_handle);
  128. }