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

This commit is contained in:
Zed A. Shaw 2025-07-09 13:34:18 -04:00
parent 2c011079a8
commit b2d0b0ee4c
4 changed files with 34 additions and 16 deletions

View file

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