Assets loading refactor
This commit is contained in:
parent
2ebaad6cb6
commit
8c261efbd4
@ -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) {
|
||||
|
||||
80
src/assets/asset_loaders.cpp
Normal file
80
src/assets/asset_loaders.cpp
Normal file
@ -0,0 +1,80 @@
|
||||
#include "asset_loaders.h"
|
||||
|
||||
#include <iostream>
|
||||
#include "Assets.h"
|
||||
#include "../coders/png.h"
|
||||
#include "../graphics/Shader.h"
|
||||
#include "../graphics/Texture.h"
|
||||
#include "../graphics/ImageData.h"
|
||||
#include "../graphics/Atlas.h"
|
||||
#include "../graphics/Font.h"
|
||||
|
||||
using std::string;
|
||||
using std::vector;
|
||||
using std::unique_ptr;
|
||||
using std::filesystem::path;
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
bool assetload::texture(Assets* assets,
|
||||
const path& filename,
|
||||
const 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 assetload::shader(Assets* assets,
|
||||
const path& filename,
|
||||
const 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 assetload::atlas(Assets* assets,
|
||||
const path& directory,
|
||||
const string& name) {
|
||||
AtlasBuilder builder;
|
||||
for (const auto& entry : fs::directory_iterator(directory)) {
|
||||
path file = entry.path();
|
||||
if (file.extension() == ".png") {
|
||||
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 assetload::font(Assets* assets,
|
||||
const path& filename,
|
||||
const string& name) {
|
||||
vector<Texture*> pages;
|
||||
for (size_t i = 0; i <= 4; i++) {
|
||||
string name = filename.string() + "_" + std::to_string(i) + ".png";
|
||||
Texture* texture = png::load_texture(name);
|
||||
if (texture == nullptr) {
|
||||
std::cerr << "failed to load bitmap font '" << name;
|
||||
std::cerr << "' (missing page " << std::to_string(i) << ")";
|
||||
std::cerr << std::endl;
|
||||
return false;
|
||||
}
|
||||
pages.push_back(texture);
|
||||
}
|
||||
Font* font = new Font(pages, pages[0]->height / 16);
|
||||
assets->store(font, name);
|
||||
return true;
|
||||
}
|
||||
24
src/assets/asset_loaders.h
Normal file
24
src/assets/asset_loaders.h
Normal file
@ -0,0 +1,24 @@
|
||||
#ifndef ASSETS_ASSET_LOADERS_H_
|
||||
#define ASSETS_ASSET_LOADERS_H_
|
||||
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
|
||||
class Assets;
|
||||
|
||||
namespace assetload {
|
||||
bool texture(Assets* assets,
|
||||
const std::filesystem::path& filename,
|
||||
const std::string& name);
|
||||
bool shader(Assets* assets,
|
||||
const std::filesystem::path& filename,
|
||||
const std::string& name);
|
||||
bool atlas(Assets* assets,
|
||||
const std::filesystem::path& directory,
|
||||
const std::string& name);
|
||||
bool font(Assets* assets,
|
||||
const std::filesystem::path& filename,
|
||||
const std::string& name);
|
||||
}
|
||||
|
||||
#endif // ASSETS_ASSET_LOADERS_H_
|
||||
Loading…
x
Reference in New Issue
Block a user