1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
2 
3 #ifndef TEXTURE_ATLAS_H
4 #define TEXTURE_ATLAS_H
5 
6 #include <string>
7 #include <vector>
8 #include <map>
9 
10 #include "System/creg/creg_cond.h"
11 #include "System/float4.h"
12 #include "System/type2.h"
13 
14 
15 class IAtlasAllocator;
16 
17 /** @brief texture coordinates of an atlas subimage. */
18 //typedef float4 AtlasedTexture;
19 
20 struct AtlasedTexture : public float4
21 {
AtlasedTextureAtlasedTexture22 	AtlasedTexture() : float4() {}
AtlasedTextureAtlasedTexture23 	AtlasedTexture(const float4& f) : float4(f) {}
24 
25 	CR_DECLARE_STRUCT(AtlasedTexture)
26 };
27 
28 
29 
30 /**
31  * @brief Same as AtlasedTexture, but with a different name,
32  * so the explosiongenerator can differentiate between different atlases.
33  */
34 struct GroundFXTexture : public AtlasedTexture
35 {
36 	CR_DECLARE_STRUCT(GroundFXTexture)
37 };
38 
39 
40 /** @brief Class for combining multiple bitmaps into one large single bitmap. */
41 class CTextureAtlas
42 {
43 public:
44 	enum TextureType {
45 		RGBA32
46 	};
47 	enum {
48 		ATLAS_ALLOC_LEGACY   = 0,
49 		ATLAS_ALLOC_QUADTREE = 1,
50 	};
51 
52 public:
53 	CTextureAtlas(unsigned int allocType = ATLAS_ALLOC_LEGACY);
54 	~CTextureAtlas();
55 
56 	//! Add a texture from a memory pointer returns -1 if failed.
57 	int AddTexFromMem(std::string name, int xsize, int ysize, TextureType texType, void* data);
58 
59 	/**
60 	 * Returns a memory pointer to the texture pixel data array.
61 	 * (reduces redundant memcpy in contrast to AddTexFromMem())
62 	 */
63 	void* AddTex(std::string name, int xsize, int ysize, TextureType texType = RGBA32);
64 
65 	//! Add a texture from a file, returns -1 if failed.
66 	int AddTexFromFile(std::string name, std::string file);
67 
68 
69 	/**
70 	 * Creates the atlas containing all the specified textures.
71 	 * @return true if suceeded, false if not all textures did fit
72 	 *         into the specified maxsize.
73 	 */
74 	bool Finalize();
75 
76 	/**
77 	 * @return a boolean true if the texture exists within
78 	 *         the "textures" map and false if it does not.
79 	 */
80 	bool TextureExists(const std::string& name);
81 
82 
83 	//! @return reference to the Texture struct of the specified texture
84 	AtlasedTexture& GetTexture(const std::string& name);
85 
86 	/**
87 	 * @return a Texture struct of the specified texture if it exists,
88 	 *         otherwise return a backup texture.
89 	 */
90 	AtlasedTexture& GetTextureWithBackup(const std::string& name, const std::string& backupName);
91 
92 
GetAllocator()93 	IAtlasAllocator* GetAllocator() { return atlasAllocator; }
94 
95 	int2 GetSize() const;
GetName()96 	std::string GetName() const { return name; }
97 
GetTexID()98 	unsigned int GetTexID() const { return atlasTexID; }
99 
100 	void BindTexture();
SetFreeTexture(bool b)101 	void SetFreeTexture(bool b) { freeTexture = b; }
SetName(const std::string & s)102 	void SetName(const std::string& s) { name = s; }
103 
SetDebug(bool b)104 	static void SetDebug(bool b) { debug = true; }
GetDebug()105 	static bool GetDebug() { return debug; }
106 
107 protected:
108 	int GetBPP(TextureType tetxType);
109 	void CreateTexture();
110 
111 protected:
112 	IAtlasAllocator* atlasAllocator;
113 
114 	struct MemTex
115 	{
116 		std::vector<std::string> names;
117 		int xsize, ysize;
118 		TextureType texType;
119 		void* data;
120 	};
121 
122 	std::string name;
123 
124 	// temporary storage of all textures
125 	std::vector<MemTex*> memtextures;
126 	std::map<std::string, MemTex*> files;
127 
128 	std::map<std::string, AtlasedTexture> textures;
129 
130 	unsigned int atlasTexID;
131 
132 	//! set to true to write finalized texture atlas to disk
133 	static bool debug;
134 
135 	bool initialized;
136 	bool freeTexture; //! free texture on atlas destruction?
137 };
138 
139 #endif // TEXTURE_ATLAS_H
140