From 9b3b81683ad8688210ad9ebdcc812eac834008a4 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Thu, 13 Feb 2025 10:55:45 -0500 Subject: [PATCH] Separate out the major UIs to get ready for their development, and enable debug button. --- color.hpp | 16 ++++++------ combat_ui.cpp | 35 +++++++++++++++++++++++++ combat_ui.hpp | 18 +++++++++++++ components.hpp | 1 + constants.hpp | 10 +++++-- gui.cpp | 71 +++++++++----------------------------------------- gui.hpp | 36 +++---------------------- meson.build | 2 ++ raycaster.cpp | 19 ++++---------- status_ui.cpp | 48 ++++++++++++++++++++++++++++++++++ status_ui.hpp | 14 ++++++++++ 11 files changed, 154 insertions(+), 116 deletions(-) create mode 100644 combat_ui.cpp create mode 100644 combat_ui.hpp create mode 100644 status_ui.cpp create mode 100644 status_ui.hpp diff --git a/color.hpp b/color.hpp index 204c135..4443c95 100644 --- a/color.hpp +++ b/color.hpp @@ -1,14 +1,14 @@ #pragma once namespace ColorValue { - const sf::Color BLACK{1, 4, 2}; - const sf::Color DARK_DARK{9, 29, 16}; - const sf::Color DARK_MID{14, 50, 26}; - const sf::Color DARK_LIGHT{0, 109, 44}; - const sf::Color MID{63, 171, 92}; - const sf::Color LIGHT_DARK{161, 217, 155}; - const sf::Color LIGHT_MID{199, 233, 192}; - const sf::Color LIGHT_LIGHT{229, 245, 224}; + const sf::Color BLACK{0, 0, 0}; + const sf::Color DARK_DARK{10, 10, 10}; + const sf::Color DARK_MID{30, 30, 30}; + const sf::Color DARK_LIGHT{60, 60, 60}; + const sf::Color MID{100, 100, 100}; + const sf::Color LIGHT_DARK{150, 150, 150}; + const sf::Color LIGHT_MID{200, 200, 200}; + const sf::Color LIGHT_LIGHT{230, 230, 230}; const sf::Color WHITE{255, 255, 255}; const sf::Color TRANSPARENT = sf::Color::Transparent; } diff --git a/combat_ui.cpp b/combat_ui.cpp new file mode 100644 index 0000000..436d2c8 --- /dev/null +++ b/combat_ui.cpp @@ -0,0 +1,35 @@ +#include "combat_ui.hpp" +#include // for Render +#include // for ftxui +#include +#include +#include +#include "constants.hpp" +#include "color.hpp" + +namespace gui { + using namespace ftxui; + + CombatUI::CombatUI(GameLevel level) : + Panel(COMBAT_UI_X, COMBAT_UI_Y, COMBAT_UI_WIDTH, COMBAT_UI_HEIGHT, false), + $level(level) + { + default_bg = {0,0,0}; + } + + void CombatUI::create_render() { + $attack1_button = Button("ATTACK1", []{ fmt::println("ATTACK1 clicked"); }); + $attack2_button = Button("ATTACK2", []{ fmt::println("ATTACK2 clicked"); }); + + auto combat_rend = Renderer([&]{ + return hbox({ + $attack1_button->Render(), + $attack2_button->Render() + }); + }); + + set_renderer(combat_rend); + add($attack1_button); + add($attack2_button); + } +} diff --git a/combat_ui.hpp b/combat_ui.hpp new file mode 100644 index 0000000..7759fe6 --- /dev/null +++ b/combat_ui.hpp @@ -0,0 +1,18 @@ +#pragma once +#include "panel.hpp" +#include "levelmanager.hpp" +#include + +namespace gui { + class CombatUI : public Panel { + public: + GameLevel $level; + Component $attack1_button; + Component $attack2_button; + + CombatUI(GameLevel level); + + void create_render(); + void update_level(GameLevel &level) { $level = level; } + }; +} diff --git a/components.hpp b/components.hpp index 603485c..af1ec91 100644 --- a/components.hpp +++ b/components.hpp @@ -45,6 +45,7 @@ namespace components { struct Debug { bool PATHS=false; bool LIGHT=false; + bool FPS=false; }; struct Weapon { diff --git a/constants.hpp b/constants.hpp index 9f4172f..c6725b6 100644 --- a/constants.hpp +++ b/constants.hpp @@ -37,9 +37,15 @@ constexpr int BASE_MAP_FONT_SIZE=90; constexpr int GAME_MAP_PIXEL_POS = 600; constexpr int MAX_FONT_SIZE = 140; constexpr int MIN_FONT_SIZE = 20; -constexpr int STATUS_UI_WIDTH = 40; -constexpr int STATUS_UI_HEIGHT = 30; +constexpr int STATUS_UI_WIDTH = 29; +constexpr int STATUS_UI_HEIGHT = 25; +constexpr int STATUS_UI_X = 43; +constexpr int STATUS_UI_Y = 200; constexpr float PERCENT = 0.01f; +constexpr int COMBAT_UI_WIDTH = 89; +constexpr int COMBAT_UI_HEIGHT = 6; +constexpr int COMBAT_UI_X = RAY_VIEW_X; +constexpr int COMBAT_UI_Y = RAY_VIEW_HEIGHT; // for the panels/renderer diff --git a/gui.cpp b/gui.cpp index b2ab234..8ca9d0e 100644 --- a/gui.cpp +++ b/gui.cpp @@ -6,62 +6,10 @@ #include "components.hpp" #include #include "systems.hpp" -#include "map_view.hpp" -#include // for Render -#include // for ftxui -#include -#include -#include -using namespace components; -using namespace ftxui; namespace gui { - - void StatusUI::create_render() { - auto player = $level.world->get_the(); - - auto status_rend = Renderer([&, player]{ - const auto& player_combat = $level.world->get(player.entity); - const auto& combat = $level.world->get(player.entity); - - std::vector log_list; - log_list.push_back(text("Log messages here.")); - - auto log_box = vbox(log_list) | yflex_grow; - - return hbox({ - hflow( - vbox( - text(fmt::format("HP: {: >3} DMG: {: >3}", - player_combat.hp, - combat.damage)), - separator(), - log_box - ) | flex_grow - ) - }); - }); - - set_renderer(status_rend); - } - - void CombatUI::create_render() { - $attack1_button = Button("ATTACK1", []{ fmt::println("ATTACK1 clicked"); }); - $attack2_button = Button("ATTACK2", []{ fmt::println("ATTACK2 clicked"); }); - - auto combat_rend = Renderer([&]{ - return hbox({ - $attack1_button->Render(), - $attack2_button->Render() - }); - }); - - set_renderer(combat_rend); - add($attack1_button); - add($attack2_button); - } - + using namespace components; FSM::FSM() : $window(sf::VideoMode({SCREEN_WIDTH, SCREEN_HEIGHT}), "Zed's Raycaster Thing"), @@ -76,7 +24,7 @@ namespace gui { { $window.setVerticalSyncEnabled(VSYNC); $window.setFramerateLimit(FRAME_LIMIT); - $text.setPosition({10,10}); + $text.setPosition({43,300}); $text.setFillColor({255,255,255}); $textures.load_tiles(); $textures.load_sprites(); @@ -261,6 +209,11 @@ namespace gui { case KEY::Escape: event(Event::CLOSE); break; + case KEY::P: { + auto& debug = $level.world->get_the(); + debug.FPS = !debug.FPS; + debug.PATHS = !debug.PATHS; + } break; default: break; // ignored } @@ -290,13 +243,10 @@ namespace gui { "count:{:<10}\n\n" "VSync? {}\n" "FR Limit: {}\n" - "Debug? {}\n\n" - "dir: {:>2.02},{:>2.02}\n" - "pos: {:>2.02},{:>2.02}\n\n", + "Debug? {}\n\n", player_combat.hp, $stats.mean(), $stats.stddev(), $stats.min, $stats.max, $stats.n, VSYNC, - FRAME_LIMIT, DEBUG_BUILD, $rayview.$dir_x, - $rayview.$dir_y, $rayview.$pos_x, $rayview.$pos_y)); + FRAME_LIMIT, DEBUG_BUILD)); $window.draw($text); } @@ -321,6 +271,9 @@ namespace gui { $combat_view.render(); $renderer.draw($combat_view); + + auto debug = $level.world->get_the(); + if(debug.FPS) draw_stats(); } void FSM::render() { diff --git a/gui.hpp b/gui.hpp index 61e2fc3..13a5026 100644 --- a/gui.hpp +++ b/gui.hpp @@ -7,41 +7,10 @@ #include "fsm.hpp" #include "render.hpp" #include "map_view.hpp" -#include // for hflow, paragraph, separator, hbox, vbox, filler, operator|, border, Element +#include "combat_ui.hpp" +#include "status_ui.hpp" namespace gui { - class StatusUI : public Panel { - public: - GameLevel $level; - - StatusUI(GameLevel level) : - Panel(43, 200, 29, 25, false), - $level(level) - { - default_bg = sf::Color{30,30,30}; - } - - void create_render(); - void update_level(GameLevel &level) { $level = level; } - }; - - class CombatUI : public Panel { - public: - GameLevel $level; - Component $attack1_button; - Component $attack2_button; - - CombatUI(GameLevel level) : - Panel(RAY_VIEW_X, RAY_VIEW_HEIGHT, 89, 6, false), - $level(level) - { - default_bg = sf::Color{30,30,30}; - } - - void create_render(); - void update_level(GameLevel &level) { $level = level; } - }; - enum class State { START, MOVING, @@ -72,6 +41,7 @@ namespace gui { // ZED: these two will go away soon int $rotation_count = 0; float $rotation = -10.0f; + bool $draw_stats = false; Point $player{0,0}; LevelManager $levels; sf::RenderWindow $window; diff --git a/meson.build b/meson.build index 5d8ca52..0c3bc69 100644 --- a/meson.build +++ b/meson.build @@ -47,6 +47,7 @@ sources = [ 'ansi_parser.cpp', 'camera.cpp', 'combat.cpp', + 'combat_ui.cpp', 'components.cpp', 'config.cpp', 'dbc.cpp', @@ -68,6 +69,7 @@ sources = [ 'shiterator.hpp', 'spatialmap.cpp', 'stats.cpp', + 'status_ui.cpp', 'systems.cpp', 'texture.cpp', 'tilemap.cpp', diff --git a/raycaster.cpp b/raycaster.cpp index 5e53cf5..c3079c1 100644 --- a/raycaster.cpp +++ b/raycaster.cpp @@ -22,22 +22,13 @@ union ColorConv { uint32_t as_int; }; -inline uint32_t dumb_lighting(uint32_t pixel, double distance) { - if(distance < 1.0) return pixel; - - ColorConv conv{.as_int=pixel}; - conv.as_color.r /= distance; - conv.as_color.g /= distance; - conv.as_color.b /= distance; - - return conv.as_int; -} - inline uint32_t new_lighting(uint32_t pixel, int level) { + float factor = level * PERCENT; + ColorConv conv{.as_int=pixel}; - conv.as_color.r *= level * PERCENT; - conv.as_color.g *= level * PERCENT; - conv.as_color.b *= level * PERCENT; + conv.as_color.r *= factor; + conv.as_color.g *= factor; + conv.as_color.b *= factor; return conv.as_int; } diff --git a/status_ui.cpp b/status_ui.cpp new file mode 100644 index 0000000..0c17daf --- /dev/null +++ b/status_ui.cpp @@ -0,0 +1,48 @@ +#include "status_ui.hpp" +#include // for Render +#include // for ftxui +#include +#include +#include +#include "components.hpp" +#include "color.hpp" + +namespace gui { + using namespace components; + using namespace ftxui; + + StatusUI::StatusUI(GameLevel level) : + Panel(STATUS_UI_X, STATUS_UI_Y, STATUS_UI_WIDTH, STATUS_UI_HEIGHT, false), + $level(level) + { + default_bg = ColorValue::DARK_MID; + } + + void StatusUI::create_render() { + auto player = $level.world->get_the(); + + auto status_rend = Renderer([&, player]{ + const auto& player_combat = $level.world->get(player.entity); + const auto& combat = $level.world->get(player.entity); + + std::vector log_list; + log_list.push_back(text("Log messages here.")); + + auto log_box = vbox(log_list) | yflex_grow; + + return hbox({ + hflow( + vbox( + text(fmt::format("HP: {: >3} DMG: {: >3}", + player_combat.hp, + combat.damage)), + separator(), + log_box + ) | flex_grow + ) + }); + }); + + set_renderer(status_rend); + } +} diff --git a/status_ui.hpp b/status_ui.hpp new file mode 100644 index 0000000..1763db9 --- /dev/null +++ b/status_ui.hpp @@ -0,0 +1,14 @@ +#pragma once +#include "panel.hpp" +#include "levelmanager.hpp" +#include "constants.hpp" + +namespace gui { + class StatusUI : public Panel { + public: + GameLevel $level; + StatusUI(GameLevel level); + void create_render(); + void update_level(GameLevel &level) { $level = level; } + }; +}