Geometry.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 "../../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. Geometry::Geometry()
  38. {
  39. database_handle = GeometryDatabase::Insert(this);
  40. }
  41. Geometry::Geometry(Geometry&& other) noexcept
  42. {
  43. MoveFrom(other);
  44. database_handle = GeometryDatabase::Insert(this);
  45. }
  46. Geometry& Geometry::operator=(Geometry&& other) noexcept
  47. {
  48. MoveFrom(other);
  49. // Keep the database handles from construction unchanged, they are tied to the *this* pointer and should not change.
  50. return *this;
  51. }
  52. void Geometry::MoveFrom(Geometry& other) noexcept
  53. {
  54. vertices = std::move(other.vertices);
  55. indices = std::move(other.indices);
  56. texture = std::exchange(other.texture, nullptr);
  57. compiled_geometry = std::exchange(other.compiled_geometry, 0);
  58. compile_attempted = std::exchange(other.compile_attempted, false);
  59. }
  60. Geometry::~Geometry()
  61. {
  62. GeometryDatabase::Erase(database_handle);
  63. Release();
  64. }
  65. void Geometry::Render(Vector2f translation)
  66. {
  67. RenderInterface* const render_interface = ::Rml::GetRenderInterface();
  68. RMLUI_ASSERT(render_interface);
  69. translation = translation.Round();
  70. // Render our compiled geometry if possible.
  71. if (compiled_geometry)
  72. {
  73. RMLUI_ZoneScopedN("RenderCompiled");
  74. render_interface->RenderCompiledGeometry(compiled_geometry, translation);
  75. }
  76. // Otherwise, if we actually have geometry, try to compile it if we haven't already done so, otherwise render it in
  77. // immediate mode.
  78. else
  79. {
  80. if (vertices.empty() || indices.empty())
  81. return;
  82. RMLUI_ZoneScopedN("RenderGeometry");
  83. if (!compile_attempted)
  84. {
  85. compile_attempted = true;
  86. compiled_geometry = render_interface->CompileGeometry(&vertices[0], (int)vertices.size(), &indices[0], (int)indices.size(),
  87. texture ? texture->GetHandle() : 0);
  88. // If we managed to compile the geometry, we can clear the local copy of vertices and indices and
  89. // immediately render the compiled version.
  90. if (compiled_geometry)
  91. {
  92. render_interface->RenderCompiledGeometry(compiled_geometry, translation);
  93. return;
  94. }
  95. }
  96. // Either we've attempted to compile before (and failed), or the compile we just attempted failed; either way,
  97. // render the uncompiled version.
  98. render_interface->RenderGeometry(&vertices[0], (int)vertices.size(), &indices[0], (int)indices.size(), texture ? texture->GetHandle() : 0,
  99. translation);
  100. }
  101. }
  102. Vector<Vertex>& Geometry::GetVertices()
  103. {
  104. return vertices;
  105. }
  106. Vector<int>& Geometry::GetIndices()
  107. {
  108. return indices;
  109. }
  110. const Texture* Geometry::GetTexture() const
  111. {
  112. return texture;
  113. }
  114. void Geometry::SetTexture(const Texture* _texture)
  115. {
  116. texture = _texture;
  117. Release();
  118. }
  119. void Geometry::Release(bool clear_buffers)
  120. {
  121. if (compiled_geometry)
  122. {
  123. ::Rml::GetRenderInterface()->ReleaseCompiledGeometry(compiled_geometry);
  124. compiled_geometry = 0;
  125. }
  126. compile_attempted = false;
  127. if (clear_buffers)
  128. {
  129. vertices.clear();
  130. indices.clear();
  131. }
  132. }
  133. Geometry::operator bool() const
  134. {
  135. return !indices.empty();
  136. }
  137. } // namespace Rml