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
+16
View File
@@ -2,6 +2,7 @@
#include "../graphics/Texture.h"
#include "../graphics/Shader.h"
#include "../graphics/Atlas.h"
#include "../graphics/Font.h"
Assets::~Assets() {
@@ -16,6 +17,10 @@ Assets::~Assets() {
for (auto& iter : fonts){
delete iter.second;
}
for (auto& iter : atlases) {
delete iter.second;
}
}
Texture* Assets::getTexture(std::string name) const {
@@ -52,3 +57,14 @@ Font* Assets::getFont(std::string name) const {
void Assets::store(Font* font, std::string name){
fonts[name] = font;
}
Atlas* Assets::getAtlas(std::string name) const {
auto found = atlases.find(name);
if (found == atlases.end())
return nullptr;
return found->second;
}
void Assets::store(Atlas* atlas, std::string name){
atlases[name] = atlas;
}
+5
View File
@@ -7,11 +7,13 @@
class Texture;
class Shader;
class Font;
class Atlas;
class Assets {
std::unordered_map<std::string, Texture*> textures;
std::unordered_map<std::string, Shader*> shaders;
std::unordered_map<std::string, Font*> fonts;
std::unordered_map<std::string, Atlas*> atlases;
public:
~Assets();
Texture* getTexture(std::string name) const;
@@ -22,6 +24,9 @@ public:
Font* getFont(std::string name) const;
void store(Font* font, std::string name);
Atlas* getAtlas(std::string name) const;
void store(Atlas* atlas, std::string name);
};
#endif /* ASSETS_ASSETS_H_ */
+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");