FIFE  be64c707dea6b3250bd4355bf5c825d25920087d
atlasbook.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2019 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 #include <algorithm>
30 
31 // 3rd party library includes
32 
33 // FIFE includes
34 // These includes are split up in two parts, separated by one empty line
35 // First block: files included from the FIFE root src directory
36 // Second block: files included from the same folder
37 #include "util/structures/rect.h"
38 
39 namespace FIFE {
40 
41  class AtlasBlock {
42  public:
45 
46  AtlasBlock(const Rect& rect, uint32_t page)
47  : page(page),
48  left(rect.x), right(rect.right()),
49  top(rect.y), bottom(rect.bottom()){
50  }
51 
53  }
54 
55  // (0,0) [left] [right]
56  // +--------------------
57  // |
58  // [top] | +---------+
59  // | | |
60  // | | |
61  // | | |
62  // | | |
63  // [bottom] | +---------+
64  // |
65 
66  // bottom > top
67  // right > left
68 
69  void setTrivial() {
70  left = right = top = bottom = 0;
71  }
72 
73  bool isTrivial() const {
74  return getWidth() == 0 || getHeight() == 0;
75  }
76 
77  uint32_t getWidth() const { return right - left; }
78  uint32_t getHeight() const { return bottom - top; }
79 
80  AtlasBlock intersects(AtlasBlock const& rect) const;
81  void merge(AtlasBlock const& rect);
82  };
83 
84  class AtlasPage {
85  public:
86  AtlasPage(uint32_t width, uint32_t height,
87  uint32_t pixelSize, uint32_t page)
88  : width(width), height(height), pixelSize(pixelSize),
89  page(page), freePixels(width*height*pixelSize){
90  }
91 
92  AtlasBlock* getBlock(uint32_t width, uint32_t height);
93  void shrink(bool pot);
94 
95  uint32_t getWidth() const {
96  return width;
97  }
98 
99  uint32_t getHeight() const {
100  return height;
101  }
102 
103  private:
104  AtlasBlock const* intersects(AtlasBlock const* block) const;
105 
106  uint32_t width, height;
109  int32_t freePixels;
110 
111  typedef std::vector<AtlasBlock> Blocks;
112  Blocks blocks;
113  };
114 
115  class AtlasBook {
116  public:
117 
118  AtlasBook(uint32_t pageWidth, uint32_t pageHeight,
119  uint32_t pixelSize = 4)
120  : pageWidth(pageWidth), pageHeight(pageHeight),
121  pixelSize(pixelSize) {
122  }
123 
124  AtlasBlock* getBlock(uint32_t width, uint32_t height);
125 
126  // try to shrink every atlas page
127  void shrink(bool pot);
128 
129  AtlasPage& getPage(size_t index) {
130  return pages[index];
131  }
132 
133  private:
134  // add new atlas to atlas container
135  AtlasPage* extendCache(uint32_t minPageWidth, uint32_t minPageHeight);
136 
137  // How big the new atlases should be
138  uint32_t pageWidth, pageHeight;
140 
141  typedef std::vector<AtlasPage> Pages;
142  Pages pages;
143  };
144 }
145 
146 #endif
uint32_t page
Definition: atlasbook.h:43
uint32_t pageWidth
Definition: atlasbook.h:138
uint32_t pixelSize
Definition: atlasbook.h:139
int32_t freePixels
Definition: atlasbook.h:109
std::vector< AtlasBlock > Blocks
Definition: atlasbook.h:111
uint32_t bottom
Definition: atlasbook.h:44
uint32_t getWidth() const
Definition: atlasbook.h:95
AtlasBlock(const Rect &rect, uint32_t page)
Definition: atlasbook.h:46
uint32_t pixelSize
Definition: atlasbook.h:107
uint32_t right
Definition: atlasbook.h:44
bool isTrivial() const
Definition: atlasbook.h:73
void merge(AtlasBlock const &rect)
Definition: atlasbook.cpp:46
AtlasBook(uint32_t pageWidth, uint32_t pageHeight, uint32_t pixelSize=4)
Definition: atlasbook.h:118
uint32_t top
Definition: atlasbook.h:44
uint32_t width
Definition: atlasbook.h:106
uint32_t getHeight() const
Definition: atlasbook.h:99
AtlasPage & getPage(size_t index)
Definition: atlasbook.h:129
uint32_t getHeight() const
Definition: atlasbook.h:78
std::vector< AtlasPage > Pages
Definition: atlasbook.h:141
uint32_t page
Definition: atlasbook.h:108
AtlasPage(uint32_t width, uint32_t height, uint32_t pixelSize, uint32_t page)
Definition: atlasbook.h:86
uint32_t left
Definition: atlasbook.h:44
AtlasBlock intersects(AtlasBlock const &rect) const
Definition: atlasbook.cpp:31
unsigned int uint32_t
Definition: core.h:40
void setTrivial()
Definition: atlasbook.h:69
uint32_t getWidth() const
Definition: atlasbook.h:77