content-pack resources support

This commit is contained in:
MihailRis
2023-12-14 02:30:39 +03:00
parent c6d2266026
commit 396ee31bcc
43 changed files with 170 additions and 90 deletions
+16 -16
View File
@@ -7,19 +7,20 @@
#include <memory>
#include "../constants.h"
#include "../files/engine_paths.h"
using std::filesystem::path;
using std::unique_ptr;
AssetsLoader::AssetsLoader(Assets* assets, path resdir)
: assets(assets), resdir(resdir) {
AssetsLoader::AssetsLoader(Assets* assets, const ResPaths* paths)
: assets(assets), paths(paths) {
}
void AssetsLoader::addLoader(int tag, aloader_func func) {
loaders[tag] = func;
}
void AssetsLoader::add(int tag, const path filename, const std::string alias) {
void AssetsLoader::add(int tag, const std::string filename, const std::string alias) {
entries.push(aloader_entry{ tag, filename, alias });
}
@@ -37,7 +38,7 @@ bool AssetsLoader::loadNext() {
return false;
}
aloader_func loader = found->second;
bool status = loader(assets, entry.filename, entry.alias);
bool status = loader(assets, paths, entry.filename, entry.alias);
entries.pop();
return status;
}
@@ -50,20 +51,19 @@ void AssetsLoader::createDefaults(AssetsLoader& loader) {
}
void AssetsLoader::addDefaults(AssetsLoader& loader) {
path resdir = loader.getDirectory();
loader.add(ASSET_SHADER, resdir/path(SHADERS_FOLDER"/main"), "main");
loader.add(ASSET_SHADER, resdir/path(SHADERS_FOLDER"/lines"), "lines");
loader.add(ASSET_SHADER, resdir/path(SHADERS_FOLDER"/ui"), "ui");
loader.add(ASSET_SHADER, resdir/path(SHADERS_FOLDER"/ui3d"), "ui3d");
loader.add(ASSET_SHADER, resdir/path(SHADERS_FOLDER"/background"), "background");
loader.add(ASSET_SHADER, resdir/path(SHADERS_FOLDER"/skybox_gen"), "skybox_gen");
loader.add(ASSET_SHADER, SHADERS_FOLDER"/main", "main");
loader.add(ASSET_SHADER, SHADERS_FOLDER"/lines", "lines");
loader.add(ASSET_SHADER, SHADERS_FOLDER"/ui", "ui");
loader.add(ASSET_SHADER, SHADERS_FOLDER"/ui3d", "ui3d");
loader.add(ASSET_SHADER, SHADERS_FOLDER"/background", "background");
loader.add(ASSET_SHADER, SHADERS_FOLDER"/skybox_gen", "skybox_gen");
loader.add(ASSET_ATLAS, resdir/path(TEXTURES_FOLDER"/blocks"), "blocks");
loader.add(ASSET_TEXTURE, resdir/path(TEXTURES_FOLDER"/menubg.png"), "menubg");
loader.add(ASSET_ATLAS, TEXTURES_FOLDER"/blocks", "blocks");
loader.add(ASSET_TEXTURE, TEXTURES_FOLDER"/menubg.png", "menubg");
loader.add(ASSET_FONT, resdir/path(FONTS_FOLDER"/font"), "normal");
loader.add(ASSET_FONT, FONTS_FOLDER"/font", "normal");
}
path AssetsLoader::getDirectory() const {
return resdir;
const ResPaths* AssetsLoader::getPaths() const {
return paths;
}
+7 -7
View File
@@ -3,7 +3,6 @@
#include <string>
#include <functional>
#include <filesystem>
#include <map>
#include <queue>
@@ -12,13 +11,14 @@ const short ASSET_SHADER = 2;
const short ASSET_FONT = 3;
const short ASSET_ATLAS = 4;
class ResPaths;
class Assets;
typedef std::function<bool(Assets*, const std::filesystem::path&, const std::string&)> aloader_func;
typedef std::function<bool(Assets*, const ResPaths*, const std::string&, const std::string&)> aloader_func;
struct aloader_entry {
int tag;
const std::filesystem::path filename;
const std::string filename;
const std::string alias;
};
@@ -26,11 +26,11 @@ class AssetsLoader {
Assets* assets;
std::map<int, aloader_func> loaders;
std::queue<aloader_entry> entries;
std::filesystem::path resdir;
const ResPaths* paths;
public:
AssetsLoader(Assets* assets, std::filesystem::path resdir);
AssetsLoader(Assets* assets, const ResPaths* paths);
void addLoader(int tag, aloader_func func);
void add(int tag, const std::filesystem::path filename, const std::string alias);
void add(int tag, const std::string filename, const std::string alias);
bool hasNext() const;
bool loadNext();
@@ -38,7 +38,7 @@ public:
static void createDefaults(AssetsLoader& loader);
static void addDefaults(AssetsLoader& loader);
std::filesystem::path getDirectory() const;
const ResPaths* getPaths() const;
};
#endif // ASSETS_ASSETS_LOADER_H
+22 -13
View File
@@ -1,8 +1,10 @@
#include "asset_loaders.h"
#include <iostream>
#include <filesystem>
#include "Assets.h"
#include "../files/files.h"
#include "../files/engine_paths.h"
#include "../coders/png.h"
#include "../graphics/Shader.h"
#include "../graphics/Texture.h"
@@ -18,9 +20,10 @@ using std::filesystem::path;
namespace fs = std::filesystem;
bool assetload::texture(Assets* assets,
const path filename,
const ResPaths* paths,
const string filename,
const string name) {
Texture* texture = png::load_texture(filename.string());
Texture* texture = png::load_texture(paths->find(filename).string());
if (texture == nullptr) {
std::cerr << "failed to load texture '" << name << "'" << std::endl;
return false;
@@ -30,10 +33,11 @@ bool assetload::texture(Assets* assets,
}
bool assetload::shader(Assets* assets,
const path filename,
const string name) {
path vertexFile = path(filename.string()+".glslv");
path fragmentFile = path(filename.string()+".glslf");
const ResPaths* paths,
const string filename,
const string name) {
path vertexFile = paths->find(filename+".glslv");
path fragmentFile = paths->find(filename+".glslf");
string vertexSource = files::read_string(vertexFile);
string fragmentSource = files::read_string(fragmentFile);
@@ -49,13 +53,16 @@ bool assetload::shader(Assets* assets,
}
bool assetload::atlas(Assets* assets,
const path directory,
const string name) {
const ResPaths* paths,
const string directory,
const string name) {
AtlasBuilder builder;
for (const auto& entry : fs::directory_iterator(directory)) {
path file = entry.path();
for (const auto& file : paths->listdir(directory)) {
if (file.extension() == ".png") {
string name = file.stem().string();
if (builder.has(name)) {
continue; // skip duplicates
}
std::unique_ptr<ImageData> image (png::load_image(file.string()));
image->fixAlphaColor();
builder.add(name, image.release());
@@ -67,11 +74,13 @@ bool assetload::atlas(Assets* assets,
}
bool assetload::font(Assets* assets,
const path filename,
const string name) {
const ResPaths* paths,
const string filename,
const string name) {
vector<Texture*> pages;
for (size_t i = 0; i <= 4; i++) {
string name = filename.string() + "_" + std::to_string(i) + ".png";
string name = filename + "_" + std::to_string(i) + ".png";
name = paths->find(name).string();
Texture* texture = png::load_texture(name);
if (texture == nullptr) {
std::cerr << "failed to load bitmap font '" << name;
+12 -8
View File
@@ -2,23 +2,27 @@
#define ASSETS_ASSET_LOADERS_H_
#include <string>
#include <filesystem>
class ResPaths;
class Assets;
namespace assetload {
bool texture(Assets* assets,
const std::filesystem::path filename,
const ResPaths* paths,
const std::string 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 ResPaths* paths,
const std::string filename,
const std::string name);
bool atlas(Assets* assets,
const ResPaths* paths,
const std::string directory,
const std::string name);
bool font(Assets* assets,
const std::filesystem::path filename,
const std::string name);
const ResPaths* paths,
const std::string filename,
const std::string name);
}
#endif // ASSETS_ASSET_LOADERS_H_