Clean up the GUI some by moving the map_view out into its own file.

master
Zed A. Shaw 4 weeks ago
parent 421cca308b
commit 6da830595c
  1. 66
      gui.cpp
  2. 18
      gui.hpp
  3. 70
      map_view.cpp
  4. 20
      map_view.hpp
  5. 1
      meson.build

@ -6,73 +6,11 @@
#include "components.hpp" #include "components.hpp"
#include <numbers> #include <numbers>
#include "systems.hpp" #include "systems.hpp"
#include "map_view.hpp"
using namespace components; using namespace components;
namespace gui { namespace gui {
using ftxui::Color;
MapViewUI::MapViewUI(GameLevel &level) :
Panel(RAY_VIEW_X, 0, 0, 0, true),
$level(level)
{}
void MapViewUI::update_level(GameLevel &level) {
$level = level;
}
void MapViewUI::draw_map() {
const auto& debug = $level.world->get_the<Debug>();
const auto& player = $level.world->get_the<Player>();
const auto& player_position = $level.world->get<Position>(player.entity);
Point start = $level.map->center_camera(player_position.location, width, height);
auto &tiles = $level.map->tiles();
auto &paths = $level.map->paths();
auto &lighting = $level.lights->lighting();
// WARN: this is exploiting that -1 in size_t becomes largest
size_t end_x = std::min(size_t(width), $level.map->width() - start.x);
size_t end_y = std::min(size_t(height), $level.map->height() - start.y);
for(size_t y = 0; y < end_y; ++y) {
for(size_t x = 0; x < end_x; ++x)
{
const Tile& tile = tiles.at(start.x+x, start.y+y);
// light value is an integer that's a percent
float light_value = debug.LIGHT ? 80 * PERCENT : lighting[start.y+y][start.x+x] * PERCENT;
int dnum = debug.PATHS ? paths[start.y+y][start.x+x] : WALL_PATH_LIMIT;
if(debug.PATHS && dnum != WALL_PATH_LIMIT) {
string num = dnum > 15 ? "*" : fmt::format("{:x}", dnum);
$canvas.DrawText(x * 2, y * 4, num, [dnum, tile, light_value](auto &pixel) {
pixel.foreground_color = Color::HSV(dnum * 20, 150, 200);
pixel.background_color = Color::HSV(30, 20, tile.foreground[2] * 50 * PERCENT);
});
} else {
$canvas.DrawText(x * 2, y * 4, tile.display, [tile, light_value](auto &pixel) {
pixel.foreground_color = Color::HSV(tile.foreground[0], tile.foreground[1], tile.foreground[2] * light_value);
pixel.background_color = Color::HSV(tile.background[0], tile.background[1], tile.background[2] * light_value);
});
}
}
}
System::draw_entities(*$level.world, *$level.map, lighting, $canvas, start, width, height);
}
void MapViewUI::create_render() {
set_renderer(Renderer([&] {
draw_map();
return canvas($canvas);
}));
}
void MapViewUI::resize_canvas() {
// set canvas to best size
$canvas = Canvas(width * 2, height * 4);
}
FSM::FSM() : FSM::FSM() :
$window(sf::VideoMode({SCREEN_WIDTH, SCREEN_HEIGHT}), "Zed's Raycaster Thing"), $window(sf::VideoMode({SCREEN_WIDTH, SCREEN_HEIGHT}), "Zed's Raycaster Thing"),
$renderer($window), $renderer($window),
@ -111,8 +49,8 @@ namespace gui {
$renderer.init_terminal(); $renderer.init_terminal();
$map_view.create_render(); $map_view.create_render();
$renderer.resize_grid(MAX_FONT_SIZE, $map_view);
$map_view.resize_canvas(); $map_view.resize_canvas();
$renderer.resize_grid(MAX_FONT_SIZE, $map_view);
state(State::IDLE); state(State::IDLE);
} }

@ -6,25 +6,9 @@
#include "camera.hpp" #include "camera.hpp"
#include "fsm.hpp" #include "fsm.hpp"
#include "render.hpp" #include "render.hpp"
#include "panel.hpp" #include "map_view.hpp"
#include <ftxui/dom/canvas.hpp>
using ftxui::Canvas;
namespace gui { namespace gui {
class MapViewUI : public Panel {
public:
Canvas $canvas;
GameLevel $level;
MapViewUI(GameLevel &level);
void create_render();
void resize_canvas();
void draw_map();
void update_level(GameLevel &level);
};
enum class State { enum class State {
START, START,
MOVING, MOVING,

@ -0,0 +1,70 @@
#include "map_view.hpp"
#include <functional>
#include <string>
#include "systems.hpp"
namespace gui {
using namespace components;
using ftxui::Color;
MapViewUI::MapViewUI(GameLevel &level) :
Panel(RAY_VIEW_X, 0, 0, 0, true),
$level(level)
{}
void MapViewUI::update_level(GameLevel &level) {
$level = level;
}
void MapViewUI::draw_map() {
const auto& debug = $level.world->get_the<Debug>();
const auto& player = $level.world->get_the<Player>();
const auto& player_position = $level.world->get<Position>(player.entity);
Point start = $level.map->center_camera(player_position.location, width, height);
auto &tiles = $level.map->tiles();
auto &paths = $level.map->paths();
auto &lighting = $level.lights->lighting();
// WARN: this is exploiting that -1 in size_t becomes largest
size_t end_x = std::min(size_t(width), $level.map->width() - start.x);
size_t end_y = std::min(size_t(height), $level.map->height() - start.y);
for(size_t y = 0; y < end_y; ++y) {
for(size_t x = 0; x < end_x; ++x)
{
const Tile& tile = tiles.at(start.x+x, start.y+y);
// light value is an integer that's a percent
float light_value = debug.LIGHT ? 80 * PERCENT : lighting[start.y+y][start.x+x] * PERCENT;
int dnum = debug.PATHS ? paths[start.y+y][start.x+x] : WALL_PATH_LIMIT;
if(debug.PATHS && dnum != WALL_PATH_LIMIT) {
string num = dnum > 15 ? "*" : fmt::format("{:x}", dnum);
$canvas.DrawText(x * 2, y * 4, num, [dnum, tile, light_value](auto &pixel) {
pixel.foreground_color = Color::HSV(dnum * 20, 150, 200);
pixel.background_color = Color::HSV(30, 20, tile.foreground[2] * 50 * PERCENT);
});
} else {
$canvas.DrawText(x * 2, y * 4, tile.display, [tile, light_value](auto &pixel) {
pixel.foreground_color = Color::HSV(tile.foreground[0], tile.foreground[1], tile.foreground[2] * light_value);
pixel.background_color = Color::HSV(tile.background[0], tile.background[1], tile.background[2] * light_value);
});
}
}
}
System::draw_entities(*$level.world, *$level.map, lighting, $canvas, start, width, height);
}
void MapViewUI::create_render() {
set_renderer(Renderer([&] {
draw_map();
return canvas($canvas);
}));
}
void MapViewUI::resize_canvas() {
// set canvas to best size
$canvas = Canvas(width * 2, height * 4);
}
}

@ -0,0 +1,20 @@
#pragma once
#include "panel.hpp"
#include <ftxui/dom/canvas.hpp>
#include "levelmanager.hpp"
using ftxui::Canvas;
namespace gui {
class MapViewUI : public Panel {
public:
Canvas $canvas;
GameLevel $level;
MapViewUI(GameLevel &level);
void create_render();
void resize_canvas();
void draw_map();
void update_level(GameLevel &level);
};
}

@ -56,6 +56,7 @@ sources = [
'levelmanager.cpp', 'levelmanager.cpp',
'lights.cpp', 'lights.cpp',
'map.cpp', 'map.cpp',
'map_view.cpp',
'matrix.cpp', 'matrix.cpp',
'matrix.cpp', 'matrix.cpp',
'panel.cpp', 'panel.cpp',

Loading…
Cancel
Save