1 /*
2  * Copyright 2011-2012 Arx Libertatis Team (see the AUTHORS file)
3  *
4  * This file is part of Arx Libertatis.
5  *
6  * Arx Libertatis is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Arx Libertatis 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Arx Libertatis.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef ARX_GRAPHICS_TEXTURE_PACKEDTEXTURE_H
21 #define ARX_GRAPHICS_TEXTURE_PACKEDTEXTURE_H
22 
23 #include <vector>
24 #include <stddef.h>
25 
26 #include "graphics/image/Image.h"
27 #include "math/Rectangle.h"
28 #include "math/Vector2.h"
29 
30 class Texture2D;
31 
32 class PackedTexture {
33 
34 public:
35 
36 	PackedTexture(unsigned int textureSize, Image::Format pFormat);
37 	~PackedTexture();
38 
39 	//! Reset the packed texture - remove all images
40 	void clear();
41 
42 	//! Upload changed textures
43 	void upload();
44 
45 	bool insertImage(const Image & image, unsigned int & textureIndex, Vec2i & offset);
46 
47 	Texture2D & getTexture(unsigned int index);
48 
getTextureSize()49 	unsigned int getTextureSize() const { return textureSize; }
getTextureCount()50 	size_t getTextureCount() const { return textures.size(); }
51 
52 protected:
53 
54 	class TextureTree {
55 
56 	public:
57 
58 		struct Node {
59 
60 			Node();
61 			~Node();
62 
63 			Node * insertImage(const Image & pImg);
64 
65 			Node * children[2];
66 			Rect rect;
67 			bool used;
68 		};
69 
70 		explicit TextureTree(unsigned int textureSize, Image::Format textureFormat);
71 		~TextureTree();
72 
73 		Node * insertImage(const Image & pImg);
74 
75 	private:
76 
77 		Node root;
78 
79 	public:
80 
81 		Texture2D * texture;
82 		bool dirty;
83 	};
84 
85 private:
86 
87 	std::vector<TextureTree *> textures;
88 	typedef std::vector<TextureTree *>::iterator texture_iterator;
89 
90 	const unsigned int textureSize;
91 	Image::Format textureFormat;
92 
93 };
94 
95 
96 #endif // ARX_GRAPHICS_TEXTURE_PACKEDTEXTURE_H
97