A bit of cleanup and testing of the panel, then some optimization to avoid re-rendering and multiple wchar converts.
This commit is contained in:
parent
6e848004c4
commit
7d3605f58b
5 changed files with 41 additions and 16 deletions
12
gui.cpp
12
gui.cpp
|
@ -201,9 +201,19 @@ void GUI::shake() {
|
||||||
void GUI::render_scene() {
|
void GUI::render_scene() {
|
||||||
$renderer.clear();
|
$renderer.clear();
|
||||||
|
|
||||||
$renderer.draw_text_ui($status_ui);
|
$renderer.draw_text_ui($status_ui, true);
|
||||||
$renderer.draw_screen($map_view);
|
$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();
|
$renderer.display();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
19
panel.cpp
19
panel.cpp
|
@ -1,24 +1,31 @@
|
||||||
#include "panel.hpp"
|
#include "panel.hpp"
|
||||||
|
|
||||||
void Panel::resize(int width, int height) {
|
void Panel::resize(int width, int height) {
|
||||||
|
$dirty = true;
|
||||||
$screen = Screen(width, height);
|
$screen = Screen(width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Panel::set_renderer(std::function< Element()> render) {
|
void Panel::set_renderer(std::function< Element()> render) {
|
||||||
|
$dirty = true;
|
||||||
$component = Renderer(render);
|
$component = Renderer(render);
|
||||||
}
|
}
|
||||||
|
|
||||||
Screen &Panel::render() {
|
void Panel::render() {
|
||||||
|
$dirty = true;
|
||||||
if($must_clear) $screen.Clear();
|
if($must_clear) $screen.Clear();
|
||||||
Render($screen, $component->Render());
|
Render($screen, $component->Render());
|
||||||
return $screen;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::wstring Panel::to_string() {
|
const std::wstring& Panel::to_string() {
|
||||||
std::string screenout = $screen.ToString();
|
if($dirty) {
|
||||||
return $converter.from_bytes(screenout);
|
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;
|
return $screen;
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,8 @@ struct Panel {
|
||||||
int y;
|
int y;
|
||||||
int width;
|
int width;
|
||||||
int height;
|
int height;
|
||||||
|
std::wstring $screenout;
|
||||||
|
bool $dirty = true;
|
||||||
Component $component;
|
Component $component;
|
||||||
Screen $screen;
|
Screen $screen;
|
||||||
bool $must_clear = true;
|
bool $must_clear = true;
|
||||||
|
@ -33,7 +35,7 @@ struct Panel {
|
||||||
|
|
||||||
void resize(int width, int height);
|
void resize(int width, int height);
|
||||||
void set_renderer(std::function< Element()> render);
|
void set_renderer(std::function< Element()> render);
|
||||||
Screen &render();
|
void render();
|
||||||
std::wstring to_string();
|
const std::wstring &to_string();
|
||||||
Screen &screen();
|
const Screen &screen();
|
||||||
};
|
};
|
||||||
|
|
14
render.cpp
14
render.cpp
|
@ -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;
|
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 = '#';
|
wchar_t last_tile = '#';
|
||||||
sf::FloatRect sp_bounds;
|
sf::FloatRect sp_bounds;
|
||||||
float width_delta = 0;
|
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::RectangleShape backing(
|
||||||
sf::Vector2f($ui_bounds.width * panel.width,
|
sf::Vector2f($ui_bounds.width * panel.width,
|
||||||
$ui_bounds.height * panel.height));
|
$ui_bounds.height * panel.height));
|
||||||
|
|
||||||
backing.setFillColor(sf::Color(0, 0, 0));
|
backing.setFillColor(sf::Color(0, 0, 0));
|
||||||
|
|
||||||
|
if(with_border) {
|
||||||
|
backing.setOutlineColor(color(Value::MID));
|
||||||
|
backing.setOutlineThickness(5);
|
||||||
|
}
|
||||||
|
|
||||||
backing.setPosition(panel.x, panel.y);
|
backing.setPosition(panel.x, panel.y);
|
||||||
$window.draw(backing);
|
$window.draw(backing);
|
||||||
|
|
||||||
panel.render();
|
panel.render();
|
||||||
std::wstring panelout = panel.to_string();
|
const std::wstring &panelout = panel.to_string();
|
||||||
$ui_text.setPosition(panel.x, panel.y);
|
$ui_text.setPosition(panel.x, panel.y);
|
||||||
$ui_text.setString(panelout);
|
$ui_text.setString(panelout);
|
||||||
$window.draw($ui_text);
|
$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) {
|
void SFMLRender::draw_screen(Panel &panel, float x, float y) {
|
||||||
panel.render();
|
panel.render();
|
||||||
std::wstring panelout = panel.to_string();
|
const std::wstring &panelout = panel.to_string();
|
||||||
render_text(panelout, panel.x + x, panel.y + y);
|
render_text(panelout, panel.x + x, panel.y + y);
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,8 +55,8 @@ 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, Point &view_port);
|
bool resize_map(int new_size, Point &view_port);
|
||||||
void render_text(std::wstring &text, float x, float y);
|
void render_text(const std::wstring &text, float x, float y);
|
||||||
void draw_text_ui(Panel &panel);
|
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);
|
void draw_screen(Panel &panel, float map_off_x=0.0f, float map_off_y=0.0f);
|
||||||
|
|
||||||
bool poll_event(sf::Event &event) {
|
bool poll_event(sf::Event &event) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue