A bit of some clean up, API unifying, and some performance tweaks.

main
Zed A. Shaw 11 hours ago
parent a4926bedcb
commit fb1fd9d8bc
  1. 2
      gui.cpp
  2. 1
      main.cpp
  3. 1
      panel.cpp
  4. 1
      panel.hpp
  5. 58
      render.cpp
  6. 4
      render.hpp
  7. 41
      scratchpad/img2ansi.cpp
  8. 4
      status.txt

@ -240,7 +240,9 @@ void GUI::shake() {
void GUI::render_scene() { void GUI::render_scene() {
$renderer.clear(); $renderer.clear();
$status_ui.render();
$renderer.draw($status_ui); $renderer.draw($status_ui);
$map_view.render();
$renderer.draw($map_view); $renderer.draw($map_view);
$renderer.display(); $renderer.display();

@ -15,7 +15,6 @@
#include <io.h> #include <io.h>
#endif #endif
using namespace ftxui; using namespace ftxui;
namespace fs = std::filesystem; namespace fs = std::filesystem;

@ -13,6 +13,7 @@ void Panel::set_renderer(Component renderer) {
} }
void Panel::add(Component child) { void Panel::add(Component child) {
$dirty = true;
$component->Add(child); $component->Add(child);
} }

@ -24,6 +24,7 @@ struct Panel {
bool must_clear = true; bool must_clear = true;
bool grid = false; bool grid = false;
sf::Color default_bg = color::BLACK; sf::Color default_bg = color::BLACK;
sf::Color default_fg = color::LIGHT_LIGHT;
sf::Color border_color = color::MID; sf::Color border_color = color::MID;
int border_px = UI_PANEL_BORDER_PX; int border_px = UI_PANEL_BORDER_PX;

@ -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; 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 = '#'; wchar_t last_tile = '#';
sf::FloatRect sp_bounds; sf::FloatRect sp_bounds;
float width_delta = 0; float width_delta = 0;
float height_delta = 0; float height_delta = 0;
sf::Sprite &sprite = get_text_sprite(last_tile); sf::Sprite &sprite = get_text_sprite(last_tile);
const float start_x = x; 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 // make a copy so we don't modify the cached one
$ansi.parse(text, [&](sf::Color fg, sf::Color bg) { $ansi.parse(text, [&](sf::Color fg, sf::Color bg) {
cur_fg = fg; cur_fg = fg;
$bg_sprite.setColor(bg); cur_bg = bg;
}, },
[&](wchar_t tile) { [&](wchar_t tile) {
@ -94,7 +96,6 @@ void SFMLRender::render_grid(const std::wstring &text, float x, float y) {
} }
break; break;
default: { default: {
$bg_sprite.setPosition({x, y});
// only get a new sprite if the tile changed // only get a new sprite if the tile changed
if(last_tile != tile) { 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.setPosition({x+width_delta, y+height_delta});
sprite.setColor(cur_fg); 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); $window.draw(sprite);
// next cell // next cell
x += $base_glyph.advance; x += $base_glyph.advance;
@ -135,14 +142,14 @@ inline sf::FloatRect draw_chunk(sf::RenderWindow& window,
return bounds; 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; std::wstring out;
float x = start_x; float x = start_x;
float y = start_y; float y = start_y;
sf::Color cur_bg = default_bg; sf::Color cur_bg = default_bg;
// start with the default_fg until it's changed // start with the default_fg until it's changed
$ui_text.setFillColor($default_fg); $ui_text.setFillColor(default_fg);
$ansi.parse(text, $ansi.parse(text,
[&](sf::Color fg, sf::Color bg) { [&](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) { void SFMLRender::draw(Panel &panel, float x_offset, float y_offset) {
panel.render();
const std::wstring &panelout = panel.to_string(); const std::wstring &panelout = panel.to_string();
if(panel.grid) { // BUG: ui vs bg doesn't make sense. maybe grid vs. text?
render_grid(panelout, panel.x + x_offset, panel.y + y_offset); auto bounds = panel.grid ? $bg_bounds : $ui_bounds;
} 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); sf::RectangleShape backing(
sf::Vector2f(bounds.width * panel.width + panel.border_px,
bounds.height * panel.height + panel.border_px));
if(panel.has_border) { backing.setFillColor(panel.default_bg);
backing.setOutlineColor(panel.border_color);
backing.setOutlineThickness(panel.border_px);
}
backing.setPosition(panel.x + x_offset, panel.y + y_offset); if(panel.has_border) {
$window.draw(backing); backing.setOutlineColor(panel.border_color);
render_text(panelout, panel.default_bg, panel.x, panel.y); 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.default_fg, panel.default_bg, panel.x + x_offset, panel.y + y_offset);
} else {
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); sf::Sprite &get_text_sprite(wchar_t tile);
bool resize_grid(int new_size, Panel &panel_out); bool resize_grid(int new_size, Panel &panel_out);
void render_grid(const std::wstring &text, 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 bgcolor, 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); 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 // divide the image into cells
auto size = image.getSize(); auto size = image.getSize();
const Point cell = {4, 6}; const Point cell = {3, 6};
// create a grid panel to hold the cells
Panel panel(0, 0, 0, 0, true);
println("IMAGE SIZE {},{}", size.x, size.y); println("IMAGE SIZE {},{}", size.x, size.y);
RGBColor avg{0,0,0}; RGBColor avg{0,0,0};
@ -108,8 +105,11 @@ int main(int argc, char *argv[]) {
} }
// average it for the cell size // average it for the cell size
RGBColor color = {avg.r / int(cell.x * cell.y), RGBColor color = {
avg.g / int(cell.x * cell.y), avg.b / int(cell.x * cell.y)}; avg.r / int(cell.x * cell.y),
avg.g / int(cell.x * cell.y),
avg.b / int(cell.x * cell.y),
};
// add it // add it
colors[i][j] = color; colors[i][j] = color;
@ -120,10 +120,9 @@ int main(int argc, char *argv[]) {
} }
Canvas drawing; Canvas drawing;
// create a grid panel to hold the cells
Panel panel(0, 0, 0, 0, true);
SFMLRender renderer; SFMLRender renderer;
renderer.resize_grid(26, panel); renderer.resize_grid(26, panel);
drawing = Canvas(panel.width * 2, panel.height * 4); drawing = Canvas(panel.width * 2, panel.height * 4);
@ -142,8 +141,28 @@ int main(int argc, char *argv[]) {
sf::Event event; 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()) { 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(); renderer.display();
while(renderer.poll_event(event)) { while(renderer.poll_event(event)) {
@ -151,8 +170,6 @@ int main(int argc, char *argv[]) {
renderer.close(); renderer.close();
} }
} }
std::this_thread::sleep_for(100ms);
} }
return 0; return 0;

@ -1,9 +1,5 @@
TODAY'S GOAL: 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: TODO:
* Keep panel unified by implementing border and backing on grids too. * Keep panel unified by implementing border and backing on grids too.

Loading…
Cancel
Save