Tried to set the background color in the ftxui canvas and weirdly it started doing almost what I want with lighting, but I didn't write any code to do that. There's some bug in how I'm doing it that's causing it to set the colors...correctly. Must find out why.

This commit is contained in:
Zed A. Shaw 2024-11-23 23:11:20 -05:00
parent fb1fd9d8bc
commit 1bb04b4562
5 changed files with 55 additions and 13 deletions

View file

@ -9,6 +9,8 @@
#include "ftxui/screen/terminal.hpp" // for SetColorSupport, Color, TrueColor
#include "dbc.hpp"
const bool DEBUG_MAP=false;
using std::string;
using namespace fmt;
using namespace components;
@ -132,6 +134,8 @@ 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) {
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
// BUG: this color is a made up BS color for seeing entities until I can work on them
canvas.DrawText(loc.x*2, loc.y*4, tile.chr, Color::RGB(255, 50, 50));
}
});
@ -142,7 +146,8 @@ void System::draw_map(DinkyECS::World &world, Map &game_map, ftxui::Canvas &canv
const auto& player = world.get_the<Player>();
const auto& player_position = world.get<Position>(player.entity);
Point start = game_map.center_camera(player_position.location, view_x, view_y);
Matrix &walls = game_map.walls();
auto &walls = game_map.walls();
auto &paths = game_map.paths();
size_t end_x = std::min(view_x, game_map.width() - start.x);
size_t end_y = std::min(view_y, game_map.height() - start.y);
@ -151,10 +156,30 @@ void System::draw_map(DinkyECS::World &world, Map &game_map, ftxui::Canvas &canv
for(size_t y = 0; y < end_y; ++y) {
string tile = walls[start.y+y][start.x+x] == 1 ? config.WALL_TILE : config.FLOOR_TILE;
// the 2 and 4 are from ftxui::Canvas since it does a kind of "subpixel" drawing
// LIGHT: if tile is in light then color ++ otherwise --
// LIGHT: is put into the/a collision map and if a cell is a light's neighbor
// it gets brighter.
const int LIGHT_MIN = 30;
const int LIGHT_MAX = 180;
Point light_at{start.x+x, start.y+y};
int dnum = paths[light_at.y][light_at.x];
int light_value = std::clamp(255 - (dnum * 75), LIGHT_MIN, LIGHT_MAX);
if(tile == config.WALL_TILE) {
canvas.DrawText(x * 2, y * 4, tile, Color::RGB(70, 70, 70));
canvas.DrawText(x * 2, y * 4, tile, Color::HSV(230, 20, 20));
} else if(DEBUG_MAP) {
string num = format("{:x}", dnum);
num = num.size() > 2 ? "*" : num;
canvas.DrawText(x * 2, y * 4, num, Color::HSV(dnum * 20, 150, light_value));
} else {
canvas.DrawText(x * 2, y * 4, tile, Color::RGB(20, 20, 20));
// floor tile or similar
// BUG: no idea why this works but this actually implements decent light
canvas.DrawText(x * 2, y * 4, tile, [&, light_value](auto &pixel) {
pixel.foreground_color = Color::HSV(30, light_value / 2, light_value);
pixel.background_color = Color::HSV(30, 20, light_value);
});
}
}
}