1 // Aseprite Gfx Library
2 // Copyright (C) 2001-2015 David Capello
3 //
4 // This file is released under the terms of the MIT license.
5 // Read LICENSE.txt for more information.
6 
7 #ifndef GFX_TEXTURE_SIZE_H_INCLUDED
8 #define GFX_TEXTURE_SIZE_H_INCLUDED
9 #pragma once
10 
11 #include "gfx/fwd.h"
12 #include "gfx/rect.h"
13 #include <vector>
14 
15 namespace gfx {
16 
17   // TODO add support for rotations
18   class PackingRects {
19   public:
20     typedef std::vector<Rect> Rects;
21     typedef Rects::const_iterator const_iterator;
22 
23     // Iterate over all given rectangles (in the same order they where
24     // given in addSize() calls).
begin()25     const_iterator begin() const { return m_rects.begin(); }
end()26     const_iterator end() const { return m_rects.end(); }
27 
size()28     std::size_t size() const { return m_rects.size(); }
29     const Rect& operator[](int i) const { return m_rects[i]; }
30 
31     // Adds a new rectangle.
32     void add(const Size& sz);
33     void add(const Rect& rc);
34 
35     // Returns the best size for the texture.
36     Size bestFit();
37 
38     // Rearrange all given rectangles to best fit a texture size.
39     // Returns true if all rectangles were correctly arranged or false
40     // if there is not enough space.
41     bool pack(const Size& size);
42 
43     // Returns the bounds of the packed area.
bounds()44     const Rect& bounds() const { return m_bounds; }
45 
46   private:
47     Rect m_bounds;
48     Rects m_rects;
49   };
50 
51 } // namespace gfx
52 
53 #endif
54