The new SFMLGui is now worked into the code and barely works. Cleanup is next.

This commit is contained in:
Zed A. Shaw 2024-09-01 18:11:33 -04:00
parent 70d1389c54
commit 4bd2d12219
5 changed files with 41 additions and 121 deletions

87
gui.cpp
View file

@ -2,22 +2,15 @@
#include <stdlib.h> // for EXIT_SUCCESS
#include <chrono> // for milliseconds
#include <fmt/core.h>
#include <ftxui/component/event.hpp> // for Event
#include <ftxui/component/mouse.hpp> // for ftxui
#include <ftxui/dom/elements.hpp> // for text, separator, Element, operator|, vbox, border
#include <memory> // for allocator, shared_ptr
#include <string> // for operator+, to_string
#include <thread> // 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 <vector>
#include <SFML/Audio.hpp>
#include <nlohmann/json.hpp>
#include <fstream>
#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<std::string>();
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<bool()> runner) {
auto screen = ScreenInteractive::Fullscreen();
screen.TrackMouse(true);
auto gui = SFMLGui(game);
// Create a component counting the number of frames drawn and event handled.
float scroll_x = 0.1;
float scroll_y = 1.0;
gui.startup();
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<Element> 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));
while (gui.is_open()) {
bool result = runner();
gui.handle_events();
gui.update_entities();
gui.update_log(lines);
}
gui.shutdown();
return EXIT_SUCCESS;
}