2004-06-03 11:38:14 -07:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
2004-06-02 18:43:33 -07:00
|
|
|
#include <algorithm>
|
2005-06-20 08:14:36 -07:00
|
|
|
#include <vector>
|
2004-05-29 13:56:24 -07:00
|
|
|
|
|
|
|
|
#include "TextureManager.h"
|
2005-06-20 08:14:36 -07:00
|
|
|
#include "TextureEntry.h"
|
2005-08-07 14:58:36 -07:00
|
|
|
#include "TerrainProperties.h"
|
2005-06-20 08:14:36 -07:00
|
|
|
|
2005-08-12 10:06:53 -07:00
|
|
|
#include "lib/res/graphics/ogl_tex.h"
|
2005-08-09 08:55:44 -07:00
|
|
|
#include "lib/ogl.h"
|
|
|
|
|
#include "lib/timer.h"
|
2004-06-02 18:43:33 -07:00
|
|
|
|
2005-08-09 08:55:44 -07:00
|
|
|
#include "ps/CLogger.h"
|
2007-12-20 12:21:45 -08:00
|
|
|
#include "ps/Filesystem.h"
|
2004-05-29 13:56:24 -07:00
|
|
|
|
2005-06-20 08:14:36 -07:00
|
|
|
#define LOG_CATEGORY "graphics"
|
2004-05-29 13:56:24 -07:00
|
|
|
|
2005-06-20 10:34:17 -07:00
|
|
|
CTextureManager::CTextureManager():
|
|
|
|
|
m_LastGroupIndex(0)
|
|
|
|
|
{}
|
2004-05-29 17:46:58 -07:00
|
|
|
|
|
|
|
|
CTextureManager::~CTextureManager()
|
|
|
|
|
{
|
2005-11-05 21:05:07 -08:00
|
|
|
UnloadTerrainTextures();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CTextureManager::UnloadTerrainTextures()
|
|
|
|
|
{
|
|
|
|
|
for (size_t i=0; i < m_TextureEntries.size(); i++)
|
2005-06-20 08:14:36 -07:00
|
|
|
delete m_TextureEntries[i];
|
2005-11-05 21:05:07 -08:00
|
|
|
m_TextureEntries.clear();
|
|
|
|
|
|
|
|
|
|
TerrainGroupMap::iterator it = m_TerrainGroups.begin();
|
2005-08-07 14:58:36 -07:00
|
|
|
while (it != m_TerrainGroups.end())
|
2005-06-20 08:14:36 -07:00
|
|
|
{
|
|
|
|
|
delete it->second;
|
|
|
|
|
++it;
|
2004-05-29 17:46:58 -07:00
|
|
|
}
|
2005-11-05 21:05:07 -08:00
|
|
|
m_TerrainGroups.clear();
|
|
|
|
|
|
|
|
|
|
m_LastGroupIndex = 0;
|
2004-05-29 17:46:58 -07:00
|
|
|
}
|
2004-05-29 13:56:24 -07:00
|
|
|
|
2005-06-20 08:14:36 -07:00
|
|
|
CTextureEntry* CTextureManager::FindTexture(CStr tag)
|
2004-05-29 13:56:24 -07:00
|
|
|
{
|
2005-08-07 14:58:36 -07:00
|
|
|
// Strip extension off of tag
|
|
|
|
|
long pos=tag.ReverseFind(".");
|
|
|
|
|
if (pos != -1)
|
|
|
|
|
{
|
2007-02-01 06:46:14 -08:00
|
|
|
tag = tag.substr(0, pos);
|
2005-08-07 14:58:36 -07:00
|
|
|
}
|
2005-06-20 08:14:36 -07:00
|
|
|
for (uint i=0;i<m_TextureEntries.size();i++)
|
|
|
|
|
{
|
|
|
|
|
if (m_TextureEntries[i]->GetTag() == tag)
|
|
|
|
|
return m_TextureEntries[i];
|
|
|
|
|
}
|
|
|
|
|
|
2007-12-29 08:22:23 -08:00
|
|
|
LOG(CLogger::Warning, LOG_CATEGORY, "TextureManager: Couldn't find terrain %s", tag.c_str());
|
2005-06-20 08:14:36 -07:00
|
|
|
return 0;
|
2004-05-29 13:56:24 -07:00
|
|
|
}
|
|
|
|
|
|
2005-06-20 08:14:36 -07:00
|
|
|
CTextureEntry* CTextureManager::FindTexture(Handle handle)
|
2004-05-29 13:56:24 -07:00
|
|
|
{
|
2005-08-07 14:58:36 -07:00
|
|
|
return CTextureEntry::GetByHandle(handle);
|
2004-05-29 13:56:24 -07:00
|
|
|
}
|
|
|
|
|
|
2005-10-27 18:43:16 -07:00
|
|
|
CTerrainPropertiesPtr CTextureManager::GetPropertiesFromFile(CTerrainPropertiesPtr props, const char* path)
|
2004-05-29 13:56:24 -07:00
|
|
|
{
|
2005-08-07 14:58:36 -07:00
|
|
|
return CTerrainProperties::FromXML(props, path);
|
2004-05-29 13:56:24 -07:00
|
|
|
}
|
|
|
|
|
|
2006-07-20 07:37:58 -07:00
|
|
|
CTextureEntry *CTextureManager::AddTexture(CTerrainPropertiesPtr props, const CStr& path)
|
2004-05-29 13:56:24 -07:00
|
|
|
{
|
2005-08-07 14:58:36 -07:00
|
|
|
CTextureEntry *entry = new CTextureEntry(props, path);
|
|
|
|
|
m_TextureEntries.push_back(entry);
|
|
|
|
|
return entry;
|
|
|
|
|
}
|
2004-05-29 13:56:24 -07:00
|
|
|
|
|
|
|
|
void CTextureManager::DeleteTexture(CTextureEntry* entry)
|
|
|
|
|
{
|
|
|
|
|
typedef std::vector<CTextureEntry*>::iterator Iter;
|
2005-06-20 08:14:36 -07:00
|
|
|
Iter i=std::find(m_TextureEntries.begin(),m_TextureEntries.end(),entry);
|
|
|
|
|
if (i!=m_TextureEntries.end()) {
|
|
|
|
|
m_TextureEntries.erase(i);
|
2004-05-29 13:56:24 -07:00
|
|
|
}
|
|
|
|
|
delete entry;
|
|
|
|
|
}
|
|
|
|
|
|
2005-08-07 14:58:36 -07:00
|
|
|
// FIXME This could be effectivized by surveying the xml files in the directory
|
|
|
|
|
// instead of trial-and-error checking for existence of the xml file through
|
|
|
|
|
// the VFS.
|
2005-08-09 18:12:03 -07:00
|
|
|
// jw: indeed this is inefficient and RecurseDirectory should be implemented
|
|
|
|
|
// via VFSUtil::EnumFiles, but it works fine and "only" takes 25ms for
|
|
|
|
|
// typical maps. therefore, we'll leave it for now.
|
2005-10-27 18:43:16 -07:00
|
|
|
void CTextureManager::LoadTextures(CTerrainPropertiesPtr props, const char* dir)
|
2005-08-07 14:58:36 -07:00
|
|
|
{
|
2007-12-20 12:21:45 -08:00
|
|
|
VfsPaths pathnames;
|
|
|
|
|
if(fs_GetPathnames(g_VFS, dir, 0, pathnames) < 0)
|
2005-10-19 15:39:54 -07:00
|
|
|
return;
|
2007-12-20 12:21:45 -08:00
|
|
|
for(size_t i = 0; i < pathnames.size(); i++)
|
2005-10-19 15:39:54 -07:00
|
|
|
{
|
2007-12-20 12:21:45 -08:00
|
|
|
const char* texture_name = pathnames[i].string().c_str();
|
2005-10-19 15:39:54 -07:00
|
|
|
|
2005-10-23 16:57:59 -07:00
|
|
|
// skip files that obviously aren't textures.
|
|
|
|
|
// note: this loop runs for each file in dir, even .xml;
|
|
|
|
|
// we should skip those to avoid spurious "texture load failed".
|
|
|
|
|
// we can't use FindFile's filter param because new texture formats
|
|
|
|
|
// may later be added and that interface doesn't support specifying
|
|
|
|
|
// multiple extensions.
|
2006-04-22 09:26:16 -07:00
|
|
|
if(!tex_is_known_extension(texture_name))
|
2005-10-27 18:43:16 -07:00
|
|
|
continue;
|
2005-10-23 16:57:59 -07:00
|
|
|
|
2005-10-19 15:39:54 -07:00
|
|
|
// build name of associated xml file (i.e. replace extension)
|
|
|
|
|
char xml_name[PATH_MAX+5]; // add room for .XML
|
|
|
|
|
strcpy_s(xml_name, PATH_MAX, texture_name);
|
2006-04-22 09:26:16 -07:00
|
|
|
const char* ext = path_extension(texture_name);
|
|
|
|
|
SAFE_STRCPY(xml_name + (ext-texture_name), "xml");
|
2005-10-19 15:39:54 -07:00
|
|
|
|
2005-10-27 18:43:16 -07:00
|
|
|
CTerrainPropertiesPtr myprops;
|
2005-10-19 15:39:54 -07:00
|
|
|
// Has XML file -> attempt to load properties
|
2007-12-20 12:21:45 -08:00
|
|
|
if (FileExists(xml_name))
|
2005-10-19 15:39:54 -07:00
|
|
|
{
|
2005-10-27 18:43:16 -07:00
|
|
|
myprops = GetPropertiesFromFile(props, xml_name);
|
2005-08-07 14:58:36 -07:00
|
|
|
if (myprops)
|
2007-12-29 08:22:23 -08:00
|
|
|
LOG(CLogger::Normal, LOG_CATEGORY, "CTextureManager: Successfully loaded override xml %s for texture %s", xml_name, texture_name);
|
2005-10-19 15:39:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Error or non-existant xml file -> use parent props
|
|
|
|
|
if (!myprops)
|
|
|
|
|
myprops = props;
|
|
|
|
|
|
|
|
|
|
AddTexture(myprops, texture_name);
|
|
|
|
|
}
|
2005-08-07 14:58:36 -07:00
|
|
|
}
|
|
|
|
|
|
2005-10-27 18:43:16 -07:00
|
|
|
void CTextureManager::RecurseDirectory(CTerrainPropertiesPtr parentProps, const char* cur_dir_path)
|
2004-05-29 13:56:24 -07:00
|
|
|
{
|
2007-12-29 08:22:23 -08:00
|
|
|
//LOG(CLogger::Normal, LOG_CATEGORY, "CTextureManager::RecurseDirectory(%s)", path.c_str());
|
2005-10-19 15:39:54 -07:00
|
|
|
|
2005-10-27 18:43:16 -07:00
|
|
|
CTerrainPropertiesPtr props;
|
2005-10-19 15:39:54 -07:00
|
|
|
|
|
|
|
|
// Load terrains.xml first, if it exists
|
|
|
|
|
char fn[PATH_MAX];
|
2006-05-21 16:22:55 -07:00
|
|
|
snprintf(fn, PATH_MAX, "%s%s", cur_dir_path, "terrains.xml");
|
2006-09-28 13:41:12 -07:00
|
|
|
fn[PATH_MAX-1] = '\0';
|
2007-12-20 12:21:45 -08:00
|
|
|
if (FileExists(fn))
|
2005-10-27 18:43:16 -07:00
|
|
|
props = GetPropertiesFromFile(parentProps, fn);
|
2005-08-07 14:58:36 -07:00
|
|
|
|
2005-08-09 18:27:33 -07:00
|
|
|
// No terrains.xml, or read failures -> use parent props (i.e.
|
|
|
|
|
if (!props)
|
2005-08-07 14:58:36 -07:00
|
|
|
{
|
2007-12-29 08:22:23 -08:00
|
|
|
LOG(CLogger::Normal, LOG_CATEGORY,
|
2005-10-19 15:39:54 -07:00
|
|
|
"CTextureManager::RecurseDirectory(%s): no terrains.xml (or errors while loading) - using parent properties", cur_dir_path);
|
2005-08-07 14:58:36 -07:00
|
|
|
props = parentProps;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Recurse once for each subdirectory
|
2007-12-20 12:21:45 -08:00
|
|
|
DirectoryNames subdirectoryNames;
|
|
|
|
|
g_VFS->GetDirectoryEntries(cur_dir_path, 0, &subdirectoryNames);
|
|
|
|
|
for (uint i=0;i<subdirectoryNames.size();i++)
|
2004-06-02 07:27:54 -07:00
|
|
|
{
|
2007-12-20 12:21:45 -08:00
|
|
|
char subdirectoryPath[PATH_MAX];
|
|
|
|
|
path_append(subdirectoryPath, cur_dir_path, subdirectoryNames[i].c_str(), PATH_APPEND_SLASH);
|
|
|
|
|
RecurseDirectory(props, subdirectoryPath);
|
2004-05-29 13:56:24 -07:00
|
|
|
}
|
|
|
|
|
|
2005-10-23 16:57:59 -07:00
|
|
|
LoadTextures(props, cur_dir_path);
|
2004-05-29 13:56:24 -07:00
|
|
|
}
|
|
|
|
|
|
2005-08-09 08:55:44 -07:00
|
|
|
|
2005-05-03 14:36:57 -07:00
|
|
|
int CTextureManager::LoadTerrainTextures()
|
2004-05-29 13:56:24 -07:00
|
|
|
{
|
2006-05-17 07:48:18 -07:00
|
|
|
RecurseDirectory(CTerrainPropertiesPtr(), "art/textures/terrain/types/");
|
2005-06-20 08:14:36 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
2004-05-29 13:56:24 -07:00
|
|
|
|
2006-07-20 07:37:58 -07:00
|
|
|
CTerrainGroup *CTextureManager::FindGroup(const CStr& name)
|
2005-06-20 08:14:36 -07:00
|
|
|
{
|
2005-08-07 14:58:36 -07:00
|
|
|
TerrainGroupMap::const_iterator it=m_TerrainGroups.find(name);
|
|
|
|
|
if (it != m_TerrainGroups.end())
|
2005-06-20 08:14:36 -07:00
|
|
|
return it->second;
|
|
|
|
|
else
|
2005-08-07 14:58:36 -07:00
|
|
|
return m_TerrainGroups[name] = new CTerrainGroup(name, ++m_LastGroupIndex);
|
2005-06-20 10:34:17 -07:00
|
|
|
}
|
|
|
|
|
|
2005-08-07 14:58:36 -07:00
|
|
|
void CTerrainGroup::AddTerrain(CTextureEntry *pTerrain)
|
2005-06-20 08:14:36 -07:00
|
|
|
{
|
|
|
|
|
m_Terrains.push_back(pTerrain);
|
|
|
|
|
}
|
|
|
|
|
|
2005-08-07 14:58:36 -07:00
|
|
|
void CTerrainGroup::RemoveTerrain(CTextureEntry *pTerrain)
|
2005-06-20 08:14:36 -07:00
|
|
|
{
|
2006-09-25 18:44:20 -07:00
|
|
|
std::vector<CTextureEntry *>::iterator it;
|
2005-06-20 08:14:36 -07:00
|
|
|
it=find(m_Terrains.begin(), m_Terrains.end(), pTerrain);
|
|
|
|
|
if (it != m_Terrains.end())
|
|
|
|
|
m_Terrains.erase(it);
|
2004-05-29 13:56:24 -07:00
|
|
|
}
|