diff --git a/gui.cpp b/gui.cpp index 070f05c..ec2a44a 100644 --- a/gui.cpp +++ b/gui.cpp @@ -6,73 +6,11 @@ #include "components.hpp" #include #include "systems.hpp" +#include "map_view.hpp" using namespace components; 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(); - const auto& player = $level.world->get_the(); - const auto& player_position = $level.world->get(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() : $window(sf::VideoMode({SCREEN_WIDTH, SCREEN_HEIGHT}), "Zed's Raycaster Thing"), $renderer($window), @@ -111,8 +49,8 @@ namespace gui { $renderer.init_terminal(); $map_view.create_render(); - $renderer.resize_grid(MAX_FONT_SIZE, $map_view); $map_view.resize_canvas(); + $renderer.resize_grid(MAX_FONT_SIZE, $map_view); state(State::IDLE); } diff --git a/gui.hpp b/gui.hpp index f1cba24..d034c7c 100644 --- a/gui.hpp +++ b/gui.hpp @@ -6,25 +6,9 @@ #include "camera.hpp" #include "fsm.hpp" #include "render.hpp" -#include "panel.hpp" -#include - -using ftxui::Canvas; +#include "map_view.hpp" 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 { START, MOVING, diff --git a/map_view.cpp b/map_view.cpp new file mode 100644 index 0000000..6c65823 --- /dev/null +++ b/map_view.cpp @@ -0,0 +1,70 @@ +#include "map_view.hpp" +#include +#include +#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(); + const auto& player = $level.world->get_the(); + const auto& player_position = $level.world->get(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); + } +} diff --git a/map_view.hpp b/map_view.hpp new file mode 100644 index 0000000..d144784 --- /dev/null +++ b/map_view.hpp @@ -0,0 +1,20 @@ +#pragma once +#include "panel.hpp" +#include +#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); + }; +} diff --git a/meson.build b/meson.build index 0e7f443..5d8ca52 100644 --- a/meson.build +++ b/meson.build @@ -56,6 +56,7 @@ sources = [ 'levelmanager.cpp', 'lights.cpp', 'map.cpp', + 'map_view.cpp', 'matrix.cpp', 'matrix.cpp', 'panel.cpp',