Rendering with color is working now but still has problems with enabling/resetting the default colors.

main
Zed A. Shaw 3 weeks ago
parent a36b187879
commit e864e14eab
  1. 58
      ansi_parser.cpp
  2. 10
      ansi_parser.rl
  3. 5
      main.cpp
  4. 5
      map.hpp
  5. 4
      render.cpp
  6. 2
      render.hpp
  7. 9
      status.txt
  8. 9
      systems.cpp

@ -1,5 +1,5 @@
#line 1 ".\\ansi_parser.rl" #line 1 "ansi_parser.rl"
#include <fmt/core.h> #include <fmt/core.h>
#include <string_view> #include <string_view>
#include <charconv> #include <charconv>
@ -10,11 +10,11 @@
using namespace fmt; using namespace fmt;
#line 77 ".\\ansi_parser.rl" #line 81 "ansi_parser.rl"
#line 13 ".\\ansi_parser.cpp" #line 13 "ansi_parser.cpp"
static const char _foo_actions[] = { static const char _foo_actions[] = {
0, 1, 0, 1, 3, 1, 4, 1, 0, 1, 0, 1, 3, 1, 4, 1,
5, 1, 6, 1, 7, 1, 11, 1, 5, 1, 6, 1, 7, 1, 11, 1,
@ -85,7 +85,7 @@ static const int foo_error = 0;
static const int foo_en_main = 19; static const int foo_en_main = 19;
#line 80 ".\\ansi_parser.rl" #line 84 "ansi_parser.rl"
bool parse_ansi(std::wstring_view codes, sf::Color default_fg, sf::Color default_bg, WriteCB write) { bool parse_ansi(std::wstring_view codes, sf::Color default_fg, sf::Color default_bg, WriteCB write) {
const wchar_t *start = NULL; const wchar_t *start = NULL;
@ -96,17 +96,17 @@ bool parse_ansi(std::wstring_view codes, sf::Color default_fg, sf::Color default
const wchar_t *eof = pe; const wchar_t *eof = pe;
sf::Color bgcolor(default_bg); sf::Color bgcolor(default_bg);
sf::Color color(default_fg); sf::Color color(default_fg);
sf::Color &target = bgcolor; sf::Color &target = color;
#line 94 ".\\ansi_parser.cpp" #line 94 "ansi_parser.cpp"
{ {
cs = foo_start; cs = foo_start;
} }
#line 93 ".\\ansi_parser.rl" #line 97 "ansi_parser.rl"
#line 97 ".\\ansi_parser.cpp" #line 97 "ansi_parser.cpp"
{ {
int _klen; int _klen;
unsigned int _trans; unsigned int _trans;
@ -180,13 +180,13 @@ _match:
switch ( *_acts++ ) switch ( *_acts++ )
{ {
case 0: case 0:
#line 14 ".\\ansi_parser.rl" #line 14 "ansi_parser.rl"
{ {
start = p; start = p;
} }
break; break;
case 1: case 1:
#line 18 ".\\ansi_parser.rl" #line 18 "ansi_parser.rl"
{ {
value = 0; value = 0;
size_t len = p - start; size_t len = p - start;
@ -210,52 +210,56 @@ _match:
} }
break; break;
case 2: case 2:
#line 40 ".\\ansi_parser.rl" #line 40 "ansi_parser.rl"
{ } { }
break; break;
case 3: case 3:
#line 41 ".\\ansi_parser.rl" #line 41 "ansi_parser.rl"
{ target = color; } {
target = color;
}
break; break;
case 4: case 4:
#line 42 ".\\ansi_parser.rl" #line 44 "ansi_parser.rl"
{ target = bgcolor; } {
target = bgcolor;
}
break; break;
case 5: case 5:
#line 44 ".\\ansi_parser.rl" #line 48 "ansi_parser.rl"
{ {
write(bgcolor, color, (*p)); write(bgcolor, color, (*p));
} }
break; break;
case 6: case 6:
#line 48 ".\\ansi_parser.rl" #line 52 "ansi_parser.rl"
{ color = default_fg; } { color = default_fg; }
break; break;
case 7: case 7:
#line 49 ".\\ansi_parser.rl" #line 53 "ansi_parser.rl"
{ bgcolor = default_bg; } { bgcolor = default_bg; }
break; break;
case 8: case 8:
#line 51 ".\\ansi_parser.rl" #line 55 "ansi_parser.rl"
{ target.r = value; } { target.r = value; }
break; break;
case 9: case 9:
#line 52 ".\\ansi_parser.rl" #line 56 "ansi_parser.rl"
{ target.g = value; } { target.g = value; }
break; break;
case 10: case 10:
#line 53 ".\\ansi_parser.rl" #line 57 "ansi_parser.rl"
{ target.b = value; } { target.b = value; }
break; break;
case 11: case 11:
#line 54 ".\\ansi_parser.rl" #line 58 "ansi_parser.rl"
{ value = 0; } { value = 0; }
break; break;
case 12: case 12:
#line 55 ".\\ansi_parser.rl" #line 59 "ansi_parser.rl"
{ } { }
break; break;
#line 232 ".\\ansi_parser.cpp" #line 236 "ansi_parser.cpp"
} }
} }
@ -272,10 +276,10 @@ _again:
while ( __nacts-- > 0 ) { while ( __nacts-- > 0 ) {
switch ( *__acts++ ) { switch ( *__acts++ ) {
case 12: case 12:
#line 55 ".\\ansi_parser.rl" #line 59 "ansi_parser.rl"
{ } { }
break; break;
#line 250 ".\\ansi_parser.cpp" #line 254 "ansi_parser.cpp"
} }
} }
} }
@ -283,7 +287,7 @@ _again:
_out: {} _out: {}
} }
#line 94 ".\\ansi_parser.rl" #line 98 "ansi_parser.rl"
return p - pe == 0; return p - pe == 0;
} }

