TextureLayout.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "TextureLayout.h"
  2. #include "TextureLayoutRectangle.h"
  3. #include "TextureLayoutTexture.h"
  4. #include <algorithm>
  5. namespace Rml {
  6. struct RectangleSort {
  7. bool operator()(const TextureLayoutRectangle& lhs, const TextureLayoutRectangle& rhs) const
  8. {
  9. return lhs.GetDimensions().y > rhs.GetDimensions().y;
  10. }
  11. };
  12. TextureLayout::TextureLayout() {}
  13. TextureLayout::~TextureLayout() {}
  14. void TextureLayout::AddRectangle(int id, Vector2i dimensions)
  15. {
  16. rectangles.push_back(TextureLayoutRectangle(id, dimensions));
  17. }
  18. TextureLayoutRectangle& TextureLayout::GetRectangle(int index)
  19. {
  20. RMLUI_ASSERT(index >= 0);
  21. RMLUI_ASSERT(index < GetNumRectangles());
  22. return rectangles[index];
  23. }
  24. int TextureLayout::GetNumRectangles() const
  25. {
  26. return (int)rectangles.size();
  27. }
  28. TextureLayoutTexture& TextureLayout::GetTexture(int index)
  29. {
  30. RMLUI_ASSERT(index >= 0);
  31. RMLUI_ASSERT(index < GetNumTextures());
  32. return textures[index];
  33. }
  34. int TextureLayout::GetNumTextures() const
  35. {
  36. return (int)textures.size();
  37. }
  38. bool TextureLayout::GenerateLayout(int max_texture_dimensions)
  39. {
  40. // Sort the rectangles by height.
  41. std::sort(rectangles.begin(), rectangles.end(), RectangleSort());
  42. int num_placed_rectangles = 0;
  43. while (num_placed_rectangles != GetNumRectangles())
  44. {
  45. TextureLayoutTexture texture;
  46. int texture_size = texture.Generate(*this, max_texture_dimensions);
  47. if (texture_size == 0)
  48. return false;
  49. textures.push_back(texture);
  50. num_placed_rectangles += texture_size;
  51. }
  52. return true;
  53. }
  54. } // namespace Rml