Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
TerrainTextureManager.h
Go to the documentation of this file.
1/* Copyright (C) 2023 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
22#include "ps/CStr.h"
23#include "ps/Singleton.h"
26
27#include <map>
28#include <memory>
29#include <vector>
30
31// access to sole CTerrainTextureManager object
32#define g_TexMan CTerrainTextureManager::GetSingleton()
33
34#define NUM_ALPHA_MAPS 14
35
38
39typedef std::shared_ptr<CTerrainProperties> CTerrainPropertiesPtr;
40
42{
43 // name of this terrain group (as specified by the terrain XML)
44 CStr m_Name;
45 // "index".. basically a bogus integer that can be used by ScEd to set texture
46 // priorities
47 size_t m_Index;
48 // list of textures of this type (found from the texture directory)
49 std::vector<CTerrainTextureEntry*> m_Terrains;
50
51public:
52 CTerrainGroup(CStr name, size_t index):
53 m_Name(name),
54 m_Index(index)
55 {}
56
57 // Add a texture entry to this terrain type
59 // Remove a texture entry
61
62 size_t GetIndex() const
63 { return m_Index; }
64 CStr GetName() const
65 { return m_Name; }
66
67 const std::vector<CTerrainTextureEntry*> &GetTerrains() const
68 { return m_Terrains; }
69};
70
71
73{
74 // Composite alpha map (all the alpha maps packed into one texture).
75 std::unique_ptr<Renderer::Backend::ITexture> m_CompositeAlphaMap;
76 // Data is used to separate file loading and uploading to GPU.
77 std::shared_ptr<u8> m_CompositeDataToUpload;
78 // Coordinates of each (untransformed) alpha map within the packed texture.
79 struct
80 {
81 float u0, u1, v0, v1;
83};
84
85
86///////////////////////////////////////////////////////////////////////////////////////////
87// CTerrainTextureManager : manager class for all terrain texture objects
88class CTerrainTextureManager : public Singleton<CTerrainTextureManager>
89{
91
92public:
93 using TerrainGroupMap = std::map<CStr, CTerrainGroup*>;
94 using TerrainAlphaMap = std::map<VfsPath, TerrainAlpha>;
95
96 // constructor, destructor
99
100 // Find all XML's in the directory (with subdirs) and try to load them as
101 // terrain XML's
103
105
106 CTerrainTextureEntry* FindTexture(const CStr& tag) const;
107
108 // Create a texture object for a new terrain texture at path, using the
109 // property sheet props.
111
112 // Remove the texture from all our maps and lists and delete it afterwards.
114
115 // Find or create a new texture group. All terrain groups are owned by the
116 // texturemanager (TerrainTypeManager)
117 CTerrainGroup* FindGroup(const CStr& name);
118
120 { return m_TerrainGroups; }
121
122 CTerrainTextureManager::TerrainAlphaMap::iterator LoadAlphaMap(const VfsPath& alphaMapType);
123
125
126private:
128
129 // All texture entries created by this class, for easy freeing now that
130 // textures may be in several STextureType's
131 std::vector<CTerrainTextureEntry*> m_TextureEntries;
132
134
136
138
139 // A way to separate file loading and uploading to GPU to not stall uploading.
140 // Once we get a properly threaded loading we might optimize that.
141 std::vector<CTerrainTextureManager::TerrainAlphaMap::iterator> m_AlphaMapsToUpload;
142};
143
144#endif // INCLUDED_TERRAINTEXTUREMANAGER
std::shared_ptr< CTerrainProperties > CTerrainPropertiesPtr
Definition: TerrainProperties.h:40
std::shared_ptr< CTerrainProperties > CTerrainPropertiesPtr
Definition: TerrainTextureManager.h:39
#define NUM_ALPHA_MAPS
Definition: TerrainTextureManager.h:34
Definition: TerrainTextureManager.h:42
void RemoveTerrain(CTerrainTextureEntry *)
Definition: TerrainTextureManager.cpp:332
size_t m_Index
Definition: TerrainTextureManager.h:47
void AddTerrain(CTerrainTextureEntry *)
Definition: TerrainTextureManager.cpp:327
std::vector< CTerrainTextureEntry * > m_Terrains
Definition: TerrainTextureManager.h:49
const std::vector< CTerrainTextureEntry * > & GetTerrains() const
Definition: TerrainTextureManager.h:67
CStr m_Name
Definition: TerrainTextureManager.h:44
CTerrainGroup(CStr name, size_t index)
Definition: TerrainTextureManager.h:52
size_t GetIndex() const
Definition: TerrainTextureManager.h:62
CStr GetName() const
Definition: TerrainTextureManager.h:64
Definition: TerrainProperties.h:43
Definition: TerrainTextureEntry.h:33
Definition: TerrainTextureManager.h:89
void DeleteTexture(CTerrainTextureEntry *entry)
Definition: TerrainTextureManager.cpp:89
TerrainAlphaMap m_TerrainAlphas
Definition: TerrainTextureManager.h:135
int LoadTerrainTextures()
Definition: TerrainTextureManager.cpp:125
std::vector< CTerrainTextureEntry * > m_TextureEntries
Definition: TerrainTextureManager.h:131
std::map< CStr, CTerrainGroup * > TerrainGroupMap
Definition: TerrainTextureManager.h:93
std::map< VfsPath, TerrainAlpha > TerrainAlphaMap
Definition: TerrainTextureManager.h:94
size_t m_LastGroupIndex
Definition: TerrainTextureManager.h:137
CTerrainTextureEntry * AddTexture(const CTerrainPropertiesPtr &props, const VfsPath &path)
Definition: TerrainTextureManager.cpp:82
const TerrainGroupMap & GetGroups() const
Definition: TerrainTextureManager.h:119
TerrainGroupMap m_TerrainGroups
Definition: TerrainTextureManager.h:133
Renderer::Backend::IDevice * m_Device
Definition: TerrainTextureManager.h:127
void UnloadTerrainTextures()
Definition: TerrainTextureManager.cpp:57
CTerrainGroup * FindGroup(const CStr &name)
Definition: TerrainTextureManager.cpp:132
void UploadResourcesIfNeeded(Renderer::Backend::IDeviceCommandContext *deviceCommandContext)
Definition: TerrainTextureManager.cpp:308
std::vector< CTerrainTextureManager::TerrainAlphaMap::iterator > m_AlphaMapsToUpload
Definition: TerrainTextureManager.h:141
CTerrainTextureManager(Renderer::Backend::IDevice *device)
Definition: TerrainTextureManager.cpp:38
~CTerrainTextureManager()
Definition: TerrainTextureManager.cpp:49
CTerrainTextureEntry * FindTexture(const CStr &tag) const
Definition: TerrainTextureManager.cpp:70
CTerrainTextureManager::TerrainAlphaMap::iterator LoadAlphaMap(const VfsPath &alphaMapType)
Definition: TerrainTextureManager.cpp:144
Definition: path.h:80
Definition: IDeviceCommandContext.h:42
Definition: IDevice.h:48
Template base class for singletons.
Definition: Singleton.h:34
Definition: TerrainTextureManager.h:73
float v0
Definition: TerrainTextureManager.h:81
std::shared_ptr< u8 > m_CompositeDataToUpload
Definition: TerrainTextureManager.h:77
struct TerrainAlpha::@11 m_AlphaMapCoords[NUM_ALPHA_MAPS]
float u1
Definition: TerrainTextureManager.h:81
float u0
Definition: TerrainTextureManager.h:81
float v1
Definition: TerrainTextureManager.h:81
std::unique_ptr< Renderer::Backend::ITexture > m_CompositeAlphaMap
Definition: TerrainTextureManager.h:75