Changes in texture code & refactoring

* refactored and altered slightly the texture pack code
 * added flushing of texture cache
 * some refactoring and const-correctness in CApplication methods
dev-ui
Piotr Dziwinski 2013-05-26 14:50:23 +02:00
parent b41957f2f9
commit 8f6fd2a131
4 changed files with 173 additions and 148 deletions

View File

@ -146,25 +146,24 @@ CApplication::CApplication()
m_dataPath = COLOBOT_DEFAULT_DATADIR;
m_langPath = COLOBOT_I18N_DIR;
m_texPackPath = "";
m_language = LANGUAGE_ENV;
m_texPack = "";
m_lowCPU = true;
for (int i = 0; i < DIR_MAX; ++i)
m_dataDirs[i] = nullptr;
m_standardDataDirs[i] = nullptr;
m_dataDirs[DIR_AI] = "ai";
m_dataDirs[DIR_FONT] = "fonts";
m_dataDirs[DIR_HELP] = "help";
m_dataDirs[DIR_ICON] = "icons";
m_dataDirs[DIR_LEVEL] = "levels";
m_dataDirs[DIR_MODEL] = "models";
m_dataDirs[DIR_MUSIC] = "music";
m_dataDirs[DIR_SOUND] = "sounds";
m_dataDirs[DIR_TEXTURE] = "textures";
m_standardDataDirs[DIR_AI] = "ai";
m_standardDataDirs[DIR_FONT] = "fonts";
m_standardDataDirs[DIR_HELP] = "help";
m_standardDataDirs[DIR_ICON] = "icons";
m_standardDataDirs[DIR_LEVEL] = "levels";
m_standardDataDirs[DIR_MODEL] = "models";
m_standardDataDirs[DIR_MUSIC] = "music";
m_standardDataDirs[DIR_SOUND] = "sounds";
m_standardDataDirs[DIR_TEXTURE] = "textures";
}
CApplication::~CApplication()
@ -272,7 +271,7 @@ ParseArgsStatus CApplication::ParseArguments(int argc, char *argv[])
GetLogger()->Message(" -language lang set language (one of: en, de, fr, pl)\n");
GetLogger()->Message(" -langdir path set custom language directory path\n");
GetLogger()->Message(" -vbo mode set OpenGL VBO mode (one of: auto, enable, disable)\n");
GetLogger()->Message(" -texpack name set texture pack\n");
GetLogger()->Message(" -texpack path set path to custom texture pack\n");
return PARSE_ARGS_HELP;
}
case OPT_DEBUG:
@ -286,6 +285,18 @@ ParseArgsStatus CApplication::ParseArguments(int argc, char *argv[])
GetLogger()->Info("Using custom data dir: '%s'\n", m_dataPath.c_str());
break;
}
case OPT_LANGDIR:
{
m_langPath = optarg;
GetLogger()->Info("Using custom language dir: '%s'\n", m_langPath.c_str());
break;
}
case OPT_TEXPACK:
{
m_texPackPath = optarg;
GetLogger()->Info("Using texturepack: '%s'\n", m_texPackPath.c_str());
break;
}
case OPT_LOGLEVEL:
{
LogLevel logLevel;
@ -312,12 +323,6 @@ ParseArgsStatus CApplication::ParseArguments(int argc, char *argv[])
m_language = language;
break;
}
case OPT_LANGDIR:
{
m_langPath = optarg;
GetLogger()->Info("Using custom language dir: '%s'\n", m_langPath.c_str());
break;
}
case OPT_VBO:
{
std::string vbo;
@ -336,12 +341,6 @@ ParseArgsStatus CApplication::ParseArguments(int argc, char *argv[])
break;
}
case OPT_TEXPACK:
{
m_texPack = optarg;
GetLogger()->Info("Using texturepack: '%s'\n", m_texPack.c_str());
break;
}
default:
assert(false); // should never get here
}
@ -394,20 +393,27 @@ bool CApplication::Create()
m_sound->Create(true);
// Cache sound files
if (defaultValues) {
if (defaultValues)
{
GetProfile().SetLocalProfileString("Resources", "Sound", GetDataSubdirPath(DIR_SOUND));
GetProfile().SetLocalProfileString("Resources", "Music", GetDataSubdirPath(DIR_MUSIC));
}
if (GetProfile().GetLocalProfileString("Resources", "Sound", path)) {
if (GetProfile().GetLocalProfileString("Resources", "Sound", path))
{
m_sound->CacheAll(path);
} else {
}
else
{
m_sound->CacheAll(GetDataSubdirPath(DIR_SOUND));
}
if (GetProfile().GetLocalProfileString("Resources", "Music", path)) {
if (GetProfile().GetLocalProfileString("Resources", "Music", path))
{
m_sound->AddMusicFiles(path);
} else {
}
else
{
m_sound->AddMusicFiles(GetDataSubdirPath(DIR_MUSIC));
}
@ -496,8 +502,6 @@ bool CApplication::Create()
m_engine->SetDevice(m_device);
m_engine->SetTexturePack(m_texPack);
if (! m_engine->Create() )
{
m_errorMessage = std::string("Error in CEngine::Init()\n") + standardInfoMessage;
@ -970,12 +974,12 @@ end:
return m_exitCode;
}
int CApplication::GetExitCode()
int CApplication::GetExitCode() const
{
return m_exitCode;
}
const std::string& CApplication::GetErrorMessage()
const std::string& CApplication::GetErrorMessage() const
{
return m_errorMessage;
}
@ -1269,7 +1273,7 @@ void CApplication::ResumeSimulation()
GetLogger()->Info("Resume simulation\n");
}
bool CApplication::GetSimulationSuspended()
bool CApplication::GetSimulationSuspended() const
{
return m_simulationSuspended;
}
@ -1327,48 +1331,48 @@ Event CApplication::CreateUpdateEvent()
return frameEvent;
}
float CApplication::GetSimulationSpeed()
float CApplication::GetSimulationSpeed() const
{
return m_simulationSpeed;
}
float CApplication::GetAbsTime()
float CApplication::GetAbsTime() const
{
return m_absTime;
}
long long CApplication::GetExactAbsTime()
long long CApplication::GetExactAbsTime() const
{
return m_exactAbsTime;
}
long long CApplication::GetRealAbsTime()
long long CApplication::GetRealAbsTime() const
{
return m_realAbsTime;
}
float CApplication::GetRelTime()
float CApplication::GetRelTime() const
{
return m_relTime;
}
long long CApplication::GetExactRelTime()
long long CApplication::GetExactRelTime() const
{
return m_exactRelTime;
}
long long CApplication::GetRealRelTime()
long long CApplication::GetRealRelTime() const
{
return m_realRelTime;
}
Gfx::GLDeviceConfig CApplication::GetVideoConfig()
Gfx::GLDeviceConfig CApplication::GetVideoConfig() const
{
return m_deviceConfig;
}
VideoQueryResult CApplication::GetVideoResolutionList(std::vector<Math::IntPoint> &resolutions,
bool fullScreen, bool resizeable)
bool fullScreen, bool resizeable) const
{
resolutions.clear();
@ -1415,27 +1419,27 @@ void CApplication::SetDebugMode(bool mode)
m_debugMode = mode;
}
bool CApplication::GetDebugMode()
bool CApplication::GetDebugMode() const
{
return m_debugMode;
}
int CApplication::GetKmods()
int CApplication::GetKmods() const
{
return m_kmodState;
}
bool CApplication::GetKmodState(int kmod)
bool CApplication::GetKmodState(int kmod) const
{
return (m_kmodState & kmod) != 0;
}
bool CApplication::GetTrackedKeyState(TrackedKey key)
bool CApplication::GetTrackedKeyState(TrackedKey key) const
{
return (m_trackedKeys & key) != 0;
}
bool CApplication::GetMouseButtonState(int index)
bool CApplication::GetMouseButtonState(int index) const
{
return (m_mouseButtonsState & (1<<index)) != 0;
}
@ -1453,7 +1457,7 @@ void CApplication::SetGrabInput(bool grab)
SDL_WM_GrabInput(grab ? SDL_GRAB_ON : SDL_GRAB_OFF);
}
bool CApplication::GetGrabInput()
bool CApplication::GetGrabInput() const
{
int result = SDL_WM_GrabInput(SDL_GRAB_QUERY);
return result == SDL_GRAB_ON;
@ -1468,12 +1472,12 @@ void CApplication::SetMouseMode(MouseMode mode)
SDL_ShowCursor(SDL_DISABLE);
}
MouseMode CApplication::GetMouseMode()
MouseMode CApplication::GetMouseMode() const
{
return m_mouseMode;
}
Math::Point CApplication::GetMousePos()
Math::Point CApplication::GetMousePos() const
{
return m_mousePos;
}
@ -1486,7 +1490,7 @@ void CApplication::MoveMouse(Math::Point pos)
SDL_WarpMouse(windowPos.x, windowPos.y);
}
std::vector<JoystickDevice> CApplication::GetJoystickList()
std::vector<JoystickDevice> CApplication::GetJoystickList() const
{
std::vector<JoystickDevice> result;
@ -1503,7 +1507,7 @@ std::vector<JoystickDevice> CApplication::GetJoystickList()
return result;
}
JoystickDevice CApplication::GetJoystick()
JoystickDevice CApplication::GetJoystick() const
{
return m_joystick;
}
@ -1525,36 +1529,37 @@ void CApplication::SetJoystickEnabled(bool enable)
}
}
bool CApplication::GetJoystickEnabled()
bool CApplication::GetJoystickEnabled() const
{
return m_joystickEnabled;
}
std::string CApplication::GetDataDirPath()
std::string CApplication::GetDataDirPath() const
{
return m_dataPath;
}
std::string CApplication::GetDataSubdirPath(DataDir stdDir)
std::string CApplication::GetDataSubdirPath(DataDir stdDir) const
{
int index = static_cast<int>(stdDir);
assert(index >= 0 && index < DIR_MAX);
std::stringstream str;
str << m_dataPath;
str << "/";
str << m_dataDirs[index];
str << m_standardDataDirs[index];
return str.str();
}
std::string CApplication::GetDataFilePath(DataDir stdDir, const std::string& subpath)
std::string CApplication::GetDataFilePath(DataDir stdDir, const std::string& subpath) const
{
int index = static_cast<int>(stdDir);
assert(index >= 0 && index < DIR_MAX);
std::stringstream str;
str << m_dataPath;
str << "/";
str << m_dataDirs[index];
if (stdDir == DIR_HELP) {
str << m_standardDataDirs[index];
if (stdDir == DIR_HELP)
{
str << "/";
str << GetLanguageChar();
}
@ -1563,12 +1568,31 @@ std::string CApplication::GetDataFilePath(DataDir stdDir, const std::string& sub
return str.str();
}
Language CApplication::GetLanguage()
std::string CApplication::GetTexPackFilePath(const std::string& textureName) const
{
std::stringstream str;
if (! m_texPackPath.empty())
{
str << m_texPackPath;
str << "/";
str << textureName;
if (! boost::filesystem::exists(str.str()))
{
GetLogger()->Trace("Texture '%s' not in texpack\n", textureName.c_str());
str.str("");
}
}
return str.str();
}
Language CApplication::GetLanguage() const
{
return m_language;
}
char CApplication::GetLanguageChar()
char CApplication::GetLanguageChar() const
{
char langChar = 'E';
switch (m_language)
@ -1703,7 +1727,7 @@ void CApplication::SetLowCPU(bool low)
m_lowCPU = low;
}
bool CApplication::GetLowCPU()
bool CApplication::GetLowCPU() const
{
return m_lowCPU;
}
@ -1718,7 +1742,7 @@ void CApplication::StopPerformanceCounter(PerformanceCounter counter)
GetSystemUtils()->GetCurrentTimeStamp(m_performanceCounters[counter][1]);
}
float CApplication::GetPerformanceCounterData(PerformanceCounter counter)
float CApplication::GetPerformanceCounterData(PerformanceCounter counter) const
{
return m_performanceCountersData[counter];
}

View File

@ -209,20 +209,20 @@ public:
//! Main event loop
int Run();
//! Returns the code to be returned at main() exit
int GetExitCode();
int GetExitCode() const;
//! Returns the message of error (set to something if exit code is not 0)
const std::string& GetErrorMessage();
const std::string& GetErrorMessage() const;
//! Cleans up before exit
void Destroy();
//! Returns a list of possible video modes
VideoQueryResult GetVideoResolutionList(std::vector<Math::IntPoint> &resolutions,
bool fullScreen, bool resizeable);
bool fullScreen, bool resizeable) const;
//! Returns the current video mode
Gfx::GLDeviceConfig GetVideoConfig();
Gfx::GLDeviceConfig GetVideoConfig() const;
//! Change the video mode to given mode
bool ChangeVideoConfig(const Gfx::GLDeviceConfig &newConfig);
@ -232,35 +232,35 @@ public:
//! Resumes animation
void ResumeSimulation();
//! Returns whether simulation is suspended
bool GetSimulationSuspended();
bool GetSimulationSuspended() const;
//@{
//! Management of simulation speed
void SetSimulationSpeed(float speed);
float GetSimulationSpeed();
float GetSimulationSpeed() const;
//@}
//! Returns the absolute time counter [seconds]
float GetAbsTime();
float GetAbsTime() const;
//! Returns the exact absolute time counter [nanoseconds]
long long GetExactAbsTime();
long long GetExactAbsTime() const;
//! Returns the exact absolute time counter disregarding speed setting [nanoseconds]
long long GetRealAbsTime();
long long GetRealAbsTime() const;
//! Returns the relative time since last update [seconds]
float GetRelTime();
float GetRelTime() const;
//! Returns the exact realative time since last update [nanoseconds]
long long GetExactRelTime();
long long GetExactRelTime() const;
//! Returns the exact relative time since last update disregarding speed setting [nanoseconds]
long long GetRealRelTime();
long long GetRealRelTime() const;
//! Returns a list of available joystick devices
std::vector<JoystickDevice> GetJoystickList();
std::vector<JoystickDevice> GetJoystickList() const;
//! Returns info about the current joystick
JoystickDevice GetJoystick();
JoystickDevice GetJoystick() const;
//! Change the current joystick device
bool ChangeJoystick(const JoystickDevice &newJoystick);
@ -268,7 +268,7 @@ public:
//! Management of joystick enable state
//@{
void SetJoystickEnabled(bool enable);
bool GetJoystickEnabled();
bool GetJoystickEnabled() const;
//@}
//! Polls the state of joystick axes and buttons
@ -278,15 +278,15 @@ public:
void UpdateMouse();
//! Returns the current key modifiers
int GetKmods();
int GetKmods() const;
//! Returns whether the given kmod is active
bool GetKmodState(int kmod);
bool GetKmodState(int kmod) const;
//! Returns whether the tracked key is pressed
bool GetTrackedKeyState(TrackedKey key);
bool GetTrackedKeyState(TrackedKey key) const;
//! Returns whether the mouse button is pressed
bool GetMouseButtonState(int index);
bool GetMouseButtonState(int index) const;
//! Resets tracked key states and modifiers
void ResetKeyStates();
@ -294,17 +294,17 @@ public:
//! Management of the grab mode for input (keyboard & mouse)
//@{
void SetGrabInput(bool grab);
bool GetGrabInput();
bool GetGrabInput() const;
//@}
//! Management of mouse mode
//@{
void SetMouseMode(MouseMode mode);
MouseMode GetMouseMode();
MouseMode GetMouseMode() const;
//@}
//! Returns the position of mouse cursor (in interface coords)
Math::Point GetMousePos();
Math::Point GetMousePos() const;
//! Moves (warps) the mouse cursor to the specified position (in interface coords)
void MoveMouse(Math::Point pos);
@ -312,22 +312,25 @@ public:
//! Management of debug mode (prints more info in logger)
//@{
void SetDebugMode(bool mode);
bool GetDebugMode();
bool GetDebugMode() const;
//@}
//! Returns the full path to data directory
std::string GetDataDirPath();
std::string GetDataDirPath() const;
//! Returns the full path to a standard dir in data directory
std::string GetDataSubdirPath(DataDir stdDir);
std::string GetDataSubdirPath(DataDir stdDir) const;
//! Returns the full path to a file in data directory given standard dir and subpath
std::string GetDataFilePath(DataDir stdDir, const std::string &subpath);
std::string GetDataFilePath(DataDir stdDir, const std::string &subpath) const;
//! Returns the full path to a file in texture pack directory
std::string GetTexPackFilePath(const std::string& textureName) const;
//! Management of language
//@{
Language GetLanguage();
char GetLanguageChar();
Language GetLanguage() const;
char GetLanguageChar() const;
void SetLanguage(Language language);
static bool ParseLanguage(const std::string& str, Language& language);
//@}
@ -335,14 +338,14 @@ public:
//! Management of sleep in main loop (lowers CPU usage)
//@{
void SetLowCPU(bool low);
bool GetLowCPU();
bool GetLowCPU() const;
//@}
//! Management of performance counters
//@{
void StartPerformanceCounter(PerformanceCounter counter);
void StopPerformanceCounter(PerformanceCounter counter);
float GetPerformanceCounterData(PerformanceCounter counter);
float GetPerformanceCounterData(PerformanceCounter counter) const;
//@}
protected:
@ -462,14 +465,14 @@ protected:
//! Path to directory with language files
std::string m_langPath;
const char* m_dataDirs[DIR_MAX];
//! Path to directory with user texture pack
std::string m_texPackPath;
const char* m_standardDataDirs[DIR_MAX];
//! Application language
Language m_language;
//! Texture pack
std::string m_texPack;
//! Low cpu mode
bool m_lowCPU;
};

