Map tiles are now correctly sized and positioned. Errors from before were due to floating point being used for positioning.

master
Zed A. Shaw 2 days ago
parent 2c011079a8
commit b2d0b0ee4c
  1. 5
      Makefile
  2. BIN
      assets/map_tiles.png
  3. 17
      tests/map.cpp
  4. 28
      tools/icongen.cpp

@ -21,6 +21,9 @@ endif
build: build:
meson compile -j 10 -C $(ROOT_DIR)/builddir meson compile -j 10 -C $(ROOT_DIR)/builddir
asset_build: build
./builddir/icongen
release_build: release_build:
meson --wipe builddir -Db_ndebug=true --buildtype release meson --wipe builddir -Db_ndebug=true --buildtype release
meson compile -j 10 -C builddir meson compile -j 10 -C builddir
@ -33,7 +36,7 @@ tracy_build:
meson setup --wipe builddir --buildtype debugoptimized -Dtracy_enable=true -Dtracy:on_demand=true meson setup --wipe builddir --buildtype debugoptimized -Dtracy_enable=true -Dtracy:on_demand=true
meson compile -j 10 -C builddir meson compile -j 10 -C builddir
test: build test: asset_build build
./builddir/runtests "[map-sprite]" ./builddir/runtests "[map-sprite]"
run: build test run: build test

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.5 KiB

After

Width:  |  Height:  |  Size: 5.6 KiB

@ -99,6 +99,8 @@ TEST_CASE("map image test", "[map-sprite]") {
for(auto tile : tiles) { for(auto tile : tiles) {
sf::Vector2i coords{tile["x"], tile["y"]}; sf::Vector2i coords{tile["x"], tile["y"]};
REQUIRE(coords.x % size.x == 0);
REQUIRE(coords.y % size.y == 0);
sprite_coord.insert_or_assign(tile["display"], coords); sprite_coord.insert_or_assign(tile["display"], coords);
} }
@ -117,13 +119,24 @@ TEST_CASE("map image test", "[map-sprite]") {
REQUIRE(sprite_coord.contains(display)); REQUIRE(sprite_coord.contains(display));
auto coords = sprite_coord.at(display); auto coords = sprite_coord.at(display);
sf::IntRect square{coords, size}; sf::IntRect square{coords, {size.x, size.y}};
sf::Sprite sprite{map_sprites, square}; sf::Sprite sprite{map_sprites, square};
sprite.setColor({150,150,150,255}); sprite.setColor({150,150,150,255});
sprite.setPosition({float(it.x) * float(size.x), float(it.y) * float(size.y)}); sprite.setPosition({float(it.x * size.x), float(it.y * size.y)});
render.draw(sprite); render.draw(sprite);
} }
level.world->query<components::Position, components::Tile>([&](auto, auto &pos, auto &entity_glyph) {
REQUIRE(sprite_coord.contains(entity_glyph.display));
auto coords = sprite_coord.at(entity_glyph.display);
sf::IntRect square{coords, {size.x, size.y}};
sf::Sprite sprite{map_sprites, square};
sprite.setColor({255,150,150,255});
sprite.setPosition({float(pos.location.x * size.x), float(pos.location.y * size.y)});
render.draw(sprite);
});
render.display(); render.display();
sf::Image out_img = render.getTexture().copyToImage(); sf::Image out_img = render.getTexture().copyToImage();
bool worked = out_img.saveToFile("map_test.png"); bool worked = out_img.saveToFile("map_test.png");

