Font sizes and map view now work with arbitrary map sizes.

This commit is contained in:
Zed A. Shaw 2024-11-09 10:56:22 -05:00
parent 2dccc6b17b
commit 824a384ffd
4 changed files with 30 additions and 20 deletions

View file

@ -3,6 +3,9 @@
#include <cmath>
#include <fmt/core.h>
#include <array>
#include "map.hpp"
using namespace fmt;
std::array<sf::Color, 10> VALUES{
sf::Color{1, 4, 2}, // black
@ -27,7 +30,7 @@ sf::Color SFMLRender::color(Value val) {
SFMLRender::SFMLRender(Canvas &canvas, Screen &map_screen, Screen &screen) :
$window(sf::VideoMode(VIDEO_X,VIDEO_Y), "Roguish"),
$map_font_size(BASE_MAP_FONT_SIZE),
$map_font_size(0),
$line_spacing(0),
$canvas(canvas),
$map_screen(map_screen),
@ -59,12 +62,30 @@ sf::Sprite &SFMLRender::get_text_sprite(wchar_t tile) {
return $sprites[tile];
}
inline bool base_glyph_check(sf::Font &font, sf::Glyph &base_glyph, Point &view_port, int &font_size, int new_size) {
auto glyph = font.getGlyph(BG_TILE, new_size, false);
bool SFMLRender::resize_map(int new_size) {
if(MIN_FONT_SIZE < new_size && new_size < MAX_FONT_SIZE) {
int view_x = std::ceil((VIDEO_X - GAME_MAP_POS) / glyph.bounds.width);
int view_y = std::ceil(VIDEO_Y / glyph.bounds.height);
// don't allow resizing beyond/below game map size
if(view_x <= GAME_MAP_X && view_y <= GAME_MAP_Y) {
// looks good, set 'em all
base_glyph = glyph;
view_port = {size_t(view_x), size_t(view_y)};
font_size = new_size;
return true;
} else {
return false;
}
}
bool SFMLRender::resize_map(int new_size, Point &view_port) {
if($map_font_size == new_size || new_size < MIN_FONT_SIZE || new_size > MAX_FONT_SIZE) return false;
if(base_glyph_check($font, $base_glyph, view_port, $map_font_size, new_size)) {
$sprites.clear(); // need to reset the sprites for the new size
$map_font_size = new_size;
$base_glyph = $font.getGlyph(BG_TILE, $map_font_size, false);
$line_spacing = $font.getLineSpacing($map_font_size);
$bg_sprite = get_text_sprite(BG_TILE);
$bg_bounds = $bg_sprite.getLocalBounds();