Icons now work way better and don't have the the 'Rayview cuts icons' bug. It actually was a bug in the lel-guecs Sprite class that was using the TextureRect from the source sprite. Now its initialized with the framesize from the .json. This also uses the new guecs::Icon, but I have to fix that as it doesn't scale correctly. Closes #2.

This commit is contained in:
Zed A. Shaw 2025-07-22 15:03:37 -04:00
parent b311713064
commit ff7111b006
22 changed files with 81 additions and 64 deletions

View file

@ -7,33 +7,43 @@
#include <memory>
namespace textures {
using std::shared_ptr, std::make_shared;
using std::shared_ptr, std::make_shared, nlohmann::json, std::string;
static TextureManager TMGR;
static bool initialized = false;
void load_sprites() {
Config assets("assets/config.json");
for(auto& [name, settings] : assets["sprites"].items()) {
void load_sprite_textures(SpriteTextureMap &mapping, json &config, bool smooth) {
for(auto& [name, settings] : config.items()) {
auto texture = make_shared<sf::Texture>(settings["path"]);
texture->setSmooth(assets["graphics"]["smooth_textures"]);
texture->setSmooth(smooth);
auto sprite = make_shared<sf::Sprite>(*texture);
int width = settings["frame_width"];
int height = settings["frame_height"];
dbc::check(width % 2 == 0,
fmt::format("sprite {} has invalid frame size", name));
fmt::format("sprite {} has invalid frame size {}", name, width));
sf::Vector2i frame_size{width, height};
sprite->setTextureRect({{0,0}, frame_size});
TMGR.sprite_textures.try_emplace(name, sprite, texture, frame_size);
dbc::check(!mapping.contains(name),
fmt::format("duplicate sprite/icon name {}", (string)name));
mapping.try_emplace(name, sprite, texture, frame_size);
}
}
void load_sprites() {
Config sprites("assets/config.json");
bool smooth = sprites["graphics"]["smooth_textures"];
load_sprite_textures(TMGR.sprite_textures, sprites["sprites"], smooth);
Config icons("assets/icons.json");
load_sprite_textures(TMGR.icon_textures, icons.json(), smooth);
}
inline void resize_shit(size_t size) {
TMGR.surfaces.resize(size);
TMGR.ceilings.resize(size);
@ -49,12 +59,12 @@ namespace textures {
for(auto &el : tiles.items()) {
auto &config = el.value();
const std::string& texture_fname = config["texture"];
const string& texture_fname = config["texture"];
size_t surface_i = config["id"];
dbc::check(!TMGR.name_to_id.contains(el.key()),
fmt::format("duplicate key in textures {}",
(std::string)el.key()));
(string)el.key()));
TMGR.name_to_id.insert_or_assign(el.key(), surface_i);
@ -68,9 +78,9 @@ namespace textures {
// NOTE: ceilings defaults to 0 which is floor texture so only need to update
if(config.contains("ceiling")) {
const std::string& name = config["ceiling"];
const string& name = config["ceiling"];
dbc::check(tiles.contains(name), fmt::format("invalid ceiling name {} in tile config {}", name, (std::string)el.key()));
dbc::check(tiles.contains(name), fmt::format("invalid ceiling name {} in tile config {}", name, (string)el.key()));
auto& ceiling = tiles[name];
TMGR.ceilings[surface_i] = ceiling["id"];
@ -80,7 +90,7 @@ namespace textures {
void load_map_tiles() {
Config config("./assets/map_tiles.json");
nlohmann::json& tiles = config.json();
json& tiles = config.json();
for(auto tile : tiles) {
sf::Vector2i coords{tile["x"], tile["y"]};
@ -107,12 +117,12 @@ namespace textures {
}
}
SpriteTexture get(const std::string& name) {
SpriteTexture& get(const string& name, SpriteTextureMap& mapping) {
dbc::check(initialized, "you forgot to call textures::init()");
dbc::check(TMGR.sprite_textures.contains(name),
fmt::format("!!!!! texture pack does not contain {} sprite", name));
dbc::check(mapping.contains(name),
fmt::format("!!!!! textures do not contain {} sprite", name));
auto result = TMGR.sprite_textures.at(name);
auto& result = mapping.at(name);
dbc::check(result.sprite != nullptr,
fmt::format("bad sprite from textures::get named {}", name));
@ -122,7 +132,15 @@ namespace textures {
return result;
}
sf::Image load_image(const std::string& filename) {
SpriteTexture get_sprite(const string& name) {
return get(name, TMGR.sprite_textures);
}
SpriteTexture get_icon(const string& name) {
return get(name, TMGR.icon_textures);
}
sf::Image load_image(const string& filename) {
sf::Image texture;
bool good = texture.loadFromFile(filename);
dbc::check(good, fmt::format("failed to load {}", filename));
@ -150,7 +168,7 @@ namespace textures {
return (const uint32_t *)TMGR.surfaces[ceiling_num].getPixelsPtr();
}
size_t get_id(const std::string& name) {
size_t get_id(const string& name) {
dbc::check(TMGR.name_to_id.contains(name),
fmt::format("there is no texture named {} in tiles.json", name));
return TMGR.name_to_id.at(name);