You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.8 KiB
62 lines
1.8 KiB
#include "guecs/sfml/textures.hpp"
|
|
#include "guecs/sfml/config.hpp"
|
|
#include <SFML/Graphics/Image.hpp>
|
|
#include <fmt/core.h>
|
|
#include <memory>
|
|
|
|
namespace textures {
|
|
using std::shared_ptr, std::make_shared;
|
|
|
|
static TextureManager TMGR;
|
|
static bool initialized = false;
|
|
|
|
void load_sprites() {
|
|
Config assets("assets/config.json");
|
|
|
|
for(auto& [name, settings] : assets["sprites"].items()) {
|
|
auto file_name = settings["path"];
|
|
auto file_path = Config::path_to(file_name);
|
|
auto texture = make_shared<sf::Texture>(file_path);
|
|
|
|
texture->setSmooth(assets["graphics"]["smooth_textures"]);
|
|
auto sprite = make_shared<sf::Sprite>(*texture);
|
|
|
|
int width = settings["frame_width"];
|
|
int height = settings["frame_height"];
|
|
|
|
sf::Vector2i frame_size{width, height};
|
|
sprite->setTextureRect({{0,0}, frame_size});
|
|
TMGR.sprite_textures.try_emplace(name, sprite, texture, frame_size);
|
|
}
|
|
}
|
|
|
|
void init() {
|
|
if(!initialized) {
|
|
load_sprites();
|
|
initialized = true;
|
|
}
|
|
}
|
|
|
|
SpriteTexture get(const std::string& name) {
|
|
assert(initialized && "you forgot to call textures::init()");
|
|
assert(TMGR.sprite_textures.contains(name) &&
|
|
"!!!!! texture pack does not contain sprite");
|
|
|
|
auto result = TMGR.sprite_textures.at(name);
|
|
|
|
assert(result.sprite != nullptr &&
|
|
"bad sprite from textures::get named can't be null");
|
|
assert(result.texture != nullptr &&
|
|
"bad texture from textures::get named can't be null");
|
|
|
|
return result;
|
|
}
|
|
|
|
sf::Image load_image(const std::string& filename) {
|
|
sf::Image texture;
|
|
auto file_path = Config::path_to(filename);
|
|
bool good = texture.loadFromFile(file_path);
|
|
assert(good && "failed to load image file");
|
|
return texture;
|
|
}
|
|
};
|
|
|