A bit of some clean up, API unifying, and some performance tweaks.
This commit is contained in:
parent
a4926bedcb
commit
fb1fd9d8bc
8 changed files with 72 additions and 42 deletions
2
gui.cpp
2
gui.cpp
|
@ -240,7 +240,9 @@ void GUI::shake() {
|
|||
void GUI::render_scene() {
|
||||
$renderer.clear();
|
||||
|
||||
$status_ui.render();
|
||||
$renderer.draw($status_ui);
|
||||
$map_view.render();
|
||||
$renderer.draw($map_view);
|
||||
|
||||
$renderer.display();
|
||||
|
|
1
main.cpp
1
main.cpp
|
@ -15,7 +15,6 @@
|
|||
#include <io.h>
|
||||
#endif
|
||||
|
||||
|
||||
using namespace ftxui;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@ void Panel::set_renderer(Component renderer) {
|
|||
}
|
||||
|
||||
void Panel::add(Component child) {
|
||||
$dirty = true;
|
||||
$component->Add(child);
|
||||
}
|
||||
|
||||
|
|
|
@ -24,6 +24,7 @@ struct Panel {
|
|||
bool must_clear = true;
|
||||
bool grid = false;
|
||||
sf::Color default_bg = color::BLACK;
|
||||
sf::Color default_fg = color::LIGHT_LIGHT;
|
||||
sf::Color border_color = color::MID;
|
||||
int border_px = UI_PANEL_BORDER_PX;
|
||||
|
||||
|
|
60
render.cpp
60
render.cpp
|
@ -69,19 +69,21 @@ 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_grid(const std::wstring &text, float x, float y) {
|
||||
void SFMLRender::render_grid(const std::wstring &text, sf::Color default_fg, sf::Color default_bg, 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;
|
||||
// BUG: get default_fg from panel too
|
||||
sf::Color cur_fg = default_fg;
|
||||
sf::Color cur_bg = default_bg;
|
||||
|
||||
// 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);
|
||||
cur_bg = bg;
|
||||
},
|
||||
|
||||
[&](wchar_t tile) {
|
||||
|
@ -94,7 +96,6 @@ void SFMLRender::render_grid(const std::wstring &text, float x, float y) {
|
|||
}
|
||||
break;
|
||||
default: {
|
||||
$bg_sprite.setPosition({x, y});
|
||||
|
||||
// only get a new sprite if the tile changed
|
||||
if(last_tile != tile) {
|
||||
|
@ -106,7 +107,13 @@ void SFMLRender::render_grid(const std::wstring &text, float x, float y) {
|
|||
sprite.setPosition({x+width_delta, y+height_delta});
|
||||
sprite.setColor(cur_fg);
|
||||
|
||||
$window.draw($bg_sprite);
|
||||
// only draw background char if it's different from default
|
||||
if(cur_bg != default_bg) {
|
||||
$bg_sprite.setPosition({x, y});
|
||||
$bg_sprite.setColor(cur_bg);
|
||||
$window.draw($bg_sprite);
|
||||
}
|
||||
|
||||
$window.draw(sprite);
|
||||
// next cell
|
||||
x += $base_glyph.advance;
|
||||
|
@ -135,14 +142,14 @@ inline sf::FloatRect draw_chunk(sf::RenderWindow& window,
|
|||
return bounds;
|
||||
}
|
||||
|
||||
void SFMLRender::render_text(const std::wstring &text, sf::Color default_bg, float start_x, float start_y) {
|
||||
void SFMLRender::render_text(const std::wstring &text, sf::Color default_fg, 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);
|
||||
$ui_text.setFillColor(default_fg);
|
||||
|
||||
$ansi.parse(text,
|
||||
[&](sf::Color fg, sf::Color bg) {
|
||||
|
@ -184,26 +191,33 @@ void SFMLRender::render_text(const std::wstring &text, sf::Color default_bg, flo
|
|||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Does not render the panel, you have to do that so you can control
|
||||
* when things render.
|
||||
*/
|
||||
void SFMLRender::draw(Panel &panel, float x_offset, float y_offset) {
|
||||
panel.render();
|
||||
const std::wstring &panelout = panel.to_string();
|
||||
|
||||
// BUG: ui vs bg doesn't make sense. maybe grid vs. text?
|
||||
auto bounds = panel.grid ? $bg_bounds : $ui_bounds;
|
||||
|
||||
sf::RectangleShape backing(
|
||||
sf::Vector2f(bounds.width * panel.width + panel.border_px,
|
||||
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);
|
||||
|
||||
if(panel.grid) {
|
||||
render_grid(panelout, panel.x + x_offset, panel.y + y_offset);
|
||||
render_grid(panelout, panel.default_fg, panel.default_bg, 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);
|
||||
render_text(panelout, panel.default_fg, panel.default_bg, panel.x + x_offset, panel.y + y_offset);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,8 +51,8 @@ struct SFMLRender {
|
|||
|
||||
sf::Sprite &get_text_sprite(wchar_t tile);
|
||||
bool resize_grid(int new_size, Panel &panel_out);
|
||||
void render_grid(const std::wstring &text, float x, float y);
|
||||
void render_text(const std::wstring &text, sf::Color bgcolor, float x, float y);
|
||||
void render_grid(const std::wstring &text, sf::Color default_fg, sf::Color default_bg, float x, float y);
|
||||
void render_text(const std::wstring &text, sf::Color default_fg, sf::Color default_bg, float x, float y);
|
||||
|
||||
void draw(Panel &panel, float x_offset=0.0f, float y_offset=0.0f);
|
||||
|
||||
|
|
|
@ -81,10 +81,7 @@ int main(int argc, char *argv[]) {
|
|||
// divide the image into cells
|
||||
auto size = image.getSize();
|
||||
|
||||
const Point cell = {4, 6};
|
||||
|
||||
// create a grid panel to hold the cells
|
||||
Panel panel(0, 0, 0, 0, true);
|
||||
const Point cell = {3, 6};
|
||||
|
||||
println("IMAGE SIZE {},{}", size.x, size.y);
|
||||
RGBColor avg{0,0,0};
|
||||
|
@ -108,8 +105,11 @@ int main(int argc, char *argv[]) {
|
|||
}
|
||||
|
||||
// average it for the cell size
|
||||
RGBColor color = {avg.r / int(cell.x * cell.y),
|
||||
avg.g / int(cell.x * cell.y), avg.b / int(cell.x * cell.y)};
|
||||
RGBColor color = {
|
||||
avg.r / int(cell.x * cell.y),
|
||||
avg.g / int(cell.x * cell.y),
|
||||
avg.b / int(cell.x * cell.y),
|
||||
};
|
||||
|
||||
// add it
|
||||
colors[i][j] = color;
|
||||
|
@ -120,10 +120,9 @@ int main(int argc, char *argv[]) {
|
|||
}
|
||||
|
||||
Canvas drawing;
|
||||
|
||||
|
||||
// create a grid panel to hold the cells
|
||||
Panel panel(0, 0, 0, 0, true);
|
||||
SFMLRender renderer;
|
||||
|
||||
renderer.resize_grid(26, panel);
|
||||
|
||||
drawing = Canvas(panel.width * 2, panel.height * 4);
|
||||
|
@ -142,8 +141,28 @@ int main(int argc, char *argv[]) {
|
|||
|
||||
sf::Event event;
|
||||
|
||||
int start_x = 0;
|
||||
int start_y = 0;
|
||||
int end_x = 600;
|
||||
int end_y = 300;
|
||||
int cur_x = start_x;
|
||||
int cur_y = start_y;
|
||||
|
||||
sf::Texture texture;
|
||||
texture.loadFromFile(image_file);
|
||||
sf::Sprite sprite(texture);
|
||||
|
||||
panel.render();
|
||||
|
||||
while(renderer.is_open()) {
|
||||
renderer.draw(panel);
|
||||
cur_x = cur_x < end_x ? cur_x + 1 : start_x;
|
||||
cur_y = cur_y < end_y ? cur_y + 1 : start_y;
|
||||
|
||||
// sprite.setPosition(cur_x, cur_y);
|
||||
// renderer.$window.draw(sprite);
|
||||
// renderer.display();
|
||||
|
||||
renderer.draw(panel, cur_x, cur_y);
|
||||
renderer.display();
|
||||
|
||||
while(renderer.poll_event(event)) {
|
||||
|
@ -151,8 +170,6 @@ int main(int argc, char *argv[]) {
|
|||
renderer.close();
|
||||
}
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(100ms);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
|
@ -1,9 +1,5 @@
|
|||
TODAY'S GOAL:
|
||||
|
||||
* Image -> Text converter.
|
||||
* Renderer needs to not have fixed setting for its size from the map.
|
||||
* Refactor out GAME_MAP_* to be a parameter.
|
||||
|
||||
|
||||
TODO:
|
||||
* Keep panel unified by implementing border and backing on grids too.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue