Implemented resource manager which can load textures and levels.
Implemented script to generate Resources.g.h file linking assets to code. Added assets from old project.
This commit is contained in:
78
src/resources/ResourceManager/ResourceManager.cpp
Normal file
78
src/resources/ResourceManager/ResourceManager.cpp
Normal file
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* 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_renderer(nullptr),
|
||||
m_loadedLevel(nullptr),
|
||||
m_loadedResources(new LoadedResource[sizeof(RInfo) / sizeof(RInfo[0])])
|
||||
{
|
||||
}
|
||||
|
||||
ResourceManager::~ResourceManager()
|
||||
{
|
||||
delete[] m_loadedResources;
|
||||
}
|
||||
|
||||
void ResourceManager::initialize(SDL_Renderer* renderer)
|
||||
{
|
||||
m_renderer = renderer;
|
||||
}
|
||||
|
||||
void ResourceManager::loadMainMenu()
|
||||
{
|
||||
}
|
||||
|
||||
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(RInfo[id].path, 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 */
|
99
src/resources/ResourceManager/ResourceManager_Levels.cpp
Normal file
99
src/resources/ResourceManager/ResourceManager_Levels.cpp
Normal file
@ -0,0 +1,99 @@
|
||||
/*
|
||||
* ResourceManager_Levels.cpp
|
||||
*
|
||||
* Created on: Nov 11, 2016
|
||||
* Author: tibi
|
||||
*/
|
||||
|
||||
#include <resources/ResourceManager.h>
|
||||
#include <resources/Resources.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
#include <json.hpp>
|
||||
|
||||
using namespace nlohmann;
|
||||
|
||||
namespace farmlands {
|
||||
namespace resources {
|
||||
|
||||
void ResourceManager::loadLevelLayer(std::shared_ptr<model::Level> level, size_t layer, int resourceId)
|
||||
{
|
||||
assert(RInfo[resourceId].type == ResourceType::LevelLayer);
|
||||
|
||||
char buffer[1024 * 10];
|
||||
|
||||
// Open file
|
||||
std::ifstream in(getPath(resourceId));
|
||||
if (!in)
|
||||
throw 0; // TODO: replace with exception type
|
||||
|
||||
// Read CSV file line by line
|
||||
for (size_t row = 0; row < level->rowCount(); row++)
|
||||
{
|
||||
in.getline(buffer, sizeof(buffer));
|
||||
|
||||
if (in.eof())
|
||||
throw 0; // TODO: replace with exception type
|
||||
|
||||
// Separated by comma (or maybe semicolon)
|
||||
char* nextNum = strtok(buffer, ",;");
|
||||
for (size_t col = 0; col < level->columnCount() && nextNum != NULL; col++)
|
||||
{
|
||||
model::Cell cell = (model::Cell)strtol(nextNum, NULL, 10);
|
||||
level->setCell(layer, row, col, cell);
|
||||
|
||||
nextNum = strtok(NULL, ",;");
|
||||
}
|
||||
}
|
||||
|
||||
in.close();
|
||||
}
|
||||
|
||||
|
||||
void ResourceManager::loadLevel(int levelId)
|
||||
{
|
||||
// Sanity checks
|
||||
assert(RInfo[levelId].type == ResourceType::Level);
|
||||
|
||||
// Open file
|
||||
std::ifstream levelIn(getPath(levelId));
|
||||
if (!levelIn)
|
||||
throw 0; // TODO: replace with exception type
|
||||
|
||||
// Parse file
|
||||
json levelJs;
|
||||
levelIn>>levelJs;
|
||||
|
||||
size_t layerCount = levelJs.value("layerCount", 0u);
|
||||
size_t rowCount = levelJs.value("height", 0u);
|
||||
size_t colCount = levelJs.value("width", 0u);
|
||||
|
||||
assert(layerCount > 0 && rowCount > 0 && colCount > 0);
|
||||
std::shared_ptr<model::Level> level(new model::Level(layerCount, rowCount, colCount));
|
||||
|
||||
level->m_cellWidth = levelJs.value("cellWidth", 0u);
|
||||
level->m_cellHeight = levelJs.value("cellHeight", 0u);
|
||||
|
||||
// Read layers
|
||||
json layersJs = levelJs["layers"];
|
||||
size_t layer = 0;
|
||||
|
||||
for (auto it = layersJs.begin(); it != layersJs.end(); it++, layer++)
|
||||
{
|
||||
std::string cellsFileName = it->value("cellsFile", std::string());
|
||||
loadLevelLayer(level, layer, getId(cellsFileName));
|
||||
|
||||
std::string textureFileName = it->value("textureFile", std::string());
|
||||
loadTexture(getId(textureFileName));
|
||||
}
|
||||
|
||||
m_loadedLevel = level;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
47
src/resources/ResourceManager/ResourceManager_Textures.cpp
Normal file
47
src/resources/ResourceManager/ResourceManager_Textures.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* ResourceManager_Textures.cpp
|
||||
*
|
||||
* Created on: Nov 12, 2016
|
||||
* Author: tibi
|
||||
*/
|
||||
|
||||
#include <resources/ResourceManager.h>
|
||||
#include <resources/Resources.h>
|
||||
|
||||
#include <cassert>
|
||||
|
||||
namespace farmlands {
|
||||
namespace resources {
|
||||
|
||||
void ResourceManager::loadTexture(int resourceId)
|
||||
{
|
||||
assert(RInfo[resourceId].type == ResourceType::Texture);
|
||||
|
||||
// Open file
|
||||
SDL_Surface* surface = IMG_Load(getPath(resourceId).c_str());
|
||||
if (surface == NULL)
|
||||
throw 0; // TODO: error handling
|
||||
|
||||
SDL_Texture* texture = SDL_CreateTextureFromSurface(m_renderer, surface);
|
||||
if (texture == NULL)
|
||||
throw 0; // TODO: error handling
|
||||
|
||||
m_loadedResources[resourceId].texture.m_surface = surface;
|
||||
m_loadedResources[resourceId].texture.m_texture = texture;
|
||||
}
|
||||
|
||||
SDL_Texture* ResourceManager::texture(int id)
|
||||
{
|
||||
assert(RInfo[id].type == ResourceType::Texture);
|
||||
return m_loadedResources[id].texture.m_texture;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user