Geometry.cpp 4.7 KB

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