1
0
forked from 0ad/0ad
0ad/source/graphics/TextureManager.cpp
janwas c0ed950657 had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).

it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.

after several hours, the code now requires fewer casts and less
guesswork.

other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.

This was SVN commit r5942.
2008-05-11 18:48:32 +00:00

199 lines
5.4 KiB
C++

#include "precompiled.h"
#include <algorithm>
#include <vector>
#include "TextureManager.h"
#include "TextureEntry.h"
#include "TerrainProperties.h"
#include "lib/res/graphics/ogl_tex.h"
#include "lib/ogl.h"
#include "lib/timer.h"
#include "ps/CLogger.h"
#include "ps/Filesystem.h"
#define LOG_CATEGORY "graphics"
CTextureManager::CTextureManager():
m_LastGroupIndex(0)
{}
CTextureManager::~CTextureManager()
{
UnloadTerrainTextures();
}
void CTextureManager::UnloadTerrainTextures()
{
for (size_t i=0; i < m_TextureEntries.size(); i++)
delete m_TextureEntries[i];
m_TextureEntries.clear();
TerrainGroupMap::iterator it = m_TerrainGroups.begin();
while (it != m_TerrainGroups.end())
{
delete it->second;
++it;
}
m_TerrainGroups.clear();
m_LastGroupIndex = 0;
}
CTextureEntry* CTextureManager::FindTexture(CStr tag)
{
// Strip extension off of tag
long pos=tag.ReverseFind(".");
if (pos != -1)
{
tag = tag.substr(0, pos);
}
for (size_t i=0;i<m_TextureEntries.size();i++)
{
if (m_TextureEntries[i]->GetTag() == tag)
return m_TextureEntries[i];
}
LOG(CLogger::Warning, LOG_CATEGORY, "TextureManager: Couldn't find terrain %s", tag.c_str());
return 0;
}
CTextureEntry* CTextureManager::FindTexture(Handle handle)
{
return CTextureEntry::GetByHandle(handle);
}
CTerrainPropertiesPtr CTextureManager::GetPropertiesFromFile(CTerrainPropertiesPtr props, const char* path)
{
return CTerrainProperties::FromXML(props, path);
}
CTextureEntry *CTextureManager::AddTexture(CTerrainPropertiesPtr props, const CStr& path)
{
CTextureEntry *entry = new CTextureEntry(props, path);
m_TextureEntries.push_back(entry);
return entry;
}
void CTextureManager::DeleteTexture(CTextureEntry* entry)
{
typedef std::vector<CTextureEntry*>::iterator Iter;
Iter i=std::find(m_TextureEntries.begin(),m_TextureEntries.end(),entry);
if (i!=m_TextureEntries.end()) {
m_TextureEntries.erase(i);
}
delete entry;
}
// 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.
// 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.
void CTextureManager::LoadTextures(CTerrainPropertiesPtr props, const char* dir)
{
VfsPaths pathnames;
if(fs_GetPathnames(g_VFS, dir, 0, pathnames) < 0)
return;
for(size_t i = 0; i < pathnames.size(); i++)
{
const char* texture_name = pathnames[i].string().c_str();
// 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.
if(!tex_is_known_extension(texture_name))
continue;
// 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);
const char* ext = path_extension(texture_name);
SAFE_STRCPY(xml_name + (ext-texture_name), "xml");
CTerrainPropertiesPtr myprops;
// Has XML file -> attempt to load properties
if (FileExists(xml_name))
{
myprops = GetPropertiesFromFile(props, xml_name);
if (myprops)
LOG(CLogger::Normal, LOG_CATEGORY, "CTextureManager: Successfully loaded override xml %s for texture %s", xml_name, texture_name);
}
// Error or non-existant xml file -> use parent props
if (!myprops)
myprops = props;
AddTexture(myprops, texture_name);
}
}
void CTextureManager::RecurseDirectory(CTerrainPropertiesPtr parentProps, const char* cur_dir_path)
{
//LOG(CLogger::Normal, LOG_CATEGORY, "CTextureManager::RecurseDirectory(%s)", path.c_str());
CTerrainPropertiesPtr props;
// Load terrains.xml first, if it exists
char fn[PATH_MAX];
snprintf(fn, PATH_MAX, "%s%s", cur_dir_path, "terrains.xml");
fn[PATH_MAX-1] = '\0';
if (FileExists(fn))
props = GetPropertiesFromFile(parentProps, fn);
// No terrains.xml, or read failures -> use parent props (i.e.
if (!props)
{
LOG(CLogger::Normal, LOG_CATEGORY,
"CTextureManager::RecurseDirectory(%s): no terrains.xml (or errors while loading) - using parent properties", cur_dir_path);
props = parentProps;
}
// Recurse once for each subdirectory
DirectoryNames subdirectoryNames;
g_VFS->GetDirectoryEntries(cur_dir_path, 0, &subdirectoryNames);
for (size_t i=0;i<subdirectoryNames.size();i++)
{
char subdirectoryPath[PATH_MAX];
path_append(subdirectoryPath, cur_dir_path, subdirectoryNames[i].c_str(), PATH_APPEND_SLASH);
RecurseDirectory(props, subdirectoryPath);
}
LoadTextures(props, cur_dir_path);
}
int CTextureManager::LoadTerrainTextures()
{
RecurseDirectory(CTerrainPropertiesPtr(), "art/textures/terrain/types/");
return 0;
}
CTerrainGroup *CTextureManager::FindGroup(const CStr& name)
{
TerrainGroupMap::const_iterator it=m_TerrainGroups.find(name);
if (it != m_TerrainGroups.end())
return it->second;
else
return m_TerrainGroups[name] = new CTerrainGroup(name, ++m_LastGroupIndex);
}
void CTerrainGroup::AddTerrain(CTextureEntry *pTerrain)
{
m_Terrains.push_back(pTerrain);
}
void CTerrainGroup::RemoveTerrain(CTextureEntry *pTerrain)
{
std::vector<CTextureEntry *>::iterator it;
it=find(m_Terrains.begin(), m_Terrains.end(), pTerrain);
if (it != m_Terrains.end())
m_Terrains.erase(it);
}