110 lines
2.1 KiB
C++
110 lines
2.1 KiB
C++
/*
|
|
* ResourceManager.cpp
|
|
*
|
|
* Created on: Nov 7, 2016
|
|
* Author: tibi
|
|
*/
|
|
|
|
#include <resources/ResourceManager.h>
|
|
#include <resources/Resources.h>
|
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
#define FONTID(id,size) (id * 1000 + size)
|
|
#define FONTID_SIZE(fontid) (fontid % 1000)
|
|
#define FONTID_ID(fontid) (fontid / 1000)
|
|
|
|
namespace farmlands {
|
|
namespace resources {
|
|
|
|
ResourceManager::ResourceManager()
|
|
: m_gameState(nullptr),
|
|
m_loadedResources(new LoadedResource[sizeof(RInfo) / sizeof(RInfo[0])])
|
|
{
|
|
for(size_t i = 0; i < sizeof(RInfo) / sizeof(RInfo[0]); i++)
|
|
m_loadedResources[i].loaded = false;
|
|
}
|
|
|
|
ResourceManager::~ResourceManager()
|
|
{
|
|
// Unload resources
|
|
for(size_t i = 0; i < sizeof(RInfo) / sizeof(RInfo[0]); i++)
|
|
{
|
|
if (m_loadedResources[i].loaded)
|
|
{
|
|
switch(RInfo[i].type)
|
|
{
|
|
case ResourceType::Level:
|
|
delete m_loadedResources[i].level;
|
|
break;
|
|
|
|
case ResourceType::Texture:
|
|
SDL_DestroyTexture(m_loadedResources[i].texture.texture);
|
|
SDL_FreeSurface(m_loadedResources[i].texture.surface);
|
|
break;
|
|
|
|
default:
|
|
assert(false);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
delete[] m_loadedResources;
|
|
}
|
|
|
|
void ResourceManager::initialize(GameState* gameState)
|
|
{
|
|
m_gameState = gameState;
|
|
}
|
|
|
|
void ResourceManager::loadMainMenu()
|
|
{
|
|
}
|
|
|
|
void ResourceManager::loadGameAssets()
|
|
{
|
|
loadTexture(R::Player::Default);
|
|
}
|
|
|
|
TTF_Font* ResourceManager::font(int id, int pointSize)
|
|
{
|
|
// Open from cache
|
|
auto it = m_fontCache.find(FONTID(id, pointSize));
|
|
if (it != m_fontCache.end())
|
|
return it->second;
|
|
|
|
// Open font
|
|
TTF_Font* font = TTF_OpenFont(getPath(id).c_str(), pointSize);
|
|
m_fontCache.emplace(FONTID(id, pointSize), font);
|
|
|
|
return font;
|
|
}
|
|
|
|
std::string ResourceManager::getPath(int resourceId)
|
|
{
|
|
boost::filesystem::path resPath(ASSETS_DIR);
|
|
resPath.append(RInfo[resourceId].path);
|
|
|
|
return resPath.native();
|
|
}
|
|
|
|
int ResourceManager::getId(std::string resPathStr)
|
|
{
|
|
boost::filesystem::path resPath(resPathStr);
|
|
|
|
for(size_t i = 0; i < sizeof(RInfo) / sizeof(RInfo[0]); i++)
|
|
{
|
|
boost::filesystem::path iPath (RInfo[i].path);
|
|
if (resPath == iPath)
|
|
return (int)i;
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
} /* namespace storage */
|
|
} /* namespace farmlands */
|
|
|
|
|