View File

@ -62,8 +62,6 @@ CEngine::CEngine(CApplication *app)
m_sound = nullptr;
m_terrain = nullptr;
m_texPack = "";
m_showStats = false;
m_focus = 0.75f;
@ -238,12 +236,6 @@ void CEngine::SetTerrain(CTerrain* terrain)
m_terrain = terrain;
}
void CEngine::SetTexturePack(const std::string& texpackName)
{
m_texPack = texpackName;
}
bool CEngine::Create()
{
m_size = m_app->GetVideoConfig().size;
@ -323,7 +315,7 @@ void CEngine::ResetAfterDeviceChanged()
m_text->FlushCache();
// TODO reload textures, reset device state, etc.
FlushTextureCache();
}
bool CEngine::ProcessEvent(const Event &event)
@ -2117,7 +2109,7 @@ void CEngine::SetViewParams(const Math::Vector& eyePt, const Math::Vector& looka
m_sound->SetListener(eyePt, lookatPt);
}
Texture CEngine::CreateTexture(const std::string& texName, const TextureCreateParams& params, CImage* image, std::string orginalName)
Texture CEngine::CreateTexture(const std::string& texName, const TextureCreateParams& params, CImage* image)
{
if (texName.empty())
return Texture(); // invalid texture
@ -2126,39 +2118,53 @@ Texture CEngine::CreateTexture(const std::string& texName, const TextureCreatePa
return Texture(); // invalid texture
Texture tex;
CImage img;
if (image == nullptr)
{
CImage img;
if (! img.Load(m_app->GetDataFilePath(DIR_TEXTURE, texName)))
bool loadedFromTexPack = false;
std::string texPackName = m_app->GetTexPackFilePath(texName);
if (! texPackName.empty())
{
std::string error = img.GetError();
if(orginalName == "") GetLogger()->Error("Couldn't load texture '%s': %s, blacklisting\n", texName.c_str(), error.c_str());
m_texBlacklist.insert(texName);
return Texture(); // invalid texture
if (img.Load(texPackName))
{
loadedFromTexPack = true;
}
else
{
std::string error = img.GetError();
GetLogger()->Error("Couldn't load texture '%s' from texpack: %s, blacklisting the texpack path\n",
texName.c_str(), error.c_str());
m_texBlacklist.insert(texPackName);
}
}
tex = m_device->CreateTexture(&img, params);
}
else
{
tex = m_device->CreateTexture(image, params);
if (!loadedFromTexPack)
{
if (! img.Load(m_app->GetDataFilePath(DIR_TEXTURE, texName)))
{
std::string error = img.GetError();
GetLogger()->Error("Couldn't load texture '%s': %s, blacklisting\n", texName.c_str(), error.c_str());
m_texBlacklist.insert(texName);
return Texture(); // invalid texture
}
}
image = &img;
}
tex = m_device->CreateTexture(&img, params);
if (! tex.Valid())
{
if(orginalName == "") GetLogger()->Error("Couldn't load texture '%s', blacklisting\n", texName.c_str());
GetLogger()->Error("Couldn't load texture '%s', blacklisting\n", texName.c_str());
m_texBlacklist.insert(texName);
return tex;
}
if(orginalName == "") {
m_texNameMap[texName] = tex;
m_revTexNameMap[tex] = texName;
} else {
m_texNameMap[orginalName] = tex;
m_revTexNameMap[tex] = orginalName;
}
m_texNameMap[texName] = tex;
m_revTexNameMap[tex] = texName;
return tex;
}
@ -2183,19 +2189,7 @@ Texture CEngine::LoadTexture(const std::string& name, const TextureCreateParams&
if (it != m_texNameMap.end())
return (*it).second;
Texture tex;
if (m_texPack != "") {
std::string name_texpack = m_texPack + "/" + name;
if (m_texBlacklist.find(name_texpack) == m_texBlacklist.end()) {
tex = CreateTexture(name_texpack, params, nullptr, name);
if (tex.Valid())
return tex;
}
}
tex = CreateTexture(name, params);
return tex;
return CreateTexture(name, params);
}
bool CEngine::LoadAllTextures()
@ -2453,6 +2447,13 @@ void CEngine::DeleteTexture(const Texture& tex)
m_texNameMap.erase(it);
}
void CEngine::FlushTextureCache()
{
m_texNameMap.clear();
m_revTexNameMap.clear();
m_texBlacklist.clear();
}
bool CEngine::SetTexture(const std::string& name, int stage)
{
auto it = m_texNameMap.find(name);

View File

@ -726,9 +726,6 @@ public:
//! Writes a screenshot containing the current frame
bool WriteScreenShot(const std::string& fileName, int width, int height);
//! Set texture pack
void SetTexturePack(const std::string& texpackName);
//@{
//! Management of game pause mode
@ -961,6 +958,9 @@ public:
//! Deletes the given texture, unloading it and removing from cache
void DeleteTexture(const Texture& tex);
//! Empties the texture cache
void FlushTextureCache();
//! Defines of the distance field of vision
void SetTerrainVision(float vision);
@ -1233,7 +1233,7 @@ protected:
const Material& mat, int state);
//! Create texture and add it to cache
Texture CreateTexture(const std::string &texName, const TextureCreateParams &params, CImage* image = nullptr, std::string orginalName = "");
Texture CreateTexture(const std::string &texName, const TextureCreateParams &params, CImage* image = nullptr);
//! Tests whether the given object is visible
bool IsVisible(int objRank);
@ -1407,9 +1407,6 @@ protected:
* so are disabled for subsequent load calls. */
std::set<std::string> m_texBlacklist;
//! Texture pack
std::string m_texPack;
//! Mouse cursor definitions
EngineMouse m_mice[ENG_MOUSE_COUNT];
//! Texture with mouse cursors