FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
atlasbook.h
1 /***************************************************************************
2  * Copyright (C) 2005-2011 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 #ifndef FIFE_VIDEO_ATLASBOOK_H
23 #define FIFE_VIDEO_ATLASBOOK_H
24 
25 // Standard C++ library includes
26 #include <vector>
27 #include <cassert>
28 #include <cmath>
29 
30 // 3rd party library includes
31 
32 // FIFE includes
33 // These includes are split up in two parts, separated by one empty line
34 // First block: files included from the FIFE root src directory
35 // Second block: files included from the same folder
36 #include "util/structures/rect.h"
37 
38 namespace FIFE {
39 
40  class AtlasBlock {
41  public:
42  uint32_t page;
43  uint32_t left, right, top, bottom;
44 
45  AtlasBlock(const Rect& rect, uint32_t page)
46  : page(page),
47  left(rect.x), right(rect.right()),
48  top(rect.y), bottom(rect.bottom()){
49  }
50 
51  AtlasBlock() {
52  }
53 
54  // (0,0) [left] [right]
55  // +--------------------
56  // |
57  // [top] | +---------+
58  // | | |
59  // | | |
60  // | | |
61  // | | |
62  // [bottom] | +---------+
63  // |
64 
65  // bottom > top
66  // right > left
67 
68  void setTrivial() {
69  left = right = top = bottom = 0;
70  }
71 
72  bool isTrivial() const {
73  return getWidth() == 0 || getHeight() == 0;
74  }
75 
76  uint32_t getWidth() const { return right - left; }
77  uint32_t getHeight() const { return bottom - top; }
78 
79  AtlasBlock intersects(AtlasBlock const& rect) const;
80  void merge(AtlasBlock const& rect);
81  };
82 
83  class AtlasPage {
84  public:
85  AtlasPage(uint32_t width, uint32_t height,
86  uint32_t pixelSize, uint32_t page)
87  : width(width), height(height), pixelSize(pixelSize),
88  page(page), freePixels(width*height*pixelSize){
89  }
90 
91  AtlasBlock* getBlock(uint32_t width, uint32_t height);
92  void shrink(bool pot);
93 
94  uint32_t getWidth() const {
95  return width;
96  }
97 
98  uint32_t getHeight() const {
99  return height;
100  }
101 
102  private:
103  AtlasBlock const* intersects(AtlasBlock const* block) const;
104 
105  uint32_t width, height;
106  uint32_t pixelSize;
107  uint32_t page;
108  int32_t freePixels;
109 
110  typedef std::vector<AtlasBlock> Blocks;
111  Blocks blocks;
112  };
113 
114  class AtlasBook {
115  public:
116 
117  AtlasBook(uint32_t pageWidth, uint32_t pageHeight,
118  uint32_t pixelSize = 4)
119  : pageWidth(pageWidth), pageHeight(pageHeight),
120  pixelSize(pixelSize) {
121  }
122 
123  AtlasBlock* getBlock(uint32_t width, uint32_t height);
124 
125  // try to shrink every atlas page
126  void shrink(bool pot);
127 
128  AtlasPage& getPage(size_t index) {
129  return pages[index];
130  }
131 
132  private:
133  // add new atlas to atlas container
134  AtlasPage* extendCache(uint32_t minPageWidth, uint32_t minPageHeight);
135 
136  // How big the new atlases should be
137  uint32_t pageWidth, pageHeight;
138  uint32_t pixelSize;
139 
140  typedef std::vector<AtlasPage> Pages;
141  Pages pages;
142  };
143 }
144 
145 #endif
146 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */