Mostly working now, just had to manually calculate the grid. Needs a cleanup but I'm moving on for now.

main
Zed A. Shaw 4 days ago
parent 89a70f398a
commit 34c84343db
  1. 30
      ansi_parser.cpp
  2. 6
      ansi_parser.rl
  3. 68
      render.cpp
  4. 19
      render.hpp

@ -10,7 +10,7 @@
using namespace fmt; using namespace fmt;
#line 105 "ansi_parser.rl" #line 107 "ansi_parser.rl"
@ -96,7 +96,7 @@ static const int foo_error = 0;
static const int foo_en_main = 23; static const int foo_en_main = 23;
#line 108 "ansi_parser.rl" #line 110 "ansi_parser.rl"
#include <ftxui/screen/terminal.hpp> #include <ftxui/screen/terminal.hpp>
@ -127,7 +127,7 @@ bool ANSIParser::parse(std::wstring_view codes, ColorCB color_cb, WriteCB write_
cs = foo_start; cs = foo_start;
} }
#line 133 "ansi_parser.rl" #line 135 "ansi_parser.rl"
#line 120 "ansi_parser.cpp" #line 120 "ansi_parser.cpp"
{ {
@ -267,8 +267,8 @@ _match:
case 8: case 8:
#line 56 "ansi_parser.rl" #line 56 "ansi_parser.rl"
{ {
color = {100,100,100}; // UNDO THIS color = $default_bg;
bgcolor = $default_bg; // THIS TOO bgcolor = $default_fg;
color_cb(color, bgcolor); color_cb(color, bgcolor);
} }
break; break;
@ -283,29 +283,31 @@ _match:
case 10: case 10:
#line 66 "ansi_parser.rl" #line 66 "ansi_parser.rl"
{ {
color = sf::Color(100,100,100);
color_cb(color, bgcolor);
} }
break; break;
case 11: case 11:
#line 69 "ansi_parser.rl" #line 71 "ansi_parser.rl"
{ target.r = value; } { target.r = value; }
break; break;
case 12: case 12:
#line 70 "ansi_parser.rl" #line 72 "ansi_parser.rl"
{ target.g = value; } { target.g = value; }
break; break;
case 13: case 13:
#line 71 "ansi_parser.rl" #line 73 "ansi_parser.rl"
{ target.b = value; } { target.b = value; }
break; break;
case 14: case 14:
#line 72 "ansi_parser.rl" #line 74 "ansi_parser.rl"
{ value = 0; } { value = 0; }
break; break;
case 15: case 15:
#line 73 "ansi_parser.rl" #line 75 "ansi_parser.rl"
{} {}
break; break;
#line 279 "ansi_parser.cpp" #line 281 "ansi_parser.cpp"
} }
} }
@ -322,10 +324,10 @@ _again:
while ( __nacts-- > 0 ) { while ( __nacts-- > 0 ) {
switch ( *__acts++ ) { switch ( *__acts++ ) {
case 15: case 15:
#line 73 "ansi_parser.rl" #line 75 "ansi_parser.rl"
{} {}
break; break;
#line 297 "ansi_parser.cpp" #line 299 "ansi_parser.cpp"
} }
} }
} }
@ -333,7 +335,7 @@ _again:
_out: {} _out: {}
} }
#line 134 "ansi_parser.rl" #line 136 "ansi_parser.rl"
bool good = p - pe == 0; bool good = p - pe == 0;

@ -54,8 +54,8 @@ using namespace fmt;
action reset_fg { color = $default_fg; } action reset_fg { color = $default_fg; }
action reset_bg { bgcolor = $default_bg; } action reset_bg { bgcolor = $default_bg; }
action invert { action invert {
color = {100,100,100}; // UNDO THIS color = $default_bg;
bgcolor = $default_bg; // THIS TOO bgcolor = $default_fg;
color_cb(color, bgcolor); color_cb(color, bgcolor);
} }
action reset_invert { action reset_invert {
@ -64,6 +64,8 @@ using namespace fmt;
color_cb(color, bgcolor); color_cb(color, bgcolor);
} }
action half_bright { action half_bright {
color = sf::Color(100,100,100);
color_cb(color, bgcolor);
} }
action red { target.r = value; } action red { target.r = value; }

@ -43,7 +43,7 @@ SFMLRender::SFMLRender() :
$ui_text.setPosition(0,0); $ui_text.setPosition(0,0);
$ui_text.setCharacterSize(UI_FONT_SIZE); $ui_text.setCharacterSize(UI_FONT_SIZE);
$ui_text.setFillColor(color(Value::LIGHT_MID)); $ui_text.setFillColor(color(Value::LIGHT_MID));
sf::Glyph glyph = $font.getGlyph(L'', UI_FONT_SIZE, false); sf::Glyph glyph = $font.getGlyph(UI_BASE_CHAR, UI_FONT_SIZE, false);
$ui_bounds = glyph.bounds; $ui_bounds = glyph.bounds;
} }
@ -148,11 +148,22 @@ void SFMLRender::render_grid(const std::wstring &text, float x, float y) {
}); });
} }
inline sf::FloatRect draw_chunk(sf::RenderWindow& window, sf::Text& text, float x, float y, std::wstring &out) { inline sf::FloatRect draw_chunk(sf::RenderWindow& window,
sf::FloatRect ui_bounds, sf::Text& text,
sf::Color bgcolor, float x, float y, std::wstring &out)
{
text.setString(out); text.setString(out);
text.setPosition(x, y); text.setPosition({x, y});
sf::FloatRect bounds = text.getLocalBounds(); // get a base character for the cell size
// need left,top,width,height for box to be accurate sf::FloatRect bounds(x, y, ui_bounds.width * out.size(), ui_bounds.height);
sf::RectangleShape backing({bounds.width, bounds.height});
backing.setFillColor(bgcolor);
backing.setPosition({bounds.left, bounds.top + BG_BOX_OFFSET});
#ifdef DEBUG
backing.setOutlineColor(int(y) % 4 ? sf::Color::Red : sf::Color::Yellow);
backing.setOutlineThickness(1);
#endif
window.draw(backing);
window.draw(text); window.draw(text);
out.clear(); out.clear();
return bounds; return bounds;
@ -162,10 +173,7 @@ void SFMLRender::render_text(const std::wstring &text, float start_x, float star
std::wstring out; std::wstring out;
float x = start_x; float x = start_x;
float y = start_y; float y = start_y;
enum State { START=1, BUILDING=2 }; sf::Color bgcolor = $default_bg;
enum Event { NL=1, CHAR=3, SKIP=5 };
State state = START;
Event event = CHAR;
// 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);
@ -173,42 +181,38 @@ void SFMLRender::render_text(const std::wstring &text, float start_x, float star
$ansi.parse(text, $ansi.parse(text,
[&](sf::Color fg, sf::Color bg){ [&](sf::Color fg, sf::Color bg){
if(out.size() > 0 ) { if(out.size() > 0 ) {
auto bounds = draw_chunk($window, $ui_text, x, y, out); auto bounds = draw_chunk($window, $ui_bounds, $ui_text, bgcolor, x, y, out);
x += bounds.width; x += bounds.width;
} }
bgcolor = bg;
$ui_text.setFillColor(fg); $ui_text.setFillColor(fg);
}, },
[&](wchar_t tile) { [&](wchar_t tile) {
if(tile == '\r') event = SKIP; switch(tile) {
else if(tile == '\n') event = NL; case '\r': break; // ignore it
else event = CHAR; case '\n': {
sf::FloatRect bounds;
switch(event) {
case SKIP: break; // ignore it if(out.size() > 0) {
case NL: { bounds = draw_chunk($window, $ui_bounds, $ui_text, bgcolor, x, y, out);
if(state == START) { } else {
state = BUILDING; bounds = $ui_text.getLocalBounds();
} else if(state == BUILDING) {
sf::FloatRect bounds;
if(out.size() > 0) {
bounds = draw_chunk($window, $ui_text, x, y, out);
} else {
bounds = $ui_text.getLocalBounds();
}
y += bounds.height;
x = start_x; // reset to the original position
} }
y += bounds.height;
x = start_x; // reset to the original position
} }
break; break;
case CHAR: default:
state = BUILDING;
out += tile; out += tile;
break; break;
} }
} }
); );
if(out.size() > 0) {
draw_chunk($window, $ui_bounds, $ui_text, bgcolor, x, y, out);
}
} }
void SFMLRender::draw_text(Panel &panel, bool with_border) { void SFMLRender::draw_text(Panel &panel, bool with_border) {

@ -13,15 +13,16 @@
using ftxui::Canvas, ftxui::Screen; using ftxui::Canvas, ftxui::Screen;
#define BG_TILE L'█' const int VIDEO_X = 1600;
const int VIDEO_Y = 900;
constexpr int VIDEO_X = 1600; const int MIN_FONT_SIZE = 20;
constexpr int VIDEO_Y = 900; const int MAX_FONT_SIZE = 140;
constexpr int MIN_FONT_SIZE = 20; const int GAME_MAP_POS = 600;
constexpr int MAX_FONT_SIZE = 140; const int UI_FONT_SIZE=30;
constexpr int GAME_MAP_POS = 600; const int BASE_MAP_FONT_SIZE=90;
constexpr int UI_FONT_SIZE=30; const wchar_t BG_TILE = L'';
constexpr int BASE_MAP_FONT_SIZE=90; const wchar_t UI_BASE_CHAR = L'';
const int BG_BOX_OFFSET=5;
enum class Value { enum class Value {
BLACK=0, DARK_DARK, DARK_MID, BLACK=0, DARK_DARK, DARK_MID,

Loading…
Cancel
Save