#include "render.hpp" #include "ansi_parser.hpp" #include #include #include #include "map.hpp" #include #include "color.hpp" using namespace fmt; SFMLRender::SFMLRender() : $window(sf::VideoMode(VIDEO_X,VIDEO_Y), "Roguish"), $map_font_size(0), $line_spacing(0), $default_fg(color::LIGHT_MID), $default_bg(color::BLACK), $ansi($default_fg, $default_bg) { // force true color, but maybe I want to support different color sets $font.loadFromFile("./assets/text.otf"); $font.setSmooth(false); $ui_text.setFont($font); $ui_text.setPosition(0,0); $ui_text.setCharacterSize(UI_FONT_SIZE); $ui_text.setFillColor(color::LIGHT_MID); sf::Glyph glyph = $font.getGlyph(UI_BASE_CHAR, UI_FONT_SIZE, false); $ui_bounds = glyph.bounds; } sf::Sprite &SFMLRender::get_text_sprite(wchar_t tile) { if(!$sprites.contains(tile)) { sf::Glyph glyph = $font.getGlyph(tile, $map_font_size, false); // WARNING! we actually have to do this here because SFML caches // 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; } 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); // 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; } } inline void configure_tile(const sf::Sprite &sprite, sf::FloatRect &sp_bounds, sf::FloatRect bg_bounds, float &width_delta, float &height_delta) { // should look into caching all this instead of calcing it each time sp_bounds = sprite.getLocalBounds(); // calculate where to center the sprite, but only if it's smaller width_delta = bg_bounds.width > sp_bounds.width ? (bg_bounds.width - sp_bounds.width) / 2 : 0; height_delta = bg_bounds.height > sp_bounds.width ? (bg_bounds.height - sp_bounds.height) / 2 : 0; } void SFMLRender::render_grid(const std::wstring &text, float x, float y) { wchar_t last_tile = '#'; sf::FloatRect sp_bounds; float width_delta = 0; float height_delta = 0; sf::Sprite &sprite = get_text_sprite(last_tile); const float start_x = x; sf::Color cur_fg = $default_fg; // make a copy so we don't modify the cached one $ansi.parse(text, [&](sf::Color fg, sf::Color bg) { cur_fg = fg; $bg_sprite.setColor(bg); }, [&](wchar_t tile) { switch(tile) { case '\r': break; // ignore it case '\n': { // don't bother processing newlines, just skip y += $line_spacing; x = start_x; } break; default: { $bg_sprite.setPosition({x, y}); // only get a new sprite if the tile changed if(last_tile != tile) { last_tile = tile; // update last tile seen sprite = get_text_sprite(tile); configure_tile(sprite, sp_bounds, $bg_bounds, width_delta, height_delta); } sprite.setPosition({x+width_delta, y+height_delta}); sprite.setColor(cur_fg); $window.draw($bg_sprite); $window.draw(sprite); // next cell x += $base_glyph.advance; } } }); } 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) { text.setString(out); text.setPosition({x, y}); // get a base character for the cell size sf::FloatRect bounds(x, y, ui_bounds.width * out.size(), ui_bounds.height); if(default_bg != bgcolor) { sf::RectangleShape backing({bounds.width, bounds.height}); backing.setFillColor(bgcolor); backing.setPosition({bounds.left, bounds.top + BG_BOX_OFFSET}); window.draw(backing); } window.draw(text); out.clear(); return bounds; } void SFMLRender::render_text(const std::wstring &text, sf::Color default_bg, float start_x, float start_y) { std::wstring out; float x = start_x; float y = start_y; sf::Color cur_bg = default_bg; // start with the default_fg until it's changed $ui_text.setFillColor($default_fg); $ansi.parse(text, [&](sf::Color fg, sf::Color bg) { if(out.size() > 0 ) { auto bounds = draw_chunk($window, $ui_bounds, $ui_text, default_bg, cur_bg, x, y, out); x += bounds.width; } cur_bg = bg; $ui_text.setFillColor(fg); }, [&](wchar_t tile) { switch(tile) { case '\r': break; // ignore it case '\n': { sf::FloatRect bounds; if(out.size() > 0) { bounds = draw_chunk($window, $ui_bounds, $ui_text, default_bg, cur_bg, x, y, out); } else { bounds = $ui_text.getLocalBounds(); } y += bounds.height; x = start_x; // reset to the original position } break; default: out += tile; break; } } ); if(out.size() > 0) { draw_chunk($window, $ui_bounds, $ui_text, default_bg, cur_bg, x, y, out); } } void SFMLRender::draw(Panel &panel, float x_offset, float y_offset) { panel.render(); const std::wstring &panelout = panel.to_string(); if(panel.grid) { render_grid(panelout, panel.x + x_offset, panel.y + y_offset); } else { sf::RectangleShape backing( sf::Vector2f($ui_bounds.width * panel.width + panel.border_px, $ui_bounds.height * panel.height + panel.border_px)); backing.setFillColor(panel.default_bg); if(panel.has_border) { backing.setOutlineColor(panel.border_color); backing.setOutlineThickness(panel.border_px); } backing.setPosition(panel.x + x_offset, panel.y + y_offset); $window.draw(backing); render_text(panelout, panel.default_bg, panel.x, panel.y); } }