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