FIFE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
atlasbook.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2013 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:
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 
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:
87  : width(width), height(height), pixelSize(pixelSize),
88  page(page), freePixels(width*height*pixelSize){
89  }
90 
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 
108  int32_t freePixels;
109 
110  typedef std::vector<AtlasBlock> Blocks;
112  };
113 
114  class AtlasBook {
115  public:
116 
118  uint32_t pixelSize = 4)
119  : pageWidth(pageWidth), pageHeight(pageHeight),
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
139 
140  typedef std::vector<AtlasPage> Pages;
142  };
143 }
144 
145 #endif
146 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */
uint32_t page
Definition: atlasbook.h:42
AtlasBlock * getBlock(uint32_t width, uint32_t height)
Definition: atlasbook.cpp:197
uint32_t pageWidth
Definition: atlasbook.h:137
uint32_t pixelSize
Definition: atlasbook.h:138
uint32_t pageHeight
Definition: atlasbook.h:137
int32_t freePixels
Definition: atlasbook.h:108
std::vector< AtlasBlock > Blocks
Definition: atlasbook.h:110
uint32_t bottom
Definition: atlasbook.h:43
uint32_t getWidth() const
Definition: atlasbook.h:76
AtlasBlock(const Rect &rect, uint32_t page)
Definition: atlasbook.h:45
AtlasBlock intersects(AtlasBlock const &rect) const
Definition: atlasbook.cpp:31
AtlasBlock const * intersects(AtlasBlock const *block) const
Definition: atlasbook.cpp:188
uint32_t pixelSize
Definition: atlasbook.h:106
uint32_t getHeight() const
Definition: atlasbook.h:77
uint32_t right
Definition: atlasbook.h:43
void merge(AtlasBlock const &rect)
Definition: atlasbook.cpp:46
AtlasBook(uint32_t pageWidth, uint32_t pageHeight, uint32_t pixelSize=4)
Definition: atlasbook.h:117
uint32_t top
Definition: atlasbook.h:43
uint32_t height
Definition: atlasbook.h:105
uint32_t width
Definition: atlasbook.h:105
AtlasPage * extendCache(uint32_t minPageWidth, uint32_t minPageHeight)
Definition: atlasbook.cpp:207
AtlasPage & getPage(size_t index)
Definition: atlasbook.h:128
bool isTrivial() const
Definition: atlasbook.h:72
AtlasBlock * getBlock(uint32_t width, uint32_t height)
Definition: atlasbook.cpp:53
std::vector< AtlasPage > Pages
Definition: atlasbook.h:140
uint32_t page
Definition: atlasbook.h:107
AtlasPage(uint32_t width, uint32_t height, uint32_t pixelSize, uint32_t page)
Definition: atlasbook.h:85
uint32_t getHeight() const
Definition: atlasbook.h:98
uint32_t left
Definition: atlasbook.h:43
void shrink(bool pot)
Definition: atlasbook.cpp:220
unsigned int uint32_t
Definition: core.h:40
uint32_t getWidth() const
Definition: atlasbook.h:94
void shrink(bool pot)
Definition: atlasbook.cpp:155
void setTrivial()
Definition: atlasbook.h:68