Renderer is now more standalone and doesn't try to protect against small maps, that's the GUI's job.

This commit is contained in:
Zed A. Shaw 2024-11-21 15:51:22 -05:00
parent 19b8bf1850
commit 15a302d133
11 changed files with 65 additions and 90 deletions

View file

@ -10,7 +10,7 @@
using namespace fmt;
SFMLRender::SFMLRender() :
$window(sf::VideoMode(VIDEO_X,VIDEO_Y), "Roguish"),
$window(sf::VideoMode($config.video_x,$config.video_y), "Roguish"),
$map_font_size(0),
$line_spacing(0),
$default_fg(color::LIGHT_MID),
@ -22,9 +22,9 @@ SFMLRender::SFMLRender() :
$font.setSmooth(false);
$ui_text.setFont($font);
$ui_text.setPosition(0,0);
$ui_text.setCharacterSize(UI_FONT_SIZE);
$ui_text.setCharacterSize($config.ui_font_size);
$ui_text.setFillColor(color::LIGHT_MID);
sf::Glyph glyph = $font.getGlyph(UI_BASE_CHAR, UI_FONT_SIZE, false);
sf::Glyph glyph = $font.getGlyph($config.ui_base_char, $config.ui_font_size, false);
$ui_bounds = glyph.bounds;
}
@ -43,47 +43,21 @@ 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);
int view_x = std::ceil((VIDEO_X - GAME_MAP_POS) / glyph.bounds.width);
int view_y = std::ceil(VIDEO_Y / glyph.bounds.height);
bool SFMLRender::resize_grid(int new_size, Panel &panel_out) {
auto glyph = $font.getGlyph($config.bg_tile, new_size, false);
int view_x = std::ceil(($config.video_x - panel_out.x) / glyph.bounds.width);
int view_y = std::ceil(($config.video_y - panel_out.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 {
println("VIEW TOO BIG, view={},{} MAP={},{}", view_x, view_y,
GAME_MAP_X, GAME_MAP_Y);
return false;
}
}
bool SFMLRender::resize_grid(int new_size, Point &view_port) {
if($map_font_size == new_size || new_size < MIN_FONT_SIZE || new_size > MAX_FONT_SIZE) {
println("invalid map font size {}, =={}, min={}, max={}",
new_size, $map_font_size, MIN_FONT_SIZE, MAX_FONT_SIZE);
return false;
} else {
println("NEW SIZE SELECTED {}", new_size);
}
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
$line_spacing = $font.getLineSpacing($map_font_size);
$bg_sprite = get_text_sprite(BG_TILE);
$bg_bounds = $bg_sprite.getLocalBounds();
return true;
} else {
println("BASE GLYPH FAILED!");
// something else here
return false;
}
// looks good, set 'em all
$base_glyph = glyph;
$map_font_size = new_size;
$sprites.clear(); // need to reset the sprites for the new size
$line_spacing = $font.getLineSpacing($map_font_size);
$bg_sprite = get_text_sprite($config.bg_tile);
$bg_bounds = $bg_sprite.getLocalBounds();
panel_out.resize(view_x, view_y);
return true;
}
inline void configure_tile(const sf::Sprite &sprite, sf::FloatRect &sp_bounds, sf::FloatRect bg_bounds, float &width_delta, float &height_delta) {
@ -143,7 +117,7 @@ void SFMLRender::render_grid(const std::wstring &text, float x, float y) {
inline sf::FloatRect draw_chunk(sf::RenderWindow& window,
sf::FloatRect ui_bounds, sf::Text& text, sf::Color default_bg,
sf::Color bgcolor, float x, float y, std::wstring &out)
sf::Color bgcolor, int bg_box_offset, float x, float y, std::wstring &out)
{
text.setString(out);
text.setPosition({x, y});
@ -152,7 +126,7 @@ inline sf::FloatRect draw_chunk(sf::RenderWindow& window,
if(default_bg != bgcolor) {
sf::RectangleShape backing({bounds.width, bounds.height});
backing.setFillColor(bgcolor);
backing.setPosition({bounds.left, bounds.top + BG_BOX_OFFSET});
backing.setPosition({bounds.left, bounds.top + bg_box_offset});
window.draw(backing);
}
@ -175,7 +149,7 @@ void SFMLRender::render_text(const std::wstring &text, sf::Color default_bg, flo
if(out.size() > 0 ) {
auto bounds = draw_chunk($window,
$ui_bounds, $ui_text,
default_bg, cur_bg, x, y, out);
default_bg, cur_bg, $config.bg_box_offset, x, y, out);
x += bounds.width;
}
cur_bg = bg;
@ -189,7 +163,7 @@ void SFMLRender::render_text(const std::wstring &text, sf::Color default_bg, flo
if(out.size() > 0) {
bounds = draw_chunk($window, $ui_bounds,
$ui_text, default_bg, cur_bg, x, y, out);
$ui_text, default_bg, cur_bg, $config.bg_box_offset, x, y, out);
} else {
bounds = $ui_text.getLocalBounds();
}
@ -206,7 +180,7 @@ void SFMLRender::render_text(const std::wstring &text, sf::Color default_bg, flo
);
if(out.size() > 0) {
draw_chunk($window, $ui_bounds, $ui_text, default_bg, cur_bg, x, y, out);
draw_chunk($window, $ui_bounds, $ui_text, default_bg, cur_bg, $config.bg_box_offset, x, y, out);
}
}