TextureLayoutRow.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "TextureLayoutRow.h"
  2. #include "TextureLayout.h"
  3. namespace Rml {
  4. TextureLayoutRow::TextureLayoutRow()
  5. {
  6. height = 0;
  7. }
  8. TextureLayoutRow::~TextureLayoutRow() {}
  9. int TextureLayoutRow::Generate(TextureLayout& layout, int max_width, int y)
  10. {
  11. int width = 1;
  12. int first_unplaced_index = 0;
  13. int placed_rectangles = 0;
  14. while (width < max_width)
  15. {
  16. // Find the first unplaced rectangle we can fit.
  17. int index;
  18. for (index = first_unplaced_index; index < layout.GetNumRectangles(); ++index)
  19. {
  20. TextureLayoutRectangle& rectangle = layout.GetRectangle(index);
  21. if (!rectangle.IsPlaced())
  22. {
  23. if (width + rectangle.GetDimensions().x + 1 <= max_width)
  24. break;
  25. }
  26. }
  27. if (index == layout.GetNumRectangles())
  28. return placed_rectangles;
  29. TextureLayoutRectangle& rectangle = layout.GetRectangle(index);
  30. // Increment the row height if necessary.
  31. height = Math::Max(height, rectangle.GetDimensions().y);
  32. // Add this glyph onto our list and mark it as placed.
  33. rectangles.push_back(&rectangle);
  34. rectangle.Place(layout.GetNumTextures(), Vector2i(width, y));
  35. ++placed_rectangles;
  36. // Increment our width. An extra pixel is added on so the rectangles aren't pushed up
  37. // against each other. This will avoid filtering artifacts.
  38. if (rectangle.GetDimensions().x > 0)
  39. width += rectangle.GetDimensions().x + 1;
  40. first_unplaced_index = index + 1;
  41. }
  42. return placed_rectangles;
  43. }
  44. void TextureLayoutRow::Allocate(byte* texture_data, int stride)
  45. {
  46. for (size_t i = 0; i < rectangles.size(); ++i)
  47. rectangles[i]->Allocate(texture_data, stride);
  48. }
  49. int TextureLayoutRow::GetHeight() const
  50. {
  51. return height;
  52. }
  53. void TextureLayoutRow::Unplace()
  54. {
  55. for (size_t i = 0; i < rectangles.size(); ++i)
  56. rectangles[i]->Unplace();
  57. }
  58. } // namespace Rml