1 /* Copyright (C) 2015 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_TERRAINTEXTUREMANAGER
19 #define INCLUDED_TERRAINTEXTUREMANAGER
20 
21 #include <map>
22 #include <memory>
23 #include <vector>
24 
25 #include "lib/res/graphics/ogl_tex.h"
26 #include "lib/res/handle.h"
27 #include "lib/file/vfs/vfs_path.h"
28 #include "ps/CStr.h"
29 #include "ps/Singleton.h"
30 
31 // access to sole CTerrainTextureManager object
32 #define g_TexMan CTerrainTextureManager::GetSingleton()
33 
34 #define NUM_ALPHA_MAPS 14
35 
36 class XMBElement;
37 class CXeromyces;
38 class CTerrainTextureEntry;
39 class CTerrainProperties;
40 
41 typedef shared_ptr<CTerrainProperties> CTerrainPropertiesPtr;
42 
43 class CTerrainGroup
44 {
45 	// name of this terrain group (as specified by the terrain XML)
46 	CStr m_Name;
47 	// "index".. basically a bogus integer that can be used by ScEd to set texture
48 	// priorities
49 	size_t m_Index;
50 	// list of textures of this type (found from the texture directory)
51 	std::vector<CTerrainTextureEntry*> m_Terrains;
52 
53 public:
CTerrainGroup(CStr name,size_t index)54 	CTerrainGroup(CStr name, size_t index):
55 		m_Name(name),
56 		m_Index(index)
57 	{}
58 
59 	// Add a texture entry to this terrain type
60 	void AddTerrain(CTerrainTextureEntry*);
61 	// Remove a texture entry
62 	void RemoveTerrain(CTerrainTextureEntry*);
63 
GetIndex()64 	size_t GetIndex() const
65 	{ return m_Index; }
GetName()66 	CStr GetName() const
67 	{ return m_Name; }
68 
GetTerrains()69 	const std::vector<CTerrainTextureEntry*> &GetTerrains() const
70 	{ return m_Terrains; }
71 };
72 
73 
74 struct TerrainAlpha
75 {
76 	// ogl_tex handle of composite alpha map (all the alpha maps packed into one texture)
77 	Handle m_hCompositeAlphaMap;
78 	// coordinates of each (untransformed) alpha map within the packed texture
79 	struct {
80 		float u0,u1,v0,v1;
81 	} m_AlphaMapCoords[NUM_ALPHA_MAPS];
82 };
83 
84 
85 ///////////////////////////////////////////////////////////////////////////////////////////
86 // CTerrainTextureManager : manager class for all terrain texture objects
87 class CTerrainTextureManager : public Singleton<CTerrainTextureManager>
88 {
89 	friend class CTerrainTextureEntry;
90 
91 public:
92 	typedef std::map<CStr, CTerrainGroup*> TerrainGroupMap;
93 	typedef std::map<VfsPath, TerrainAlpha> TerrainAlphaMap;
94 
95 private:
96 	// All texture entries created by this class, for easy freeing now that
97 	// textures may be in several STextureType's
98 	std::vector<CTerrainTextureEntry*> m_TextureEntries;
99 
100 	TerrainGroupMap m_TerrainGroups;
101 
102 	TerrainAlphaMap m_TerrainAlphas;
103 
104 	size_t m_LastGroupIndex;
105 
106 public:
107 	// constructor, destructor
108 	CTerrainTextureManager();
109 	~CTerrainTextureManager();
110 
111 	// Find all XML's in the directory (with subdirs) and try to load them as
112 	// terrain XML's
113 	int LoadTerrainTextures();
114 
115 	void UnloadTerrainTextures();
116 
117 	CTerrainTextureEntry* FindTexture(const CStr& tag) const;
118 
119 	// Create a texture object for a new terrain texture at path, using the
120 	// property sheet props.
121 	CTerrainTextureEntry* AddTexture(const CTerrainPropertiesPtr& props, const VfsPath& path);
122 
123 	// Remove the texture from all our maps and lists and delete it afterwards.
124 	void DeleteTexture(CTerrainTextureEntry* entry);
125 
126 	// Find or create a new texture group. All terrain groups are owned by the
127 	// texturemanager (TerrainTypeManager)
128 	CTerrainGroup* FindGroup(const CStr& name);
129 
GetGroups()130 	const TerrainGroupMap& GetGroups() const
131 	{ return m_TerrainGroups; }
132 };
133 
134 
135 #endif
136