Blocks atlas is auto-assembling now

This commit is contained in:
MihailRis
2023-11-22 01:53:48 +03:00
parent ea5c117652
commit fb8d220cca
41 changed files with 622 additions and 75 deletions
+15 -14
View File
@@ -2,6 +2,7 @@
#include "Assets.h"
#include <iostream>
#include <filesystem>
#include <memory>
#include "../constants.h"
@@ -42,6 +43,7 @@ bool AssetsLoader::loadNext() {
#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 std::string& filename, const std::string& name) {
@@ -65,18 +67,19 @@ bool _load_texture(Assets* assets, const std::string& filename, const std::strin
}
bool _load_atlas(Assets* assets, const std::string& filename, const std::string& name) {
unique_ptr<ImageData> image (png::load_image(filename));
if (image == nullptr) {
std::cerr << "failed to load image '" << name << "'" << std::endl;
return false;
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->flipY();
image->fixAlphaColor();
builder.add(name, image.release());
}
}
for (int i = 0; i < ATLAS_MARGIN_SIZE; i++) {
ImageData* newimage = add_atlas_margins(image.get(), 16);
image.reset(newimage);
}
Texture* texture = Texture::from(image.get());
assets->store(texture, name);
Atlas* atlas = builder.build(2);
assets->store(atlas, name);
return true;
}
@@ -107,9 +110,7 @@ void AssetsLoader::addDefaults(AssetsLoader& loader) {
loader.add(ASSET_SHADER, SHADERS_FOLDER"/lines", "lines");
loader.add(ASSET_SHADER, SHADERS_FOLDER"/ui", "ui");
loader.add(ASSET_ATLAS, TEXTURES_FOLDER"/block.png", "block");
loader.add(ASSET_TEXTURE, TEXTURES_FOLDER"/block.png", "block_tex");
loader.add(ASSET_TEXTURE, TEXTURES_FOLDER"/slot.png", "slot");
loader.add(ASSET_ATLAS, TEXTURES_FOLDER"/blocks", "blocks");
loader.add(ASSET_TEXTURE, TEXTURES_FOLDER"/menubg.png", "menubg");
loader.add(ASSET_FONT, FONTS_FOLDER"/font", "normal");