Moved to SFML 3.0 now.

This commit is contained in:
Zed A. Shaw 2025-01-29 12:55:33 -05:00
parent 7c56f350ab
commit e05b2c304c
12 changed files with 167 additions and 173 deletions

View file

@ -16,24 +16,25 @@
using namespace fmt;
SFMLRender::SFMLRender() :
$window(sf::VideoMode($config.video_x,$config.video_y), "Roguish"),
$window(sf::VideoMode({$config.video_x, $config.video_y}), "Roguish"),
$map_font_size(0),
$line_spacing(0),
$default_fg(ColorValue::LIGHT_MID),
$default_bg(ColorValue::BLACK),
$bg_sprite($font_texture),
$font(FONT_FILE_NAME),
$ui_text($font),
$ansi($default_fg, $default_bg)
{
// force true color, but maybe I want to support different color sets
$font.loadFromFile(FONT_FILE_NAME);
$font.setSmooth(false);
$ui_text.setFont($font);
$ui_text.setPosition(0,0);
$ui_text.setPosition({0,0});
$ui_text.setCharacterSize($config.ui_font_size);
$ui_text.setFillColor(ColorValue::LIGHT_MID);
sf::Glyph glyph = $font.getGlyph($config.ui_base_char, $config.ui_font_size, false);
$text_bounds = glyph.bounds;
$cells_w = std::ceil($config.video_x / $text_bounds.width);
$cells_h = std::ceil($config.video_y / $text_bounds.height);
$cells_w = std::ceil($config.video_x / $text_bounds.size.x);
$cells_h = std::ceil($config.video_y / $text_bounds.size.y);
}
sf::Sprite &SFMLRender::get_text_sprite(wchar_t tile) {
@ -43,17 +44,16 @@ sf::Sprite &SFMLRender::get_text_sprite(wchar_t tile) {
// the glyphs on the font texture, so this gets loaded each time
// we get a new glyph from the font.
$font_texture = $font.getTexture($map_font_size);
sf::Sprite sprite($font_texture);
sprite.setTextureRect(glyph.textureRect);
$sprites[tile] = sprite;
$sprites.try_emplace(tile, $font_texture, glyph.textureRect);
}
return $sprites[tile];
return $sprites.at(tile);
}
void SFMLRender::clear_cache() {
$sprites.clear();
$font.loadFromFile("./assets/text.otf");
bool good = $font.openFromFile(FONT_FILE_NAME);
dbc::check(good, "Failed to load the font.");
$font.setSmooth(false);
$ui_text.setFont($font);
}
@ -62,14 +62,14 @@ void SFMLRender::center_panel(Panel &panel) {
int cell_center_x = ($cells_w - panel.width) / 2;
int cell_center_y = ($cells_h - panel.height) / 2;
panel.x = cell_center_x * $text_bounds.width;
panel.y = cell_center_y * $text_bounds.height;
panel.x = cell_center_x * $text_bounds.size.x;
panel.y = cell_center_y * $text_bounds.size.y;
}
void 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);
int view_x = std::ceil(($config.video_x - panel_out.x) / glyph.bounds.size.x);
int view_y = std::ceil(($config.video_y - panel_out.y) / glyph.bounds.size.y);
// looks good, set 'em all
$base_glyph = glyph;
@ -87,8 +87,8 @@ inline void configure_tile(const sf::Sprite &sprite, sf::FloatRect &sp_bounds, s
sp_bounds = sprite.getLocalBounds();
// calculate where to center the sprite, but only if it's smaller
width_delta = grid_bounds.width > sp_bounds.width ? (grid_bounds.width - sp_bounds.width) / 2 : 0;
height_delta = grid_bounds.height > sp_bounds.width ? (grid_bounds.height - sp_bounds.height) / 2 : 0;
width_delta = grid_bounds.size.x > sp_bounds.size.x ? (grid_bounds.size.x - sp_bounds.size.x) / 2 : 0;
height_delta = grid_bounds.size.y > sp_bounds.size.x ? (grid_bounds.size.y - sp_bounds.size.y) / 2 : 0;
}
void SFMLRender::render_grid(const std::wstring &text, sf::Color default_fg, sf::Color default_bg, float x, float y) {
@ -148,12 +148,12 @@ inline sf::FloatRect draw_chunk(sf::RenderWindow& window,
text.setString(out);
text.setPosition({x, y});
// get a base character for the cell size
sf::FloatRect bounds(x, y, text_bounds.width * out.size(), text_bounds.height);
sf::FloatRect bounds({x, y}, {text_bounds.size.x * out.size(), text_bounds.size.y});
if(default_bg != bgcolor) {
sf::RectangleShape backing({bounds.width, bounds.height});
sf::RectangleShape backing({bounds.size.x, bounds.size.y});
backing.setFillColor(bgcolor);
backing.setPosition({bounds.left, bounds.top + bg_box_offset});
backing.setPosition({bounds.position.x, bounds.position.y + bg_box_offset});
window.draw(backing);
}
@ -177,7 +177,7 @@ void SFMLRender::render_text(const std::wstring &text, sf::Color default_fg, sf:
auto bounds = draw_chunk($window,
$text_bounds, $ui_text,
default_bg, cur_bg, $config.bg_box_offset, x, y, out);
x += bounds.width;
x += bounds.size.x;
}
cur_bg = bg;
$ui_text.setFillColor(fg);
@ -195,7 +195,7 @@ void SFMLRender::render_text(const std::wstring &text, sf::Color default_fg, sf:
bounds = $ui_text.getLocalBounds();
}
y += bounds.height;
y += bounds.size.y;
x = start_x; // reset to the original position
}
break;
@ -225,8 +225,8 @@ void SFMLRender::draw(Panel &panel, float x_offset, float y_offset) {
auto bounds = panel.grid ? $grid_bounds : $text_bounds;
sf::RectangleShape backing(
sf::Vector2f(bounds.width * panel.width + panel.border_px,
bounds.height * panel.height + panel.border_px));
sf::Vector2f(bounds.size.x * panel.width + panel.border_px,
bounds.size.y * panel.height + panel.border_px));
backing.setFillColor(panel.default_bg);
@ -235,7 +235,7 @@ void SFMLRender::draw(Panel &panel, float x_offset, float y_offset) {
backing.setOutlineThickness(panel.border_px);
}
backing.setPosition(panel.x + x_offset, panel.y + y_offset);
backing.setPosition({panel.x + x_offset, panel.y + y_offset});
$window.draw(backing);
if(panel.grid) {
@ -252,12 +252,12 @@ bool SFMLRender::mouse_position(Panel &panel, Point &out) {
auto bounds = panel.grid ? $grid_bounds : $text_bounds;
if(pos.x >= panel.x && pos.y >= panel.y
&& pos.x <= (panel.x + panel.width * bounds.width)
&& pos.y <= (panel.y + panel.height * bounds.height))
&& pos.x <= (panel.x + panel.width * bounds.size.x)
&& pos.y <= (panel.y + panel.height * bounds.size.y))
{
out = {
size_t((pos.x - panel.x) / bounds.width),
size_t((pos.y - panel.y) / bounds.height)
size_t((pos.x - panel.x) / bounds.size.x),
size_t((pos.y - panel.y) / bounds.size.y)
};
return true;