Additional check + refactor

This commit is contained in:
MihailRis
2023-12-18 18:36:11 +03:00
parent 3c89b9c083
commit 13b204f28d
+33 -28
View File
@@ -12,17 +12,13 @@
#include "../graphics/Atlas.h" #include "../graphics/Atlas.h"
#include "../graphics/Font.h" #include "../graphics/Font.h"
using std::string;
using std::vector;
using std::unique_ptr;
using std::filesystem::path;
namespace fs = std::filesystem; namespace fs = std::filesystem;
bool assetload::texture(Assets* assets, bool assetload::texture(Assets* assets,
const ResPaths* paths, const ResPaths* paths,
const string filename, const std::string filename,
const string name) { const std::string name) {
Texture* texture = png::load_texture(paths->find(filename).string()); Texture* texture = png::load_texture(paths->find(filename).string());
if (texture == nullptr) { if (texture == nullptr) {
std::cerr << "failed to load texture '" << name << "'" << std::endl; std::cerr << "failed to load texture '" << name << "'" << std::endl;
@@ -34,16 +30,19 @@ bool assetload::texture(Assets* assets,
bool assetload::shader(Assets* assets, bool assetload::shader(Assets* assets,
const ResPaths* paths, const ResPaths* paths,
const string filename, const std::string filename,
const string name) { const std::string name) {
path vertexFile = paths->find(filename+".glslv"); fs::path vertexFile = paths->find(filename+".glslv");
path fragmentFile = paths->find(filename+".glslf"); fs::path fragmentFile = paths->find(filename+".glslf");
string vertexSource = files::read_string(vertexFile); std::string vertexSource = files::read_string(vertexFile);
string fragmentSource = files::read_string(fragmentFile); std::string fragmentSource = files::read_string(fragmentFile);
Shader* shader = Shader::loadShader(vertexFile.string(), fragmentFile.string(), Shader* shader = Shader::loadShader(
vertexSource, fragmentSource); vertexFile.string(),
fragmentFile.string(),
vertexSource, fragmentSource);
if (shader == nullptr) { if (shader == nullptr) {
std::cerr << "failed to load shader '" << name << "'" << std::endl; std::cerr << "failed to load shader '" << name << "'" << std::endl;
return false; return false;
@@ -54,19 +53,25 @@ bool assetload::shader(Assets* assets,
bool assetload::atlas(Assets* assets, bool assetload::atlas(Assets* assets,
const ResPaths* paths, const ResPaths* paths,
const string directory, const std::string directory,
const string name) { const std::string name) {
AtlasBuilder builder; AtlasBuilder builder;
for (const auto& file : paths->listdir(directory)) { for (const auto& file : paths->listdir(directory)) {
if (file.extension() == ".png") { // png is only supported format
string name = file.stem().string(); if (file.extension() != ".png")
if (builder.has(name)) { continue;
continue; // skip duplicates std::string name = file.stem().string();
} // skip duplicates
std::unique_ptr<ImageData> image (png::load_image(file.string()));//but what if load_image return nullptr? if (builder.has(name)) {
image->fixAlphaColor(); continue;
builder.add(name, image.release());
} }
std::unique_ptr<ImageData> image (png::load_image(file.string()));
if (image == nullptr) {
std::cerr << "could not to load " << file.string() << std::endl;
continue;
}
image->fixAlphaColor();
builder.add(name, image.release());
} }
Atlas* atlas = builder.build(2); Atlas* atlas = builder.build(2);
assets->store(atlas, name); assets->store(atlas, name);
@@ -75,11 +80,11 @@ bool assetload::atlas(Assets* assets,
bool assetload::font(Assets* assets, bool assetload::font(Assets* assets,
const ResPaths* paths, const ResPaths* paths,
const string filename, const std::string filename,
const string name) { const std::string name) {
vector<Texture*> pages; std::vector<Texture*> pages;
for (size_t i = 0; i <= 4; i++) { for (size_t i = 0; i <= 4; i++) {
string name = filename + "_" + std::to_string(i) + ".png"; std::string name = filename + "_" + std::to_string(i) + ".png";
name = paths->find(name).string(); name = paths->find(name).string();
Texture* texture = png::load_texture(name); Texture* texture = png::load_texture(name);
if (texture == nullptr) { if (texture == nullptr) {