diff --git a/assets/config.json b/assets/config.json index 15fa3dc..b4ff6c5 100644 --- a/assets/config.json +++ b/assets/config.json @@ -80,11 +80,6 @@ "frame_width": 256, "frame_height": 256 }, - "torch_horizontal_floor_ICON": - {"path": "assets/icons/torch_crappy_ICON.png", - "frame_width": 128, - "frame_height": 128 - }, "peasant_girl": {"path": "assets/sprites/peasant_girl_2.png", "frame_width": 256, @@ -100,11 +95,6 @@ "frame_width": 256, "frame_height": 256 }, - "healing_potion_small_ICON": - {"path": "assets/icons/healing_potion_small_ICON.png", - "frame_width": 128, - "frame_height": 128 - }, "well_down": {"path": "assets/sprites/well_down.png", "frame_width": 256, diff --git a/assets/icons.json b/assets/icons.json new file mode 100644 index 0000000..904cd97 --- /dev/null +++ b/assets/icons.json @@ -0,0 +1,12 @@ +{ + "healing_potion_small": + {"path": "assets/icons/healing_potion_small.png", + "frame_width": 96, + "frame_height": 96 + }, + "torch_horizontal_floor": + {"path": "assets/icons/torch_horizontal_floor.png", + "frame_width": 96, + "frame_height": 96 + } +} diff --git a/assets/icons/healing_potion_small.png b/assets/icons/healing_potion_small.png new file mode 100644 index 0000000..491579a Binary files /dev/null and b/assets/icons/healing_potion_small.png differ diff --git a/assets/icons/healing_potion_small_ICON.png b/assets/icons/healing_potion_small_ICON.png deleted file mode 100644 index bc790e4..0000000 Binary files a/assets/icons/healing_potion_small_ICON.png and /dev/null differ diff --git a/assets/icons/torch_crappy_ICON.png b/assets/icons/torch_crappy_ICON.png deleted file mode 100644 index ccc6480..0000000 Binary files a/assets/icons/torch_crappy_ICON.png and /dev/null differ diff --git a/assets/icons/torch_horizontal_floor.png b/assets/icons/torch_horizontal_floor.png new file mode 100644 index 0000000..2382d7f Binary files /dev/null and b/assets/icons/torch_horizontal_floor.png differ diff --git a/backend.cpp b/backend.cpp index 5bb4f25..ef0e8e3 100644 --- a/backend.cpp +++ b/backend.cpp @@ -8,9 +8,14 @@ namespace sfml { using namespace nlohmann; - guecs::SpriteTexture Backend::texture_get(const string& name) { - auto sp = textures::get(name); - return {sp.sprite, sp.texture}; + guecs::SpriteTexture Backend::get_sprite(const string& name) { + auto sp = textures::get_sprite(name); + return {sp.sprite, sp.texture, sp.frame_size}; + } + + guecs::SpriteTexture Backend::get_icon(const string& name) { + auto sp = textures::get_icon(name); + return {sp.sprite, sp.texture, sp.frame_size}; } Backend::Backend() { @@ -27,7 +32,7 @@ namespace sfml { sound::stop(name); } - std::shared_ptr Backend::shader_get(const std::string& name) { + std::shared_ptr Backend::get_shader(const std::string& name) { return shaders::get(name); } diff --git a/backend.hpp b/backend.hpp index fed18f8..fc36f35 100644 --- a/backend.hpp +++ b/backend.hpp @@ -9,10 +9,11 @@ namespace sfml { public: Backend(); - guecs::SpriteTexture texture_get(const string& name); + guecs::SpriteTexture get_sprite(const string& name); + guecs::SpriteTexture get_icon(const string& name); void sound_play(const string& name); void sound_stop(const string& name); - std::shared_ptr shader_get(const std::string& name); + std::shared_ptr get_shader(const std::string& name); bool shader_updated(); guecs::Theme theme(); }; diff --git a/gui/boss_fight_ui.cpp b/gui/boss_fight_ui.cpp index 524f9c4..269286b 100644 --- a/gui/boss_fight_ui.cpp +++ b/gui/boss_fight_ui.cpp @@ -35,7 +35,7 @@ namespace gui { $animation = $world->get($boss_id); $animation.frame_width = $sprite_config.width; - $boss_image = textures::get($sprite_config.name); + $boss_image = textures::get_sprite($sprite_config.name); sf::IntRect frame_rect{{0,0},{$sprite_config.width,$sprite_config.height}}; $boss_image.sprite->setTextureRect(frame_rect); $boss_image.sprite->setScale({$sprite_config.scale, $sprite_config.scale}); @@ -52,13 +52,13 @@ namespace gui { void BossFightUI::configure_background() { auto& boss = $world->get($boss_id); - $boss_background = textures::get(boss.background); + $boss_background = textures::get_sprite(boss.background); $boss_background.sprite->setPosition({BOSS_VIEW_X, BOSS_VIEW_Y}); $status.set($status.MAIN, {$status.$parser}); if(boss.stage) { $boss_has_stage = true; - $boss_stage = textures::get(*boss.stage); + $boss_stage = textures::get_sprite(*boss.stage); $boss_stage.sprite->setPosition({BOSS_VIEW_X, BOSS_VIEW_Y}); } } diff --git a/gui/fsm.cpp b/gui/fsm.cpp index 24a9fe5..3eb06cd 100644 --- a/gui/fsm.cpp +++ b/gui/fsm.cpp @@ -347,7 +347,6 @@ namespace gui { if(!sound::playing("ambient_1")) sound::play("ambient_1", true); $debug_ui.debug(); shaders::reload(); - $map_ui.save_map($main_ui.$compass_dir); break; case KEY::O: autowalking = true; diff --git a/gui/guecstra.cpp b/gui/guecstra.cpp index 7e7ecb8..e028f31 100644 --- a/gui/guecstra.cpp +++ b/gui/guecstra.cpp @@ -21,11 +21,9 @@ namespace guecs { } void GrabSource::setSprite(guecs::UI& gui, guecs::Entity gui_id) { - dbc::check(gui.has(gui_id), "GrabSource given sprite gui_id that doesn't exist"); + dbc::check(gui.has(gui_id), "GrabSource given sprite gui_id that doesn't exist"); - fmt::println("> Grabsource Set sprite entity {}", world_entity); - - auto& sp = gui.get(gui_id); + auto& sp = gui.get(gui_id); sprite = sp.sprite; } diff --git a/gui/loot_ui.cpp b/gui/loot_ui.cpp index 370a0d1..f5f91e2 100644 --- a/gui/loot_ui.cpp +++ b/gui/loot_ui.cpp @@ -75,7 +75,7 @@ namespace gui { dbc::check($level.world->has(item), "item in inventory UI doesn't exist in world. New level?"); auto& sprite = $level.world->get(item); - $gui.set_init(id, {fmt::format("{}_ICON", sprite.name)}); + $gui.set_init(id, {sprite.name}); guecs::GrabSource grabber{ item, [&, id]() { return remove_slot(id); }}; @@ -84,7 +84,7 @@ namespace gui { } else { // BUG: fix remove so it's safe to call on empty if($gui.has(id)) { - $gui.remove(id); + $gui.remove(id); $gui.remove(id); } diff --git a/gui/map_view.cpp b/gui/map_view.cpp index fcac5e6..e1d2edc 100644 --- a/gui/map_view.cpp +++ b/gui/map_view.cpp @@ -49,15 +49,6 @@ namespace gui { $gui.init(); } - void MapViewUI::save_map(int compass_dir) { - (void)compass_dir; - // confirm we get two different maps - auto out_img = $map_render->getTexture().copyToImage(); - bool worked = out_img.saveToFile("tmp/map_render.png"); - dbc::check(worked, "failed to render map"); - } - - void MapViewUI::render(sf::RenderWindow &window, int compass_dir) { $gui.render(window); System::draw_map($level, $map_tiles, $entity_map); diff --git a/gui/map_view.hpp b/gui/map_view.hpp index b5d9836..3b277c7 100644 --- a/gui/map_view.hpp +++ b/gui/map_view.hpp @@ -24,6 +24,5 @@ namespace gui { void update_level(GameLevel &level); void log(std::wstring msg); void update(); - void save_map(int compass_dir); }; } diff --git a/gui/ritual_ui.cpp b/gui/ritual_ui.cpp index a18d906..a248efc 100644 --- a/gui/ritual_ui.cpp +++ b/gui/ritual_ui.cpp @@ -44,7 +44,7 @@ namespace gui { } void UI::START(Event) { - $ritual_ui = textures::get("ritual_crafting_area"); + $ritual_ui = textures::get_sprite("ritual_crafting_area"); $ritual_ui.sprite->setPosition($gui.get_position()); $ritual_ui.sprite->setTextureRect($ritual_closed_rect); $ritual_anim = animation::load("ritual_blanket"); diff --git a/gui/status_ui.cpp b/gui/status_ui.cpp index 14862f5..011d2f2 100644 --- a/gui/status_ui.cpp +++ b/gui/status_ui.cpp @@ -80,7 +80,7 @@ namespace gui { auto gui_id = $gui.entity(slot); auto& sprite = $level.world->get(world_entity); - $gui.set_init(gui_id, {fmt::format("{}_ICON", sprite.name)}); + $gui.set_init(gui_id, {sprite.name}); guecs::GrabSource grabber{ world_entity, [&, gui_id]() { return remove_slot(gui_id); }}; grabber.setSprite($gui, gui_id); @@ -136,7 +136,7 @@ namespace gui { inventory.remove(world_entity); $gui.remove(slot_id); - $gui.remove(slot_id); + $gui.remove(slot_id); } void StatusUI::swap(guecs::Entity gui_a, guecs::Entity gui_b) { diff --git a/raycaster.cpp b/raycaster.cpp index 44e973c..03eb69b 100644 --- a/raycaster.cpp +++ b/raycaster.cpp @@ -423,7 +423,7 @@ void Raycaster::draw(sf::RenderTarget& target) { } void Raycaster::update_sprite(DinkyECS::Entity ent, components::Sprite& sprite) { - auto sprite_txt = textures::get(sprite.name); + auto sprite_txt = textures::get_sprite(sprite.name); $sprites.insert_or_assign(ent, sprite_txt); } diff --git a/tests/animation.cpp b/tests/animation.cpp index d3fc991..3005f9a 100644 --- a/tests/animation.cpp +++ b/tests/animation.cpp @@ -42,7 +42,7 @@ TEST_CASE("animation utility API", "[animation]") { textures::init(); animation::init(); - auto blanket = textures::get("ritual_crafting_area"); + auto blanket = textures::get_sprite("ritual_crafting_area"); auto anim = animation::load("ritual_blanket"); anim.play(); diff --git a/tests/textures.cpp b/tests/textures.cpp index 00efb5e..99de020 100644 --- a/tests/textures.cpp +++ b/tests/textures.cpp @@ -11,7 +11,7 @@ TEST_CASE("test texture management", "[textures]") { components::init(); textures::init(); - auto spider = textures::get("hairy_spider"); + auto spider = textures::get_sprite("hairy_spider"); REQUIRE(spider.sprite != nullptr); REQUIRE(spider.texture != nullptr); REQUIRE(spider.frame_size.x == TEXTURE_WIDTH); diff --git a/textures.cpp b/textures.cpp index 7d2c497..7aac290 100644 --- a/textures.cpp +++ b/textures.cpp @@ -7,33 +7,43 @@ #include 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(settings["path"]); - texture->setSmooth(assets["graphics"]["smooth_textures"]); + texture->setSmooth(smooth); auto sprite = make_shared(*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); diff --git a/textures.hpp b/textures.hpp index 2d24178..cb7d998 100644 --- a/textures.hpp +++ b/textures.hpp @@ -15,12 +15,15 @@ namespace textures { sf::Vector2i frame_size; }; + using SpriteTextureMap = std::unordered_map; + struct TextureManager { std::vector surfaces; std::vector ceilings; std::vector map_tile_set; std::vector ambient_light; - std::unordered_map sprite_textures; + SpriteTextureMap sprite_textures; + SpriteTextureMap icon_textures; std::unordered_map name_to_id; std::unordered_map map_sprites; sf::Texture map_sprite_sheet{"./assets/map_tiles.png"}; @@ -28,7 +31,8 @@ namespace textures { void init(); - SpriteTexture get(const std::string& name); + SpriteTexture get_sprite(const std::string& name); + SpriteTexture get_icon(const std::string& name); sf::Image load_image(const std::string& filename); diff --git a/tools/fragviewer.cpp b/tools/fragviewer.cpp index 40ee33c..fae1ea4 100644 --- a/tools/fragviewer.cpp +++ b/tools/fragviewer.cpp @@ -78,7 +78,7 @@ int main(int argc, char *argv[]) { window.setVerticalSyncEnabled(true); if(load_sprite) { - sprite_texture = textures::get(sprite_name); + sprite_texture = textures::get_sprite(sprite_name); sprite_texture.sprite->setPosition({0,0}); auto bounds = sprite_texture.sprite->getLocalBounds(); sf::Vector2f scale{u_resolution.x / bounds.size.x,