Further cleaning of the renderer.
This commit is contained in:
parent
fd8180bc61
commit
24b1e4a500
2 changed files with 21 additions and 21 deletions
38
render.cpp
38
render.cpp
|
@ -64,6 +64,8 @@ bool SFMLRender::resize_map(int new_size) {
|
||||||
$map_font_size = new_size;
|
$map_font_size = new_size;
|
||||||
$base_glyph = $font.getGlyph(BG_TILE, $map_font_size, false);
|
$base_glyph = $font.getGlyph(BG_TILE, $map_font_size, false);
|
||||||
$line_spacing = $font.getLineSpacing($map_font_size);
|
$line_spacing = $font.getLineSpacing($map_font_size);
|
||||||
|
$bg_sprite = get_text_sprite(BG_TILE);
|
||||||
|
$bg_bounds = $bg_sprite.getLocalBounds();
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
// something else here
|
// something else here
|
||||||
|
@ -78,19 +80,9 @@ void SFMLRender::draw_main_ui() {
|
||||||
$window.draw($ui_text);
|
$window.draw($ui_text);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SFMLRender::draw_screen(bool clear, float map_off_x, float map_off_y) {
|
void SFMLRender::render_text(std::string &text, float x, float y) {
|
||||||
if(clear) $window.clear();
|
|
||||||
draw_main_ui();
|
|
||||||
|
|
||||||
std::string map_screenout = $map_screen.ToString();
|
|
||||||
|
|
||||||
float y = 0.0f;
|
|
||||||
float x = GAME_MAP_POS;
|
|
||||||
// make a copy so we don't modify the cached one
|
// make a copy so we don't modify the cached one
|
||||||
auto bg_sprite = get_text_sprite(BG_TILE);
|
$ansi.parse(text, [&](sf::Color bg, sf::Color fg, wchar_t tile) {
|
||||||
auto bg_bounds = bg_sprite.getLocalBounds();
|
|
||||||
|
|
||||||
$ansi.parse(map_screenout, [&](sf::Color bg, sf::Color fg, wchar_t tile) {
|
|
||||||
if(tile == '\n') {
|
if(tile == '\n') {
|
||||||
// don't bother processing newlines, just skip
|
// don't bother processing newlines, just skip
|
||||||
y += $line_spacing;
|
y += $line_spacing;
|
||||||
|
@ -98,29 +90,33 @@ void SFMLRender::draw_screen(bool clear, float map_off_x, float map_off_y) {
|
||||||
} else if(tile == L'\r') {
|
} else if(tile == L'\r') {
|
||||||
return; // skip these, just windows junk
|
return; // skip these, just windows junk
|
||||||
} else {
|
} else {
|
||||||
// fmt::println("FG: {},{},{},{}; BG: {},{},{},{}; ch: {}",
|
$bg_sprite.setPosition({x, y});
|
||||||
// fg.r, fg.g, fg.b, fg.a, bg.r, bg.g, bg.b, bg.a, int(tile));
|
|
||||||
// it's a visual cell
|
|
||||||
bg_sprite.setPosition({x+map_off_x, y+map_off_y});
|
|
||||||
sf::Sprite &sprite = get_text_sprite(tile);
|
sf::Sprite &sprite = get_text_sprite(tile);
|
||||||
bg_sprite.setColor(bg);
|
$bg_sprite.setColor(bg);
|
||||||
|
|
||||||
// should look into caching all this instead of calcing it each time
|
// should look into caching all this instead of calcing it each time
|
||||||
auto sp_bounds = sprite.getLocalBounds();
|
auto 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
|
||||||
auto width_delta = bg_bounds.width > sp_bounds.width ? (bg_bounds.width - sp_bounds.width) / 2 : 0;
|
auto width_delta = $bg_bounds.width > sp_bounds.width ? ($bg_bounds.width - sp_bounds.width) / 2 : 0;
|
||||||
auto height_delta = bg_bounds.height > sp_bounds.width ? (bg_bounds.height - sp_bounds.height) / 2 : 0;
|
auto height_delta = $bg_bounds.height > sp_bounds.width ? ($bg_bounds.height - sp_bounds.height) / 2 : 0;
|
||||||
|
|
||||||
sprite.setPosition({x+width_delta+map_off_x, y+height_delta+map_off_y});
|
sprite.setPosition({x+width_delta, y+height_delta});
|
||||||
sprite.setColor(fg);
|
sprite.setColor(fg);
|
||||||
|
|
||||||
$window.draw(bg_sprite);
|
$window.draw($bg_sprite);
|
||||||
$window.draw(sprite);
|
$window.draw(sprite);
|
||||||
// next cell
|
// next cell
|
||||||
x += $base_glyph.advance;
|
x += $base_glyph.advance;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void SFMLRender::draw_screen(bool clear, float map_off_x, float map_off_y) {
|
||||||
|
if(clear) $window.clear();
|
||||||
|
draw_main_ui();
|
||||||
|
|
||||||
|
std::string map_screenout = $map_screen.ToString();
|
||||||
|
render_text(map_screenout, GAME_MAP_POS+map_off_x, map_off_y);
|
||||||
$window.display();
|
$window.display();
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#include <SFML/Window.hpp>
|
#include <SFML/Window.hpp>
|
||||||
#include <SFML/System.hpp>
|
#include <SFML/System.hpp>
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include <SFML/Graphics/Rect.hpp>
|
||||||
#include "point.hpp"
|
#include "point.hpp"
|
||||||
#include <codecvt>
|
#include <codecvt>
|
||||||
#include "ansi_parser.hpp"
|
#include "ansi_parser.hpp"
|
||||||
|
@ -37,6 +38,8 @@ struct SFMLRender {
|
||||||
sf::Font $font;
|
sf::Font $font;
|
||||||
sf::Texture $font_texture;
|
sf::Texture $font_texture;
|
||||||
sf::Glyph $base_glyph;
|
sf::Glyph $base_glyph;
|
||||||
|
sf::Sprite $bg_sprite;
|
||||||
|
sf::FloatRect $bg_bounds;
|
||||||
Canvas& $canvas;
|
Canvas& $canvas;
|
||||||
Screen& $map_screen;
|
Screen& $map_screen;
|
||||||
Screen& $screen;
|
Screen& $screen;
|
||||||
|
@ -55,6 +58,7 @@ struct SFMLRender {
|
||||||
sf::Color color(Value val);
|
sf::Color color(Value val);
|
||||||
sf::Sprite &get_text_sprite(wchar_t tile);
|
sf::Sprite &get_text_sprite(wchar_t tile);
|
||||||
bool resize_map(int new_size);
|
bool resize_map(int new_size);
|
||||||
|
void render_text(std::string &text, float x, float y);
|
||||||
void draw_main_ui();
|
void draw_main_ui();
|
||||||
void draw_screen(bool clear=true, float map_off_x=0.0f, float map_off_y=0.0f);
|
void draw_screen(bool clear=true, float map_off_x=0.0f, float map_off_y=0.0f);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue