1 /* Copyright (C) 2023 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
18 #ifndef INCLUDED_TERRAINTEXTUREMANAGER
19 #define INCLUDED_TERRAINTEXTUREMANAGER
21 #include "lib/file/vfs/vfs_path.h"
23 #include "ps/Singleton.h"
24 #include "renderer/backend/IDeviceCommandContext.h"
25 #include "renderer/backend/ITexture.h"
31 // access to sole CTerrainTextureManager object
32 #define g_TexMan CTerrainTextureManager::GetSingleton()
34 #define NUM_ALPHA_MAPS 14
36 class CTerrainTextureEntry
;
37 class CTerrainProperties
;
39 typedef std::shared_ptr
<CTerrainProperties
> CTerrainPropertiesPtr
;
43 // name of this terrain group (as specified by the terrain XML)
45 // "index".. basically a bogus integer that can be used by ScEd to set texture
48 // list of textures of this type (found from the texture directory)
49 std::vector
<CTerrainTextureEntry
*> m_Terrains
;
52 CTerrainGroup(CStr name
, size_t index
):
57 // Add a texture entry to this terrain type
58 void AddTerrain(CTerrainTextureEntry
*);
59 // Remove a texture entry
60 void RemoveTerrain(CTerrainTextureEntry
*);
62 size_t GetIndex() const
67 const std::vector
<CTerrainTextureEntry
*> &GetTerrains() const
68 { return m_Terrains
; }
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.
82 } m_AlphaMapCoords
[NUM_ALPHA_MAPS
];
86 ///////////////////////////////////////////////////////////////////////////////////////////
87 // CTerrainTextureManager : manager class for all terrain texture objects
88 class CTerrainTextureManager
: public Singleton
<CTerrainTextureManager
>
90 friend class CTerrainTextureEntry
;
93 using TerrainGroupMap
= std::map
<CStr
, CTerrainGroup
*>;
94 using TerrainAlphaMap
= std::map
<VfsPath
, TerrainAlpha
>;
96 // constructor, destructor
97 CTerrainTextureManager(Renderer::Backend::IDevice
* device
);
98 ~CTerrainTextureManager();
100 // Find all XML's in the directory (with subdirs) and try to load them as
102 int LoadTerrainTextures();
104 void UnloadTerrainTextures();
106 CTerrainTextureEntry
* FindTexture(const CStr
& tag
) const;
108 // Create a texture object for a new terrain texture at path, using the
109 // property sheet props.
110 CTerrainTextureEntry
* AddTexture(const CTerrainPropertiesPtr
& props
, const VfsPath
& path
);
112 // Remove the texture from all our maps and lists and delete it afterwards.
113 void DeleteTexture(CTerrainTextureEntry
* entry
);
115 // Find or create a new texture group. All terrain groups are owned by the
116 // texturemanager (TerrainTypeManager)
117 CTerrainGroup
* FindGroup(const CStr
& name
);
119 const TerrainGroupMap
& GetGroups() const
120 { return m_TerrainGroups
; }
122 CTerrainTextureManager::TerrainAlphaMap::iterator
LoadAlphaMap(const VfsPath
& alphaMapType
);
124 void UploadResourcesIfNeeded(Renderer::Backend::IDeviceCommandContext
* deviceCommandContext
);
127 Renderer::Backend::IDevice
* m_Device
= nullptr;
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
;
133 TerrainGroupMap m_TerrainGroups
;
135 TerrainAlphaMap m_TerrainAlphas
;
137 size_t m_LastGroupIndex
= 0;
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
;
144 #endif // INCLUDED_TERRAINTEXTUREMANAGER