Geometry.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 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 "../../Include/RmlUi/Core/Geometry.h"
  29. #include "../../Include/RmlUi/Core/Context.h"
  30. #include "../../Include/RmlUi/Core/Core.h"
  31. #include "../../Include/RmlUi/Core/Element.h"
  32. #include "../../Include/RmlUi/Core/Profiling.h"
  33. #include "../../Include/RmlUi/Core/RenderInterface.h"
  34. #include "GeometryDatabase.h"
  35. #include <utility>
  36. namespace Rml {
  37. namespace Core {
  38. Geometry::Geometry(Element* _host_element)
  39. {
  40. database_handle = GeometryDatabase::Insert(this);
  41. }
  42. Geometry::Geometry(Context* _host_context)
  43. {
  44. database_handle = GeometryDatabase::Insert(this);
  45. }
  46. Geometry::Geometry(Geometry&& other)
  47. {
  48. MoveFrom(other);
  49. database_handle = GeometryDatabase::Insert(this);
  50. }
  51. Geometry& Geometry::operator=(Geometry&& other)
  52. {
  53. MoveFrom(other);
  54. // Keep the database handles from construction unchanged, they are tied to the *this* pointer and should not change.
  55. return *this;
  56. }
  57. void Geometry::MoveFrom(Geometry& other)
  58. {
  59. host_context = std::exchange(other.host_context, nullptr);
  60. host_element = std::exchange(other.host_element, nullptr);
  61. vertices = std::move(other.vertices);
  62. indices = std::move(other.indices);
  63. texture = std::exchange(other.texture, nullptr);
  64. compiled_geometry = std::exchange(other.compiled_geometry, 0);
  65. compile_attempted = std::exchange(other.compile_attempted, false);
  66. }
  67. Geometry::~Geometry()
  68. {
  69. GeometryDatabase::Erase(database_handle);
  70. Release();
  71. }
  72. // Set the host element for this geometry; this should be passed in the constructor if possible.
  73. void Geometry::SetHostElement(Element* _host_element)
  74. {
  75. if (host_element == _host_element)
  76. return;
  77. if (host_element != nullptr)
  78. {
  79. Release();
  80. host_context = nullptr;
  81. }
  82. host_element = _host_element;
  83. }
  84. void Geometry::Render(const Vector2f& translation)
  85. {
  86. RenderInterface* render_interface = GetRenderInterface();
  87. if (render_interface == nullptr)
  88. return;
  89. // Render our compiled geometry if possible.
  90. if (compiled_geometry)
  91. {
  92. RMLUI_ZoneScopedN("RenderCompiled");
  93. render_interface->RenderCompiledGeometry(compiled_geometry, translation);
  94. }
  95. // Otherwise, if we actually have geometry, try to compile it if we haven't already done so, otherwise render it in
  96. // immediate mode.
  97. else
  98. {
  99. if (vertices.empty() ||
  100. indices.empty())
  101. return;
  102. RMLUI_ZoneScopedN("RenderGeometry");
  103. if (!compile_attempted)
  104. {
  105. compile_attempted = true;
  106. compiled_geometry = render_interface->CompileGeometry(&vertices[0], (int) vertices.size(), &indices[0], (int) indices.size(), texture != nullptr ? texture->GetHandle(GetRenderInterface()) : 0);
  107. // If we managed to compile the geometry, we can clear the local copy of vertices and indices and
  108. // immediately render the compiled version.
  109. if (compiled_geometry)
  110. {
  111. render_interface->RenderCompiledGeometry(compiled_geometry, translation);
  112. return;
  113. }
  114. }
  115. // Either we've attempted to compile before (and failed), or the compile we just attempted failed; either way,
  116. // render the uncompiled version.
  117. render_interface->RenderGeometry(&vertices[0], (int) vertices.size(), &indices[0], (int) indices.size(), texture != nullptr ? texture->GetHandle(GetRenderInterface()) : 0, translation);
  118. }
  119. }
  120. // Returns the geometry's vertices. If these are written to, Release() should be called to force a recompile.
  121. std::vector< Vertex >& Geometry::GetVertices()
  122. {
  123. return vertices;
  124. }
  125. // Returns the geometry's indices. If these are written to, Release() should be called to force a recompile.
  126. std::vector< int >& Geometry::GetIndices()
  127. {
  128. return indices;
  129. }
  130. // Gets the geometry's texture.
  131. const Texture* Geometry::GetTexture() const
  132. {
  133. return texture;
  134. }
  135. // Sets the geometry's texture.
  136. void Geometry::SetTexture(const Texture* _texture)
  137. {
  138. texture = _texture;
  139. Release();
  140. }
  141. void Geometry::Release(bool clear_buffers)
  142. {
  143. if (compiled_geometry)
  144. {
  145. GetRenderInterface()->ReleaseCompiledGeometry(compiled_geometry);
  146. compiled_geometry = 0;
  147. }
  148. compile_attempted = false;
  149. if (clear_buffers)
  150. {
  151. vertices.clear();
  152. indices.clear();
  153. }
  154. }
  155. // Returns the host context's render interface.
  156. RenderInterface* Geometry::GetRenderInterface()
  157. {
  158. if (host_context == nullptr)
  159. {
  160. if (host_element != nullptr)
  161. host_context = host_element->GetContext();
  162. }
  163. if (host_context == nullptr)
  164. return Rml::Core::GetRenderInterface();
  165. else
  166. return host_context->GetRenderInterface();
  167. }
  168. }
  169. }