@ -38,8 +38,12 @@ using namespace fmt;
} }
action color24b { } action color24b { }
action is_fg { target = color; } action is_fg {
action is_bg { target = bgcolor; } target = color;
}
action is_bg {
target = bgcolor;
}
action out { action out {
write(bgcolor, color, fc); write(bgcolor, color, fc);
@ -87,7 +91,7 @@ bool parse_ansi(std::wstring_view codes, sf::Color default_fg, sf::Color default
const wchar_t *eof = pe; const wchar_t *eof = pe;
sf::Color bgcolor(default_bg); sf::Color bgcolor(default_bg);
sf::Color color(default_fg); sf::Color color(default_fg);
sf::Color &target = bgcolor; sf::Color &target = color;
%% write init; %% write init;
%% write exec; %% write exec;

@ -7,6 +7,10 @@
#include "collider.hpp" #include "collider.hpp"
#include "render.hpp" #include "render.hpp"
#include "ftxui/screen/terminal.hpp" // for SetColorSupport, Color, TrueColor
using namespace ftxui;
/* /*
* This needs to be turned into a real world generator * This needs to be turned into a real world generator
* system. * system.
@ -47,6 +51,7 @@ void configure_world(DinkyECS::World &world, Map &game_map) {
int main() { int main() {
Terminal::SetColorSupport(Terminal::Color::TrueColor);
DinkyECS::World world; DinkyECS::World world;
Map game_map(GAME_MAP_X, GAME_MAP_Y); Map game_map(GAME_MAP_X, GAME_MAP_Y);
game_map.generate(); game_map.generate();

@ -12,9 +12,10 @@
#define WALL_VALUE 1 #define WALL_VALUE 1
#define SPACE_VALUE 0 #define SPACE_VALUE 0
#define WALL_TILE "█" #define WALL_TILE "█"
#define FLOOR_TILE "." #define FLOOR_TILE "\u2849"
#define PLAYER_TILE "" #define PLAYER_TILE ""
#define ENEMY_TILE "Ω" #define ENEMY_TILE "Ω"
#define BG_TILE L'█'
struct Room { struct Room {
size_t x = 0; size_t x = 0;

@ -59,7 +59,7 @@ bool SFMLRender::resize_map(int new_size) {
if(MIN_FONT_SIZE < new_size && new_size < MAX_FONT_SIZE) { if(MIN_FONT_SIZE < new_size && new_size < MAX_FONT_SIZE) {
$sprites.clear(); // need to reset the sprites for the new size $sprites.clear(); // need to reset the sprites for the new size
$map_font_size = new_size; $map_font_size = new_size;
$base_glyph = $font.getGlyph(L'', $map_font_size, false); $base_glyph = $font.getGlyph(BG_TILE, $map_font_size, false);
$line_spacing = $font.getLineSpacing($map_font_size); $line_spacing = $font.getLineSpacing($map_font_size);
return true; return true;
} else { } else {
@ -85,7 +85,7 @@ void SFMLRender::draw_screen(bool clear, float map_off_x, float map_off_y) {
float y = 0.0f; float y = 0.0f;
float x = GAME_MAP_POS; float x = GAME_MAP_POS;
// make a copy so we don't modify the cached one // make a copy so we don't modify the cached one
auto bg_sprite = get_text_sprite(L''); auto bg_sprite = get_text_sprite(BG_TILE);
auto bg_bounds = bg_sprite.getLocalBounds(); auto bg_bounds = bg_sprite.getLocalBounds();
sf::Color def_fg(color(Value::MID)); sf::Color def_fg(color(Value::MID));
sf::Color def_bg(color(Value::BLACK)); sf::Color def_bg(color(Value::BLACK));

@ -10,6 +10,8 @@
using ftxui::Canvas, ftxui::Screen; using ftxui::Canvas, ftxui::Screen;
#define BG_TILE L'█'
constexpr int VIDEO_X = 1600; constexpr int VIDEO_X = 1600;
constexpr int VIDEO_Y = 900; constexpr int VIDEO_Y = 900;
constexpr int MIN_FONT_SIZE = 20; constexpr int MIN_FONT_SIZE = 20;

@ -4,18 +4,9 @@ NOTES:
* src/ftxui/screen/color.cpp * src/ftxui/screen/color.cpp
* Just search for ugrep x1B * Just search for ugrep x1B
* https://man7.org/linux/man-pages/man4/console_codes.4.html * https://man7.org/linux/man-pages/man4/console_codes.4.html
* amit note:
Struct A {
int data;
template <typename T> A(T t) : data(static_cast<int>(t)) {}
};
A a(my_enum);
TODO: TODO:
* Actually render FTXUI ansi output instead of the gui.cpp hack.
* Rewrite collider to return a real struct not tuple.
* Write a test that generates a ton of maps then confirms there's a path from one room to every other room? * Write a test that generates a ton of maps then confirms there's a path from one room to every other room?
* Lua integration? * Lua integration?

@ -6,6 +6,7 @@
#include "collider.hpp" #include "collider.hpp"
#include "events.hpp" #include "events.hpp"
#include "ftxui/screen/color.hpp" #include "ftxui/screen/color.hpp"
#include "ftxui/screen/terminal.hpp" // for SetColorSupport, Color, TrueColor
using std::string; using std::string;
using namespace fmt; using namespace fmt;
@ -126,7 +127,7 @@ void System::draw_entities(DinkyECS::World &world, Map &game_map, ftxui::Canvas
&& pos.location.y >= cam_orig.y && pos.location.y <= cam_orig.y + view_y) { && pos.location.y >= cam_orig.y && pos.location.y <= cam_orig.y + view_y) {
Point loc = game_map.map_to_camera(pos.location, cam_orig); Point loc = game_map.map_to_camera(pos.location, cam_orig);
// the 2 and 4 are from ftxui::Canvas since it does a kind of "subpixel" drawing // the 2 and 4 are from ftxui::Canvas since it does a kind of "subpixel" drawing
canvas.DrawText(loc.x*2, loc.y*4, tile.chr); canvas.DrawText(loc.x*2, loc.y*4, tile.chr, Color::RGB(255, 50, 50));
} }
}); });
} }
@ -144,7 +145,11 @@ void System::draw_map(DinkyECS::World &world, Map &game_map, ftxui::Canvas &canv
for(size_t y = 0; y < end_y; ++y) { for(size_t y = 0; y < end_y; ++y) {
string tile = walls[start.y+y][start.x+x] == 1 ? WALL_TILE : FLOOR_TILE; string tile = walls[start.y+y][start.x+x] == 1 ? WALL_TILE : FLOOR_TILE;
// the 2 and 4 are from ftxui::Canvas since it does a kind of "subpixel" drawing // the 2 and 4 are from ftxui::Canvas since it does a kind of "subpixel" drawing
canvas.DrawText(x * 2, y * 4, tile, Color::HSV(100, 10, 10)); if(tile == WALL_TILE) {
canvas.DrawText(x * 2, y * 4, tile, Color::RGB(70, 70, 70));
} else {
canvas.DrawText(x * 2, y * 4, tile, Color::RGB(20, 20, 20));
}
} }
} }

Loading…
Cancel
Save