Add the frame width/height to SpriteTexture. Closes #13

master
Zed A. Shaw 2 days ago
parent ea92dcc3c4
commit 19682fd0bc
  1. 2
      Makefile
  2. 34
      tests/textures.cpp
  3. 14
      textures.cpp
  4. 1
      textures.hpp

@ -57,7 +57,7 @@ clean:
meson compile --clean -C builddir meson compile --clean -C builddir
debug_test: build 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: win_installer:
powershell 'start "C:\Program Files (x86)\solicus\InstallForge\bin\ifbuilderenvx86.exe" scripts\win_installer.ifp' powershell 'start "C:\Program Files (x86)\solicus\InstallForge\bin\ifbuilderenvx86.exe" scripts\win_installer.ifp'

@ -2,26 +2,42 @@
#include <fmt/core.h> #include <fmt/core.h>
#include <string> #include <string>
#include "textures.hpp" #include "textures.hpp"
#include "levelmanager.hpp" #include "constants.hpp"
#include "components.hpp"
using namespace fmt; using namespace fmt;
TEST_CASE("test texture management", "[textures]") { TEST_CASE("test texture management", "[textures]") {
components::init(); components::init();
textures::init(); textures::init();
auto spider = textures::get("hairy_spider"); auto spider = textures::get("hairy_spider");
REQUIRE(spider.sprite != nullptr); REQUIRE(spider.sprite != nullptr);
REQUIRE(spider.texture != 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 image = textures::load_image("assets/sprites/hairy_spider.png");
auto img_ptr = textures::get_surface(0); size_t floor_tile = textures::get_id("floor_tile");
REQUIRE(img_ptr != nullptr); 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; auto ceiling = textures::get_ceiling(floor_tile);
GameLevel level = levels.current(); REQUIRE(ceiling != nullptr);
auto& tiles = level.map->tiles();
auto& walls = level.map->walls();
REQUIRE(matrix::width(tiles) == matrix::width(walls));
REQUIRE(matrix::height(tiles) == matrix::height(walls));
} }

@ -23,9 +23,14 @@ namespace textures {
int width = settings["frame_width"]; int width = settings["frame_width"];
int height = settings["frame_height"]; 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(); auto &config = el.value();
const std::string& texture_fname = config["texture"]; const std::string& texture_fname = config["texture"];
size_t surface_i = config["id"]; 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); TMGR.name_to_id.insert_or_assign(el.key(), surface_i);
if(surface_i >= tiles.size()) { if(surface_i >= tiles.size()) {

@ -12,6 +12,7 @@ namespace textures {
struct SpriteTexture { struct SpriteTexture {
std::shared_ptr<sf::Sprite> sprite = nullptr; std::shared_ptr<sf::Sprite> sprite = nullptr;
std::shared_ptr<sf::Texture> texture = nullptr; std::shared_ptr<sf::Texture> texture = nullptr;
sf::Vector2i frame_size;
}; };
struct TextureManager { struct TextureManager {

Loading…
Cancel
Save