diff --git a/Makefile b/Makefile index 3723a40..77b1ba6 100644 --- a/Makefile +++ b/Makefile @@ -57,7 +57,7 @@ clean: meson compile --clean -C builddir debug_test: build - gdb --nx -x .gdbinit --ex run --args builddir/runtests -e "[matrix:viewport]" + gdb --nx -x .gdbinit --ex run --args builddir/runtests -e win_installer: powershell 'start "C:\Program Files (x86)\solicus\InstallForge\bin\ifbuilderenvx86.exe" scripts\win_installer.ifp' diff --git a/tests/textures.cpp b/tests/textures.cpp index 68391a4..00efb5e 100644 --- a/tests/textures.cpp +++ b/tests/textures.cpp @@ -2,26 +2,42 @@ #include #include #include "textures.hpp" -#include "levelmanager.hpp" +#include "constants.hpp" +#include "components.hpp" using namespace fmt; TEST_CASE("test texture management", "[textures]") { components::init(); textures::init(); + auto spider = textures::get("hairy_spider"); REQUIRE(spider.sprite != nullptr); REQUIRE(spider.texture != nullptr); + REQUIRE(spider.frame_size.x == TEXTURE_WIDTH); + REQUIRE(spider.frame_size.y == TEXTURE_HEIGHT); auto image = textures::load_image("assets/sprites/hairy_spider.png"); - auto img_ptr = textures::get_surface(0); - REQUIRE(img_ptr != nullptr); + size_t floor_tile = textures::get_id("floor_tile"); + size_t gray_stone = textures::get_id("gray_stone_floor_light"); + + auto floor_ptr = textures::get_surface(floor_tile); + REQUIRE(floor_ptr != nullptr); + + auto gray_stone_ptr = textures::get_surface(gray_stone); + REQUIRE(gray_stone_ptr != nullptr); + + auto& light = textures::get_ambient_light(); + REQUIRE(light.size() > 0); + REQUIRE(light[floor_tile] == 0); + REQUIRE(light[gray_stone] > 0); + + auto& tiles = textures::get_map_tile_set(); + REQUIRE(tiles.size() > 0); + REQUIRE(tiles[floor_tile] > 0); + REQUIRE(tiles[gray_stone] > 0); - LevelManager levels; - GameLevel level = levels.current(); - auto& tiles = level.map->tiles(); - auto& walls = level.map->walls(); - REQUIRE(matrix::width(tiles) == matrix::width(walls)); - REQUIRE(matrix::height(tiles) == matrix::height(walls)); + auto ceiling = textures::get_ceiling(floor_tile); + REQUIRE(ceiling != nullptr); } diff --git a/textures.cpp b/textures.cpp index aa297d0..479245a 100644 --- a/textures.cpp +++ b/textures.cpp @@ -23,9 +23,14 @@ namespace textures { int width = settings["frame_width"]; int height = settings["frame_height"]; - sprite->setTextureRect({{0,0}, {width, height}}); + dbc::check(width % 2 == 0, + fmt::format("sprite {} has invalid frame size", name)); - TMGR.sprite_textures.try_emplace(name, sprite, texture); + sf::Vector2i frame_size{width, height}; + + sprite->setTextureRect({{0,0}, frame_size}); + + TMGR.sprite_textures.try_emplace(name, sprite, texture, frame_size); } } @@ -46,6 +51,11 @@ namespace textures { auto &config = el.value(); const std::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())); + TMGR.name_to_id.insert_or_assign(el.key(), surface_i); if(surface_i >= tiles.size()) { diff --git a/textures.hpp b/textures.hpp index 39cc456..5ec9e3f 100644 --- a/textures.hpp +++ b/textures.hpp @@ -12,6 +12,7 @@ namespace textures { struct SpriteTexture { std::shared_ptr sprite = nullptr; std::shared_ptr texture = nullptr; + sf::Vector2i frame_size; }; struct TextureManager {