@ -33,26 +33,28 @@ struct MapTileBuilder {
sf::Glyph $glyph; sf::Glyph $glyph;
sf::Font $font{FONT_FILE_NAME}; sf::Font $font{FONT_FILE_NAME};
std::shared_ptr<sf::RenderTexture> $render = nullptr; std::shared_ptr<sf::RenderTexture> $render = nullptr;
sf::Vector2u $size; sf::Vector2i $size;
sf::Vector2u $image_size; sf::Vector2i $image_size;
sf::RenderTexture $temp_render; sf::RenderTexture $temp_render;
MapTileBuilder(size_t x, size_t y) : MapTileBuilder(size_t x, size_t y) :
$size(x, y), $size(x, y),
$image_size($size.x * TILE_COUNT, $size.y * TILE_COUNT), $image_size($size.x * TILE_COUNT, $size.y * TILE_COUNT),
$temp_render($size) $temp_render({(unsigned int)$size.x, (unsigned int)$size.y})
{ {
$font.setSmooth(false); $font.setSmooth(false);
} }
void best_size(wchar_t for_char) { void best_size(wchar_t for_char, bool centered) {
float factor = centered ? 0.8f : 1.0f;
sf::Vector2i adjusted_size = {int($size.x * factor), int($size.y * factor)};
$font_size = 20; // reset the size $font_size = 20; // reset the size
// fit the glyph in our box height // fit the glyph in our box height
auto temp = $font.getGlyph(for_char, $font_size, false); auto temp = $font.getGlyph(for_char, $font_size, false);
auto temp_size = $font_size; auto temp_size = $font_size;
while(temp.textureRect.size.y < int($size.y) while(temp.textureRect.size.y <= adjusted_size.y
&& temp.textureRect.size.x < int($size.x)) && temp.textureRect.size.x <= adjusted_size.x)
{ {
$glyph = temp; $glyph = temp;
$font_size = temp_size; $font_size = temp_size;
@ -79,6 +81,7 @@ struct MapTileBuilder {
void run(MapConfig& config) { void run(MapConfig& config) {
sf::Vector2u crop{$size.x * (unsigned int)config.it.width, $size.y * (unsigned int)config.it.y}; sf::Vector2u crop{$size.x * (unsigned int)config.it.width, $size.y * (unsigned int)config.it.y};
$render = std::make_shared<sf::RenderTexture>(crop); $render = std::make_shared<sf::RenderTexture>(crop);
$render->clear({0,0,0,0});
$render->setSmooth(false); $render->setSmooth(false);
sf::Vector2f cell_pos{0.0f,0.0f}; sf::Vector2f cell_pos{0.0f,0.0f};
@ -93,7 +96,7 @@ struct MapTileBuilder {
wchar_t display_char = config.map[it.y][it.x]; wchar_t display_char = config.map[it.y][it.x];
std::wstring content{display_char}; std::wstring content{display_char};
best_size(display_char); best_size(display_char, is_centered);
sf::Text icon{$font, content, $font_size}; sf::Text icon{$font, content, $font_size};
icon.setFillColor({0, 0, 0, 255}); icon.setFillColor({0, 0, 0, 255});
@ -105,16 +108,15 @@ struct MapTileBuilder {
sf::Sprite sprite{font_texture, $glyph.textureRect}; sf::Sprite sprite{font_texture, $glyph.textureRect};
auto t_size = $glyph.textureRect.size; auto t_size = $glyph.textureRect.size;
dbc::check(int($size.x - t_size.x) > 0, "font too big on x"); dbc::check($size.x - t_size.x >= 0, "font too big on x");
dbc::check(int($size.y - t_size.y) > 0, "font too big on y"); dbc::check($size.y - t_size.y >= 0, "font too big on y");
if(is_centered) { if(is_centered) {
sf::Vector2f center{ sf::Vector2f center{
(float($size.x) - float(t_size.x)) / 2.0f, float(($size.x - t_size.x) / 2),
(float($size.y) - float(t_size.y)) / 2.0f}; float(($size.y - t_size.y) / 2)};
sf::Vector2f scale{float($size.x) / float(t_size.x) * 0.8f, float($size.y) / float(t_size.y) * 0.8f}; sprite.setScale({1.0f, 1.0f});
sprite.setScale(scale);
sprite.setPosition({cell_pos.x + center.x, cell_pos.y + center.y}); sprite.setPosition({cell_pos.x + center.x, cell_pos.y + center.y});
} else { } else {
sf::Vector2f scale{float($size.x) / float(t_size.x), float($size.y) / float(t_size.y)}; sf::Vector2f scale{float($size.x) / float(t_size.x), float($size.y) / float(t_size.y)};

Loading…
Cancel
Save