A bit of cleanup and testing of the panel, then some optimization to avoid re-rendering and multiple wchar converts.

main
Zed A. Shaw 1 week ago
parent 6e848004c4
commit 7d3605f58b
  1. 12
      gui.cpp
  2. 19
      panel.cpp
  3. 8
      panel.hpp
  4. 14
      render.cpp
  5. 4
      render.hpp

@ -201,9 +201,19 @@ void GUI::shake() {
void GUI::render_scene() {
$renderer.clear();
$renderer.draw_text_ui($status_ui);
$renderer.draw_text_ui($status_ui, true);
$renderer.draw_screen($map_view);
/*
Panel prompt(30, 10, GAME_MAP_POS + 30, 200);
prompt.set_renderer([&] {
return hbox({
hflow(vbox(text("GOLD!")))
});
});
$renderer.draw_text_ui(prompt, true);
*/
$renderer.display();
}

@ -1,24 +1,31 @@
#include "panel.hpp"
void Panel::resize(int width, int height) {
$dirty = true;
$screen = Screen(width, height);
}
void Panel::set_renderer(std::function< Element()> render) {
$dirty = true;
$component = Renderer(render);
}
Screen &Panel::render() {
void Panel::render() {
$dirty = true;
if($must_clear) $screen.Clear();
Render($screen, $component->Render());
return $screen;
}
std::wstring Panel::to_string() {
std::string screenout = $screen.ToString();
return $converter.from_bytes(screenout);
const std::wstring& Panel::to_string() {
if($dirty) {
std::string as_text = $screen.ToString();
$screenout = $converter.from_bytes(as_text);
$dirty = false;
}
return $screenout;
}
Screen &Panel::screen() {
const Screen &Panel::screen() {
return $screen;
}

@ -17,6 +17,8 @@ struct Panel {
int y;
int width;
int height;
std::wstring $screenout;
bool $dirty = true;
Component $component;
Screen $screen;
bool $must_clear = true;
@ -33,7 +35,7 @@ struct Panel {
void resize(int width, int height);
void set_renderer(std::function< Element()> render);
Screen &render();
std::wstring to_string();
Screen &screen();
void render();
const std::wstring &to_string();
const Screen &screen();
};

@ -104,7 +104,7 @@ inline void configure_tile(const sf::Sprite &sprite, sf::FloatRect &sp_bounds,
height_delta = bg_bounds.height > sp_bounds.width ? (bg_bounds.height - sp_bounds.height) / 2 : 0;
}
void SFMLRender::render_text(std::wstring &text, float x, float y) {
void SFMLRender::render_text(const std::wstring &text, float x, float y) {
wchar_t last_tile = '#';
sf::FloatRect sp_bounds;
float width_delta = 0;
@ -142,17 +142,23 @@ void SFMLRender::render_text(std::wstring &text, float x, float y) {
});
}
void SFMLRender::draw_text_ui(Panel &panel) {
void SFMLRender::draw_text_ui(Panel &panel, bool with_border) {
sf::RectangleShape backing(
sf::Vector2f($ui_bounds.width * panel.width,
$ui_bounds.height * panel.height));
backing.setFillColor(sf::Color(0, 0, 0));
if(with_border) {
backing.setOutlineColor(color(Value::MID));
backing.setOutlineThickness(5);
}
backing.setPosition(panel.x, panel.y);
$window.draw(backing);
panel.render();
std::wstring panelout = panel.to_string();
const std::wstring &panelout = panel.to_string();
$ui_text.setPosition(panel.x, panel.y);
$ui_text.setString(panelout);
$window.draw($ui_text);
@ -160,6 +166,6 @@ void SFMLRender::draw_text_ui(Panel &panel) {
void SFMLRender::draw_screen(Panel &panel, float x, float y) {
panel.render();
std::wstring panelout = panel.to_string();
const std::wstring &panelout = panel.to_string();
render_text(panelout, panel.x + x, panel.y + y);
}

@ -55,8 +55,8 @@ struct SFMLRender {
sf::Color color(Value val);
sf::Sprite &get_text_sprite(wchar_t tile);
bool resize_map(int new_size, Point &view_port);
void render_text(std::wstring &text, float x, float y);
void draw_text_ui(Panel &panel);
void render_text(const std::wstring &text, float x, float y);
void draw_text_ui(Panel &panel, bool with_border=false);
void draw_screen(Panel &panel, float map_off_x=0.0f, float map_off_y=0.0f);
bool poll_event(sf::Event &event) {

Loading…
Cancel
Save