Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TerrainTextureManager.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2010 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 #include "precompiled.h"
19 
20 #include <algorithm>
21 #include <vector>
22 
23 #include "TerrainTextureManager.h"
24 #include "TerrainTextureEntry.h"
25 #include "TerrainProperties.h"
26 
28 #include "lib/ogl.h"
29 #include "lib/timer.h"
30 
31 #include "ps/CLogger.h"
32 #include "ps/Filesystem.h"
33 
34 // Disable "'boost::algorithm::detail::is_classifiedF' : assignment operator could not be generated"
35 // and "find_format_store.hpp(74) : warning C4100: 'Input' : unreferenced formal parameter"
36 #if MSC_VERSION
37 #pragma warning(disable:4512)
38 #pragma warning(disable:4100)
39 #endif
40 
41 #include <boost/algorithm/string.hpp>
42 
43 
45  m_LastGroupIndex(0)
46 {}
47 
49 {
51 
52  TerrainAlphaMap::iterator it;
53  for (it = m_TerrainAlphas.begin(); it != m_TerrainAlphas.end(); ++it)
54  {
55  ogl_tex_free(it->second.m_hCompositeAlphaMap);
56  it->second.m_hCompositeAlphaMap = 0;
57  }
58 }
59 
61 {
62  for (size_t i=0; i < m_TextureEntries.size(); i++)
63  delete m_TextureEntries[i];
64  m_TextureEntries.clear();
65 
66  TerrainGroupMap::iterator it = m_TerrainGroups.begin();
67  while (it != m_TerrainGroups.end())
68  {
69  delete it->second;
70  ++it;
71  }
72  m_TerrainGroups.clear();
73 
74  m_LastGroupIndex = 0;
75 }
76 
78 {
79  CStr tag(tag_);
80  // Strip extension off of tag
81  long pos=tag.ReverseFind(".");
82  if (pos != -1)
83  {
84  tag = tag.substr(0, pos);
85  }
86  for (size_t i=0;i<m_TextureEntries.size();i++)
87  {
88  if (m_TextureEntries[i]->GetTag() == tag)
89  return m_TextureEntries[i];
90  }
91 
92  LOGWARNING(L"CTerrainTextureManager: Couldn't find terrain %hs", tag.c_str());
93  return 0;
94 }
95 
97 {
98  return CTerrainProperties::FromXML(props, pathname);
99 }
100 
102 {
103  CTerrainTextureEntry *entry = new CTerrainTextureEntry(props, path);
104  m_TextureEntries.push_back(entry);
105  return entry;
106 }
107 
109 {
110  typedef std::vector<CTerrainTextureEntry*>::iterator Iter;
111  Iter i=std::find(m_TextureEntries.begin(),m_TextureEntries.end(),entry);
112  if (i!=m_TextureEntries.end()) {
113  m_TextureEntries.erase(i);
114  }
115  delete entry;
116 }
117 
118 // FIXME This could be effectivized by surveying the xml files in the directory
119 // instead of trial-and-error checking for existence of the xml file through
120 // the VFS.
121 // jw: indeed this is inefficient and RecurseDirectory should be implemented
122 // via VFSUtil::EnumFiles, but it works fine and "only" takes 25ms for
123 // typical maps. therefore, we'll leave it for now.
125 {
126  VfsPaths pathnames;
127  if(vfs::GetPathnames(g_VFS, path, 0, pathnames) < 0)
128  return;
129 
130  for(size_t i = 0; i < pathnames.size(); i++)
131  {
132  if (pathnames[i].Extension() != L".xml")
133  continue;
134 
135  if (pathnames[i].Basename() == L"terrains")
136  continue;
137 
138  AddTexture(props, pathnames[i]);
139  }
140 }
141 
143 {
144  //LOGMESSAGE(L"CTextureManager::RecurseDirectory(%ls)", path.string().c_str());
145 
146  CTerrainPropertiesPtr props;
147 
148  // Load terrains.xml first, if it exists
149  VfsPath pathname = path / "terrains.xml";
150  if (VfsFileExists(pathname))
151  props = GetPropertiesFromFile(parentProps, pathname);
152 
153  // No terrains.xml, or read failures -> use parent props (i.e.
154  if (!props)
155  {
156  LOGMESSAGE(L"CTerrainTextureManager::RecurseDirectory(%ls): no terrains.xml (or errors while loading) - using parent properties", path.string().c_str());
157  props = parentProps;
158  }
159 
160  // Recurse once for each subdirectory
161  DirectoryNames subdirectoryNames;
162  (void)g_VFS->GetDirectoryEntries(path, 0, &subdirectoryNames);
163  for (size_t i=0;i<subdirectoryNames.size();i++)
164  {
165  VfsPath subdirectoryPath = path / subdirectoryNames[i] / "";
166  RecurseDirectory(props, subdirectoryPath);
167  }
168 
169  LoadTextures(props, path);
170 }
171 
172 
174 {
176  RecurseDirectory(rootProps, L"art/terrains/");
177  return 0;
178 }
179 
181 {
182  TerrainGroupMap::const_iterator it=m_TerrainGroups.find(name);
183  if (it != m_TerrainGroups.end())
184  return it->second;
185  else
186  return m_TerrainGroups[name] = new CTerrainGroup(name, ++m_LastGroupIndex);
187 }
188 
190 {
191  m_Terrains.push_back(pTerrain);
192 }
193 
195 {
196  std::vector<CTerrainTextureEntry *>::iterator it;
197  it=find(m_Terrains.begin(), m_Terrains.end(), pTerrain);
198  if (it != m_Terrains.end())
199  m_Terrains.erase(it);
200 }
shared_ptr< CTerrainProperties > CTerrainPropertiesPtr
std::vector< CTerrainTextureEntry * > m_Terrains
std::vector< CTerrainTextureEntry * > m_TextureEntries
Status ogl_tex_free(Handle &ht)
Release this texture reference.
Definition: ogl_tex.cpp:586
#define LOGMESSAGE
Definition: CLogger.h:32
void RecurseDirectory(const CTerrainPropertiesPtr &props, const VfsPath &path)
CTerrainTextureEntry * FindTexture(const CStr &tag)
#define LOGWARNING
Definition: CLogger.h:34
static CTerrainPropertiesPtr FromXML(const CTerrainPropertiesPtr &parent, const VfsPath &pathname)
CTerrainGroup * FindGroup(const CStr &name)
Definition: path.h:75
const String & string() const
Definition: path.h:123
void LoadTextures(const CTerrainPropertiesPtr &props, const VfsPath &path)
CTerrainPropertiesPtr GetPropertiesFromFile(const CTerrainPropertiesPtr &props, const VfsPath &pathname)
std::vector< OsPath > DirectoryNames
Definition: file_system.h:77
bool VfsFileExists(const VfsPath &pathname)
Definition: Filesystem.cpp:34
void AddTerrain(CTerrainTextureEntry *)
CTerrainTextureEntry * AddTexture(const CTerrainPropertiesPtr &props, const VfsPath &path)
std::vector< VfsPath > VfsPaths
Definition: vfs_path.h:42
void RemoveTerrain(CTerrainTextureEntry *)
void DeleteTexture(CTerrainTextureEntry *entry)
PIVFS g_VFS
Definition: Filesystem.cpp:30
Status GetPathnames(const PIVFS &fs, const VfsPath &path, const wchar_t *filter, VfsPaths &pathnames)
Definition: vfs_util.cpp:40