From 4bd2d122196f81e324baa95fcab877956b55e684 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sun, 1 Sep 2024 18:11:33 -0400 Subject: [PATCH] The new SFMLGui is now worked into the code and barely works. Cleanup is next. --- gui.cpp | 91 +++++++--------------------------------------------- meson.build | 10 ++---- sfmlgui.cpp | 42 ++++++++++++------------ sfmlgui.hpp | 9 ++++-- sfmltest.cpp | 14 -------- 5 files changed, 43 insertions(+), 123 deletions(-) delete mode 100644 sfmltest.cpp diff --git a/gui.cpp b/gui.cpp index 7bbad57..bd19618 100644 --- a/gui.cpp +++ b/gui.cpp @@ -2,22 +2,15 @@ #include // for EXIT_SUCCESS #include // for milliseconds #include -#include // for Event -#include // for ftxui -#include // for text, separator, Element, operator|, vbox, border #include // for allocator, shared_ptr #include // for operator+, to_string #include // for sleep_for - -#include "ftxui/component/component.hpp" // for CatchEvent, Renderer, operator|= -#include "ftxui/component/loop.hpp" // for Loop -#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive #include #include #include #include +#include "sfmlgui.hpp" -using namespace ftxui; using namespace std; using namespace fmt; using namespace nlohmann; @@ -28,7 +21,7 @@ void SoundQuip::load(json &data, const char *file_key) { json::string_t file_name = audio[file_key].template get(); if(!buffer.loadFromFile(file_name)) { - cout << "Failed to load sound: " << file_key << " with file " << file_name << "\n"; + println("Failed to load sound: {} with file {}", file_key, file_name); } sound.setBuffer(buffer); @@ -58,78 +51,18 @@ void GUI::output(const string &msg) { } int GUI::main_loop(GameEngine &game, std::function runner) { - auto screen = ScreenInteractive::Fullscreen(); - screen.TrackMouse(true); - - // Create a component counting the number of frames drawn and event handled. - float scroll_x = 0.1; - float scroll_y = 1.0; - - auto status = Renderer([&] { - return vbox({ - text(fmt::format("HP {} | Hits Taken {} | Round {} | Streak {}", game.hit_points, game.hits_taken, game.rounds, game.streak)), - separator(), - hbox({ - text("HP "), - gauge(game.hit_points / 100.0f), - }), - }); - }); - - auto content = Renderer([&] { - vector output; - - for(const auto line : lines) { - output.push_back(text(line)); - } - - return vbox(output); - }); - - auto game_stuff = Renderer([&] { - return vbox({ - text("Welcome to...Turing's Tarpit!"), - separator(), - hbox({ - text("Your Face") | center | xflex_grow , - text("Something Fun") | border | flex, - }) | yflex_grow - }); - }); - - auto build_log = Renderer(content, - [&, content] { - return content->Render() - | focusPositionRelative(scroll_x, scroll_y) - | frame | flex; - }); - - auto component = Renderer(build_log, [&] { - return vbox({ - status->Render(), - separator(), - build_log->Render() | vscroll_indicator | yframe | yflex_grow, - separator(), - game_stuff->Render() | flex | size(HEIGHT, GREATER_THAN, 20), - }); - }); - - component |= CatchEvent([&](Event) -> bool { - return false; - }); - - Loop loop(&screen, component); - - while (!loop.HasQuitted()) { - int run_error = runner(); - - if(run_error != 0) output("RUNNER ERROR!!!! CATASTROPHIC!!!"); - - loop.RunOnce(); - screen.Post(Event::Custom); - std::this_thread::sleep_for(std::chrono::milliseconds(10)); + auto gui = SFMLGui(game); + + gui.startup(); + + while (gui.is_open()) { + bool result = runner(); + gui.handle_events(); + gui.update_entities(); + gui.update_log(lines); } + gui.shutdown(); return EXIT_SUCCESS; } diff --git a/meson.build b/meson.build index c275e4a..6a1a050 100644 --- a/meson.build +++ b/meson.build @@ -34,6 +34,7 @@ executable('escape_turings_tarpit', 'gui.cpp', 'watcher.cpp', 'builder.cpp', + 'sfmlgui.cpp', 'escape_turings_tarpit.cpp'], dependencies: dependencies) @@ -52,15 +53,10 @@ executable('audiotest', 'audiotest.cpp', executable('jsontest', 'jsontest.cpp', dependencies: dependencies) -executable('sfmltest', [ - 'sfmltest.cpp', - 'sfmlgui.cpp', - ], - dependencies: dependencies) - runtests = executable('runtests', [ 'game_engine.cpp', - 'tests/game_engine.cpp'], + 'tests/game_engine.cpp', + ], dependencies: dependencies + [catch2]) test('the tests', runtests) diff --git a/sfmlgui.cpp b/sfmlgui.cpp index 3937e50..c9427d9 100644 --- a/sfmlgui.cpp +++ b/sfmlgui.cpp @@ -30,9 +30,9 @@ void SFMLGui::ImGui_update() { TextColored(ImVec4(1,1,0,1), "Build Log"); BeginChild("Scrolling"); - for(int n = 0; n < 50; n++) { - TextWrapped("%04d: Some Text", n); - } + for(string &line : log) { + TextWrapped(line.c_str()); + } EndChild(); End(); } @@ -86,7 +86,7 @@ sf::Vector2f translate(int x, int y) { } -void SFMLGui::write_text(int x, int y, const char *content) { +void SFMLGui::write_text(int x, int y, string content) { sf::Vector2f position = translate(x,y); sf::Text text; text.setFont(font); @@ -107,25 +107,24 @@ void SFMLGui::update_entities() { face_box.setFillColor(sf::Color(200, 250, 200)); window.draw(face_box); - sf::RectangleShape hp_bar(translate(32,2)); + constexpr int hp_box_len = 44; + + int current_hp = (float(game.hit_points) / float(game.starting_hp)) * float(hp_box_len); + + sf::RectangleShape hp_bar(translate(current_hp,2)); hp_bar.setPosition(translate(2,21)); hp_bar.setFillColor(sf::Color(100, 250, 50)); window.draw(hp_bar); - sf::RectangleShape hp_box(translate(44,2)); + sf::RectangleShape hp_box(translate(hp_box_len,2)); hp_box.setPosition(translate(2,21)); hp_box.setOutlineColor(sf::Color(100, 200, 50)); hp_box.setOutlineThickness(10); hp_box.setFillColor(sf::Color::Transparent); window.draw(hp_box); - write_text(2, 18, "HP 222"); - // rounds text - write_text(9, 18, "Rounds 333"); - // // streaks text - write_text(21, 18, "Streaks 444"); - // // deaths text - write_text(33, 18, "Deaths 555"); + string status = fmt::format("HP {} Rounds {} Streaks {} Deaths XXX", game.hit_points, game.rounds, game.streak); + write_text(2, 18, status); if(show_build_log) { ImGui_update(); @@ -135,10 +134,18 @@ void SFMLGui::update_entities() { show_build_log = window_active_out; } -SFMLGui::SFMLGui() : window(sf::VideoMode(X_DIM, Y_DIM), "Turing's Tarpit", sf::Style::None, settings) { +SFMLGui::SFMLGui(GameEngine &g) : window(sf::VideoMode(X_DIM, Y_DIM), "Turing's Tarpit", sf::Style::None, settings), game(g) +{ } +void SFMLGui::update_log(vector &lines) { + log.clear(); + for(string &line : lines) { + log.push_back(line); + } +} + void SFMLGui::startup() { fmt::print("Setting up a window for you...\n"); settings.antialiasingLevel = 8; @@ -152,13 +159,6 @@ void SFMLGui::startup() { window.setFramerateLimit(FPS); window.setVerticalSyncEnabled(true); ImGui_setup(); - - // fake image here - if(!texture.loadFromFile("./assets/turing_tarpit_main_screen.png")) { - fmt::println("Error loading sprite!"); - } - texture.setSmooth(true); - background.setTexture(texture); } bool SFMLGui::is_open() { diff --git a/sfmlgui.hpp b/sfmlgui.hpp index 03718ff..6b64f99 100644 --- a/sfmlgui.hpp +++ b/sfmlgui.hpp @@ -3,6 +3,8 @@ #include #include #include +#include "game_engine.hpp" +#include constexpr int FPS=30; constexpr int X_DIM = 1920 / 2; @@ -22,9 +24,11 @@ class SFMLGui { bool show_build_log = false; int hit_points = 50; sf::Font font; + GameEngine &game; + vector log; public: - SFMLGui(); + SFMLGui(GameEngine &g); void startup(); @@ -33,8 +37,9 @@ public: void handle_events(); void update_entities(); + void update_log(vector &lines); - void write_text(int x, int y, const char *content); + void write_text(int x, int y, std::string content); void ImGui_setup(); void ImGui_update(); diff --git a/sfmltest.cpp b/sfmltest.cpp deleted file mode 100644 index 68de805..0000000 --- a/sfmltest.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include "sfmlgui.hpp" - -int main() { - auto gui = SFMLGui(); - - gui.startup(); - - while (gui.is_open()) { - gui.handle_events(); - gui.update_entities(); - } - - gui.shutdown(); -}