Project structure update
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
#include "Assets.h"
|
||||
|
||||
#include "../graphics/Texture.h"
|
||||
#include "../graphics/Shader.h"
|
||||
#include "../graphics/Font.h"
|
||||
|
||||
Assets::~Assets() {
|
||||
for (auto& iter : shaders){
|
||||
delete iter.second;
|
||||
}
|
||||
|
||||
for (auto& iter : textures){
|
||||
delete iter.second;
|
||||
}
|
||||
|
||||
for (auto& iter : fonts){
|
||||
delete iter.second;
|
||||
}
|
||||
}
|
||||
|
||||
Texture* Assets::getTexture(std::string name) const {
|
||||
auto found = textures.find(name);
|
||||
if (found == textures.end())
|
||||
return nullptr;
|
||||
return found->second;
|
||||
}
|
||||
|
||||
void Assets::store(Texture* texture, std::string name){
|
||||
textures[name] = texture;
|
||||
}
|
||||
|
||||
|
||||
Shader* Assets::getShader(std::string name) const{
|
||||
auto found = shaders.find(name);
|
||||
if (found == shaders.end())
|
||||
return nullptr;
|
||||
return found->second;
|
||||
}
|
||||
|
||||
void Assets::store(Shader* shader, std::string name){
|
||||
shaders[name] = shader;
|
||||
}
|
||||
|
||||
|
||||
Font* Assets::getFont(std::string name) const {
|
||||
auto found = fonts.find(name);
|
||||
if (found == fonts.end())
|
||||
return nullptr;
|
||||
return found->second;
|
||||
}
|
||||
|
||||
void Assets::store(Font* font, std::string name){
|
||||
fonts[name] = font;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#ifndef ASSETS_ASSETS_H_
|
||||
#define ASSETS_ASSETS_H_
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
class Texture;
|
||||
class Shader;
|
||||
class Font;
|
||||
|
||||
class Assets {
|
||||
std::unordered_map<std::string, Texture*> textures;
|
||||
std::unordered_map<std::string, Shader*> shaders;
|
||||
std::unordered_map<std::string, Font*> fonts;
|
||||
public:
|
||||
~Assets();
|
||||
Texture* getTexture(std::string name) const;
|
||||
void store(Texture* texture, std::string name);
|
||||
|
||||
Shader* getShader(std::string name) const;
|
||||
void store(Shader* shader, std::string name);
|
||||
|
||||
Font* getFont(std::string name) const;
|
||||
void store(Font* font, std::string name);
|
||||
};
|
||||
|
||||
#endif /* ASSETS_ASSETS_H_ */
|
||||
@@ -0,0 +1,92 @@
|
||||
#include "AssetsLoader.h"
|
||||
#include "Assets.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
AssetsLoader::AssetsLoader(Assets* assets) : assets(assets) {
|
||||
}
|
||||
|
||||
void AssetsLoader::addLoader(int tag, aloader_func func) {
|
||||
loaders[tag] = func;
|
||||
}
|
||||
|
||||
void AssetsLoader::add(int tag, const std::string filename, const std::string alias) {
|
||||
entries.push(aloader_entry{ tag, filename, alias });
|
||||
}
|
||||
|
||||
bool AssetsLoader::hasNext() const {
|
||||
return !entries.empty();
|
||||
}
|
||||
|
||||
bool AssetsLoader::loadNext() {
|
||||
const aloader_entry& entry = entries.front();
|
||||
std::cout << " loading " << entry.filename << " as " << entry.alias << std::endl;
|
||||
std::cout.flush();
|
||||
auto found = loaders.find(entry.tag);
|
||||
if (found == loaders.end()) {
|
||||
std::cerr << "unknown asset tag " << entry.tag << std::endl;
|
||||
return false;
|
||||
}
|
||||
aloader_func loader = found->second;
|
||||
bool status = loader(assets, entry.filename, entry.alias);
|
||||
entries.pop();
|
||||
return status;
|
||||
}
|
||||
|
||||
#include "../coders/png.h"
|
||||
#include "../graphics/Shader.h"
|
||||
#include "../graphics/Texture.h"
|
||||
#include "../graphics/Font.h"
|
||||
|
||||
bool _load_shader(Assets* assets, const std::string& filename, const std::string& name) {
|
||||
Shader* shader = load_shader(filename + ".glslv", filename + ".glslf");
|
||||
if (shader == nullptr) {
|
||||
std::cerr << "failed to load shader '" << name << "'" << std::endl;
|
||||
return false;
|
||||
}
|
||||
assets->store(shader, name);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool _load_texture(Assets* assets, const std::string& filename, const std::string& name) {
|
||||
Texture* texture = png::load_texture(filename);
|
||||
if (texture == nullptr) {
|
||||
std::cerr << "failed to load texture '" << name << "'" << std::endl;
|
||||
return false;
|
||||
}
|
||||
assets->store(texture, name);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool _load_font(Assets* assets, const std::string& filename, const std::string& name) {
|
||||
std::vector<Texture*> pages;
|
||||
for (size_t i = 0; i <= 4; i++) {
|
||||
Texture* texture = png::load_texture(filename + "_" + std::to_string(i) + ".png");
|
||||
if (texture == nullptr) {
|
||||
std::cerr << "failed to load bitmap font '" << name << "' (missing page " << std::to_string(i) << ")" << std::endl;
|
||||
return false;
|
||||
}
|
||||
pages.push_back(texture);
|
||||
}
|
||||
Font* font = new Font(pages);
|
||||
assets->store(font, name);
|
||||
return true;
|
||||
}
|
||||
|
||||
void AssetsLoader::createDefaults(AssetsLoader& loader) {
|
||||
loader.addLoader(ASSET_SHADER, _load_shader);
|
||||
loader.addLoader(ASSET_TEXTURE, _load_texture);
|
||||
loader.addLoader(ASSET_FONT, _load_font);
|
||||
}
|
||||
|
||||
void AssetsLoader::addDefaults(AssetsLoader& loader) {
|
||||
loader.add(ASSET_SHADER, "res/main", "main");
|
||||
loader.add(ASSET_SHADER, "res/crosshair", "crosshair");
|
||||
loader.add(ASSET_SHADER, "res/lines", "lines");
|
||||
loader.add(ASSET_SHADER, "res/ui", "ui");
|
||||
|
||||
loader.add(ASSET_TEXTURE, "res/block.png", "block");
|
||||
loader.add(ASSET_TEXTURE, "res/slot.png", "slot");
|
||||
|
||||
loader.add(ASSET_FONT, "res/font", "normal");
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#ifndef ASSETS_ASSETS_LOADER_H
|
||||
#define ASSETS_ASSETS_LOADER_H
|
||||
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <queue>
|
||||
|
||||
#define ASSET_TEXTURE 1
|
||||
#define ASSET_SHADER 2
|
||||
#define ASSET_FONT 3
|
||||
|
||||
class Assets;
|
||||
|
||||
typedef std::function<bool(Assets*, const std::string&, const std::string&)> aloader_func;
|
||||
|
||||
struct aloader_entry {
|
||||
int tag;
|
||||
const std::string filename;
|
||||
const std::string alias;
|
||||
};
|
||||
|
||||
class AssetsLoader {
|
||||
Assets* assets;
|
||||
std::map<int, aloader_func> loaders;
|
||||
std::queue<aloader_entry> entries;
|
||||
public:
|
||||
AssetsLoader(Assets* assets);
|
||||
void addLoader(int tag, aloader_func func);
|
||||
void add(int tag, const std::string filename, const std::string alias);
|
||||
|
||||
bool hasNext() const;
|
||||
bool loadNext();
|
||||
|
||||
static void createDefaults(AssetsLoader& loader);
|
||||
static void addDefaults(AssetsLoader& loader);
|
||||
};
|
||||
|
||||
#endif // ASSETS_ASSETS_LOADER_H
|
||||
Reference in New Issue
Block a user