Assets loading refactor
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
#include "AssetsLoader.h"
|
||||
#include "Assets.h"
|
||||
|
||||
#include "asset_loaders.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
@@ -40,69 +42,11 @@ bool AssetsLoader::loadNext() {
|
||||
return status;
|
||||
}
|
||||
|
||||
#include "../coders/png.h"
|
||||
#include "../graphics/Shader.h"
|
||||
#include "../graphics/ImageData.h"
|
||||
#include "../graphics/Texture.h"
|
||||
#include "../graphics/Atlas.h"
|
||||
#include "../graphics/Font.h"
|
||||
|
||||
bool _load_shader(Assets* assets, const path& filename, const std::string& name) {
|
||||
Shader* shader = Shader::loadShader(filename.string() + ".glslv", filename.string() + ".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 path& filename, const std::string& name) {
|
||||
Texture* texture = png::load_texture(filename.string());
|
||||
if (texture == nullptr) {
|
||||
std::cerr << "failed to load texture '" << name << "'" << std::endl;
|
||||
return false;
|
||||
}
|
||||
assets->store(texture, name);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool _load_atlas(Assets* assets, const path& filename, const std::string& name) {
|
||||
AtlasBuilder builder;
|
||||
for (const auto& entry : std::filesystem::directory_iterator(filename)) {
|
||||
std::filesystem::path file = entry.path();
|
||||
if (file.extension() == ".png") {
|
||||
std::string name = file.stem().string();
|
||||
std::unique_ptr<ImageData> image (png::load_image(file.string()));
|
||||
image->fixAlphaColor();
|
||||
builder.add(name, image.release());
|
||||
}
|
||||
}
|
||||
Atlas* atlas = builder.build(2);
|
||||
assets->store(atlas, name);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool _load_font(Assets* assets, const path& filename, const std::string& name) {
|
||||
std::vector<Texture*> pages;
|
||||
for (size_t i = 0; i <= 4; i++) {
|
||||
Texture* texture = png::load_texture(filename.string() + "_" + 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, pages[0]->height / 16);
|
||||
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);
|
||||
loader.addLoader(ASSET_ATLAS, _load_atlas);
|
||||
loader.addLoader(ASSET_SHADER, assetload::shader);
|
||||
loader.addLoader(ASSET_TEXTURE, assetload::texture);
|
||||
loader.addLoader(ASSET_FONT, assetload::font);
|
||||
loader.addLoader(ASSET_ATLAS, assetload::atlas);
|
||||
}
|
||||
|
||||
void AssetsLoader::addDefaults(AssetsLoader& loader) {
|
||||
|
||||
Reference in New Issue
Block a user