2009-04-18 19:00:33 +02:00
|
|
|
/* Copyright (C) 2009 Wildfire Games.
|
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2004-06-03 20:38:14 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2004-06-03 03:43:33 +02:00
|
|
|
#include <algorithm>
|
2005-06-20 17:14:36 +02:00
|
|
|
#include <vector>
|
2004-05-29 22:56:24 +02:00
|
|
|
|
|
|
|
#include "TextureManager.h"
|
2005-06-20 17:14:36 +02:00
|
|
|
#include "TextureEntry.h"
|
2005-08-07 23:58:36 +02:00
|
|
|
#include "TerrainProperties.h"
|
2005-06-20 17:14:36 +02:00
|
|
|
|
2005-08-12 19:06:53 +02:00
|
|
|
#include "lib/res/graphics/ogl_tex.h"
|
2005-08-09 17:55:44 +02:00
|
|
|
#include "lib/ogl.h"
|
|
|
|
#include "lib/timer.h"
|
2004-06-03 03:43:33 +02:00
|
|
|
|
2005-08-09 17:55:44 +02:00
|
|
|
#include "ps/CLogger.h"
|
2007-12-20 21:21:45 +01:00
|
|
|
#include "ps/Filesystem.h"
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2005-06-20 17:14:36 +02:00
|
|
|
#define LOG_CATEGORY "graphics"
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2005-06-20 19:34:17 +02:00
|
|
|
CTextureManager::CTextureManager():
|
|
|
|
m_LastGroupIndex(0)
|
|
|
|
{}
|
2004-05-30 02:46:58 +02:00
|
|
|
|
|
|
|
CTextureManager::~CTextureManager()
|
|
|
|
{
|
2005-11-06 06:05:07 +01:00
|
|
|
UnloadTerrainTextures();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTextureManager::UnloadTerrainTextures()
|
|
|
|
{
|
|
|
|
for (size_t i=0; i < m_TextureEntries.size(); i++)
|
2005-06-20 17:14:36 +02:00
|
|
|
delete m_TextureEntries[i];
|
2005-11-06 06:05:07 +01:00
|
|
|
m_TextureEntries.clear();
|
|
|
|
|
|
|
|
TerrainGroupMap::iterator it = m_TerrainGroups.begin();
|
2005-08-07 23:58:36 +02:00
|
|
|
while (it != m_TerrainGroups.end())
|
2005-06-20 17:14:36 +02:00
|
|
|
{
|
|
|
|
delete it->second;
|
|
|
|
++it;
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
2005-11-06 06:05:07 +01:00
|
|
|
m_TerrainGroups.clear();
|
|
|
|
|
|
|
|
m_LastGroupIndex = 0;
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2008-07-12 12:45:11 +02:00
|
|
|
CTextureEntry* CTextureManager::FindTexture(const CStr& tag_)
|
2004-05-29 22:56:24 +02:00
|
|
|
{
|
2008-07-12 12:45:11 +02:00
|
|
|
CStr tag(tag_);
|
2005-08-07 23:58:36 +02:00
|
|
|
// Strip extension off of tag
|
|
|
|
long pos=tag.ReverseFind(".");
|
|
|
|
if (pos != -1)
|
|
|
|
{
|
2007-02-01 15:46:14 +01:00
|
|
|
tag = tag.substr(0, pos);
|
2005-08-07 23:58:36 +02:00
|
|
|
}
|
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 20:48:32 +02:00
|
|
|
for (size_t i=0;i<m_TextureEntries.size();i++)
|
2005-06-20 17:14:36 +02:00
|
|
|
{
|
|
|
|
if (m_TextureEntries[i]->GetTag() == tag)
|
|
|
|
return m_TextureEntries[i];
|
|
|
|
}
|
|
|
|
|
2007-12-29 17:22:23 +01:00
|
|
|
LOG(CLogger::Warning, LOG_CATEGORY, "TextureManager: Couldn't find terrain %s", tag.c_str());
|
2005-06-20 17:14:36 +02:00
|
|
|
return 0;
|
2004-05-29 22:56:24 +02:00
|
|
|
}
|
|
|
|
|
2005-06-20 17:14:36 +02:00
|
|
|
CTextureEntry* CTextureManager::FindTexture(Handle handle)
|
2004-05-29 22:56:24 +02:00
|
|
|
{
|
2005-08-07 23:58:36 +02:00
|
|
|
return CTextureEntry::GetByHandle(handle);
|
2004-05-29 22:56:24 +02:00
|
|
|
}
|
|
|
|
|
2008-07-12 12:45:11 +02:00
|
|
|
CTerrainPropertiesPtr CTextureManager::GetPropertiesFromFile(const CTerrainPropertiesPtr& props, const char* path)
|
2004-05-29 22:56:24 +02:00
|
|
|
{
|
2005-08-07 23:58:36 +02:00
|
|
|
return CTerrainProperties::FromXML(props, path);
|
2004-05-29 22:56:24 +02:00
|
|
|
}
|
|
|
|
|
2008-07-12 12:45:11 +02:00
|
|
|
CTextureEntry *CTextureManager::AddTexture(const CTerrainPropertiesPtr& props, const CStr& path)
|
2004-05-29 22:56:24 +02:00
|
|
|
{
|
2005-08-07 23:58:36 +02:00
|
|
|
CTextureEntry *entry = new CTextureEntry(props, path);
|
|
|
|
m_TextureEntries.push_back(entry);
|
|
|
|
return entry;
|
|
|
|
}
|
2004-05-29 22:56:24 +02:00
|
|
|
|
|
|
|
void CTextureManager::DeleteTexture(CTextureEntry* entry)
|
|
|
|
{
|
|
|
|
typedef std::vector<CTextureEntry*>::iterator Iter;
|
2005-06-20 17:14:36 +02:00
|
|
|
Iter i=std::find(m_TextureEntries.begin(),m_TextureEntries.end(),entry);
|
|
|
|
if (i!=m_TextureEntries.end()) {
|
|
|
|
m_TextureEntries.erase(i);
|
2004-05-29 22:56:24 +02:00
|
|
|
}
|
|
|
|
delete entry;
|
|
|
|
}
|
|
|
|
|
2005-08-07 23:58:36 +02: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-10 03:12:03 +02: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.
|
2008-07-12 12:45:11 +02:00
|
|
|
void CTextureManager::LoadTextures(const CTerrainPropertiesPtr& props, const char* dir)
|
2005-08-07 23:58:36 +02:00
|
|
|
{
|
2007-12-20 21:21:45 +01:00
|
|
|
VfsPaths pathnames;
|
|
|
|
if(fs_GetPathnames(g_VFS, dir, 0, pathnames) < 0)
|
2005-10-20 00:39:54 +02:00
|
|
|
return;
|
2007-12-20 21:21:45 +01:00
|
|
|
for(size_t i = 0; i < pathnames.size(); i++)
|
2005-10-20 00:39:54 +02:00
|
|
|
{
|
2007-12-20 21:21:45 +01:00
|
|
|
const char* texture_name = pathnames[i].string().c_str();
|
2005-10-20 00:39:54 +02:00
|
|
|
|
2005-10-24 01:57:59 +02: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 18:26:16 +02:00
|
|
|
if(!tex_is_known_extension(texture_name))
|
2005-10-28 03:43:16 +02:00
|
|
|
continue;
|
2005-10-24 01:57:59 +02:00
|
|
|
|
2005-10-20 00:39:54 +02: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 18:26:16 +02:00
|
|
|
const char* ext = path_extension(texture_name);
|
|
|
|
SAFE_STRCPY(xml_name + (ext-texture_name), "xml");
|
2005-10-20 00:39:54 +02:00
|
|
|
|
2005-10-28 03:43:16 +02:00
|
|
|
CTerrainPropertiesPtr myprops;
|
2005-10-20 00:39:54 +02:00
|
|
|
// Has XML file -> attempt to load properties
|
2007-12-20 21:21:45 +01:00
|
|
|
if (FileExists(xml_name))
|
2005-10-20 00:39:54 +02:00
|
|
|
{
|
2005-10-28 03:43:16 +02:00
|
|
|
myprops = GetPropertiesFromFile(props, xml_name);
|
2005-08-07 23:58:36 +02:00
|
|
|
if (myprops)
|
2007-12-29 17:22:23 +01:00
|
|
|
LOG(CLogger::Normal, LOG_CATEGORY, "CTextureManager: Successfully loaded override xml %s for texture %s", xml_name, texture_name);
|
2005-10-20 00:39:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Error or non-existant xml file -> use parent props
|
|
|
|
if (!myprops)
|
|
|
|
myprops = props;
|
|
|
|
|
|
|
|
AddTexture(myprops, texture_name);
|
|
|
|
}
|
2005-08-07 23:58:36 +02:00
|
|
|
}
|
|
|
|
|
2008-07-12 12:45:11 +02:00
|
|
|
void CTextureManager::RecurseDirectory(const CTerrainPropertiesPtr& parentProps, const char* cur_dir_path)
|
2004-05-29 22:56:24 +02:00
|
|
|
{
|
2007-12-29 17:22:23 +01:00
|
|
|
//LOG(CLogger::Normal, LOG_CATEGORY, "CTextureManager::RecurseDirectory(%s)", path.c_str());
|
2005-10-20 00:39:54 +02:00
|
|
|
|
2005-10-28 03:43:16 +02:00
|
|
|
CTerrainPropertiesPtr props;
|
2005-10-20 00:39:54 +02:00
|
|
|
|
|
|
|
// Load terrains.xml first, if it exists
|
|
|
|
char fn[PATH_MAX];
|
2006-05-22 01:22:55 +02:00
|
|
|
snprintf(fn, PATH_MAX, "%s%s", cur_dir_path, "terrains.xml");
|
2006-09-28 22:41:12 +02:00
|
|
|
fn[PATH_MAX-1] = '\0';
|
2007-12-20 21:21:45 +01:00
|
|
|
if (FileExists(fn))
|
2005-10-28 03:43:16 +02:00
|
|
|
props = GetPropertiesFromFile(parentProps, fn);
|
2005-08-07 23:58:36 +02:00
|
|
|
|
2005-08-10 03:27:33 +02:00
|
|
|
// No terrains.xml, or read failures -> use parent props (i.e.
|
|
|
|
if (!props)
|
2005-08-07 23:58:36 +02:00
|
|
|
{
|
2007-12-29 17:22:23 +01:00
|
|
|
LOG(CLogger::Normal, LOG_CATEGORY,
|
2005-10-20 00:39:54 +02:00
|
|
|
"CTextureManager::RecurseDirectory(%s): no terrains.xml (or errors while loading) - using parent properties", cur_dir_path);
|
2005-08-07 23:58:36 +02:00
|
|
|
props = parentProps;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recurse once for each subdirectory
|
2007-12-20 21:21:45 +01:00
|
|
|
DirectoryNames subdirectoryNames;
|
2008-07-12 12:45:11 +02:00
|
|
|
(void)g_VFS->GetDirectoryEntries(cur_dir_path, 0, &subdirectoryNames);
|
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 20:48:32 +02:00
|
|
|
for (size_t i=0;i<subdirectoryNames.size();i++)
|
2004-06-02 16:27:54 +02:00
|
|
|
{
|
2007-12-20 21:21:45 +01:00
|
|
|
char subdirectoryPath[PATH_MAX];
|
|
|
|
path_append(subdirectoryPath, cur_dir_path, subdirectoryNames[i].c_str(), PATH_APPEND_SLASH);
|
|
|
|
RecurseDirectory(props, subdirectoryPath);
|
2004-05-29 22:56:24 +02:00
|
|
|
}
|
|
|
|
|
2005-10-24 01:57:59 +02:00
|
|
|
LoadTextures(props, cur_dir_path);
|
2004-05-29 22:56:24 +02:00
|
|
|
}
|
|
|
|
|
2005-08-09 17:55:44 +02:00
|
|
|
|
2005-05-03 23:36:57 +02:00
|
|
|
int CTextureManager::LoadTerrainTextures()
|
2004-05-29 22:56:24 +02:00
|
|
|
{
|
2006-05-17 16:48:18 +02:00
|
|
|
RecurseDirectory(CTerrainPropertiesPtr(), "art/textures/terrain/types/");
|
2005-06-20 17:14:36 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2006-07-20 16:37:58 +02:00
|
|
|
CTerrainGroup *CTextureManager::FindGroup(const CStr& name)
|
2005-06-20 17:14:36 +02:00
|
|
|
{
|
2005-08-07 23:58:36 +02:00
|
|
|
TerrainGroupMap::const_iterator it=m_TerrainGroups.find(name);
|
|
|
|
if (it != m_TerrainGroups.end())
|
2005-06-20 17:14:36 +02:00
|
|
|
return it->second;
|
|
|
|
else
|
2005-08-07 23:58:36 +02:00
|
|
|
return m_TerrainGroups[name] = new CTerrainGroup(name, ++m_LastGroupIndex);
|
2005-06-20 19:34:17 +02:00
|
|
|
}
|
|
|
|
|
2005-08-07 23:58:36 +02:00
|
|
|
void CTerrainGroup::AddTerrain(CTextureEntry *pTerrain)
|
2005-06-20 17:14:36 +02:00
|
|
|
{
|
|
|
|
m_Terrains.push_back(pTerrain);
|
|
|
|
}
|
|
|
|
|
2005-08-07 23:58:36 +02:00
|
|
|
void CTerrainGroup::RemoveTerrain(CTextureEntry *pTerrain)
|
2005-06-20 17:14:36 +02:00
|
|
|
{
|
2006-09-26 03:44:20 +02:00
|
|
|
std::vector<CTextureEntry *>::iterator it;
|
2005-06-20 17:14:36 +02:00
|
|
|
it=find(m_Terrains.begin(), m_Terrains.end(), pTerrain);
|
|
|
|
if (it != m_Terrains.end())
|
|
|
|
m_Terrains.erase(it);
|
2004-05-29 22:56:24 +02:00
|
|
|
}
|