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

master
Zed A. Shaw 10 months ago
parent 70d1389c54
commit 4bd2d12219
  1. 89
      gui.cpp
  2. 10
      meson.build
  3. 40
      sfmlgui.cpp
  4. 9
      sfmlgui.hpp
  5. 14
      sfmltest.cpp

@ -2,22 +2,15 @@
#include <stdlib.h> // for EXIT_SUCCESS #include <stdlib.h> // for EXIT_SUCCESS
#include <chrono> // for milliseconds #include <chrono> // for milliseconds
#include <fmt/core.h> #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 <memory> // for allocator, shared_ptr
#include <string> // for operator+, to_string #include <string> // for operator+, to_string
#include <thread> // for sleep_for #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 <vector>
#include <SFML/Audio.hpp> #include <SFML/Audio.hpp>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <fstream> #include <fstream>
#include "sfmlgui.hpp"
using namespace ftxui;
using namespace std; using namespace std;
using namespace fmt; using namespace fmt;
using namespace nlohmann; 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>(); json::string_t file_name = audio[file_key].template get<std::string>();
if(!buffer.loadFromFile(file_name)) { 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); sound.setBuffer(buffer);
@ -58,78 +51,18 @@ void GUI::output(const string &msg) {
} }
int GUI::main_loop(GameEngine &game, std::function<bool()> runner) { int GUI::main_loop(GameEngine &game, std::function<bool()> runner) {
auto screen = ScreenInteractive::Fullscreen(); auto gui = SFMLGui(game);
screen.TrackMouse(true);
gui.startup();
// 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<Element> output;
for(const auto line : lines) {
output.push_back(text(line));
}
return vbox(output); while (gui.is_open()) {
}); bool result = runner();
gui.handle_events();
auto game_stuff = Renderer([&] { gui.update_entities();
return vbox({ gui.update_log(lines);
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));
} }
gui.shutdown();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

@ -34,6 +34,7 @@ executable('escape_turings_tarpit',
'gui.cpp', 'gui.cpp',
'watcher.cpp', 'watcher.cpp',
'builder.cpp', 'builder.cpp',
'sfmlgui.cpp',
'escape_turings_tarpit.cpp'], 'escape_turings_tarpit.cpp'],
dependencies: dependencies) dependencies: dependencies)
@ -52,15 +53,10 @@ executable('audiotest', 'audiotest.cpp',
executable('jsontest', 'jsontest.cpp', executable('jsontest', 'jsontest.cpp',
dependencies: dependencies) dependencies: dependencies)
executable('sfmltest', [
'sfmltest.cpp',
'sfmlgui.cpp',
],
dependencies: dependencies)
runtests = executable('runtests', [ runtests = executable('runtests', [
'game_engine.cpp', 'game_engine.cpp',
'tests/game_engine.cpp'], 'tests/game_engine.cpp',
],
dependencies: dependencies + [catch2]) dependencies: dependencies + [catch2])
test('the tests', runtests) test('the tests', runtests)

@ -30,8 +30,8 @@ void SFMLGui::ImGui_update() {
TextColored(ImVec4(1,1,0,1), "Build Log"); TextColored(ImVec4(1,1,0,1), "Build Log");
BeginChild("Scrolling"); BeginChild("Scrolling");
for(int n = 0; n < 50; n++) { for(string &line : log) {
TextWrapped("%04d: Some Text", n); TextWrapped(line.c_str());
} }
EndChild(); EndChild();
End(); 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::Vector2f position = translate(x,y);
sf::Text text; sf::Text text;
text.setFont(font); text.setFont(font);
@ -107,25 +107,24 @@ void SFMLGui::update_entities() {
face_box.setFillColor(sf::Color(200, 250, 200)); face_box.setFillColor(sf::Color(200, 250, 200));
window.draw(face_box); 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.setPosition(translate(2,21));
hp_bar.setFillColor(sf::Color(100, 250, 50)); hp_bar.setFillColor(sf::Color(100, 250, 50));
window.draw(hp_bar); 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.setPosition(translate(2,21));
hp_box.setOutlineColor(sf::Color(100, 200, 50)); hp_box.setOutlineColor(sf::Color(100, 200, 50));
hp_box.setOutlineThickness(10); hp_box.setOutlineThickness(10);
hp_box.setFillColor(sf::Color::Transparent); hp_box.setFillColor(sf::Color::Transparent);
window.draw(hp_box); window.draw(hp_box);
write_text(2, 18, "HP 222"); string status = fmt::format("HP {} Rounds {} Streaks {} Deaths XXX", game.hit_points, game.rounds, game.streak);
// rounds text write_text(2, 18, status);
write_text(9, 18, "Rounds 333");
// // streaks text
write_text(21, 18, "Streaks 444");
// // deaths text
write_text(33, 18, "Deaths 555");
if(show_build_log) { if(show_build_log) {
ImGui_update(); ImGui_update();
@ -135,10 +134,18 @@ void SFMLGui::update_entities() {
show_build_log = window_active_out; 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<string> &lines) {
log.clear();
for(string &line : lines) {
log.push_back(line);
}
}
void SFMLGui::startup() { void SFMLGui::startup() {
fmt::print("Setting up a window for you...\n"); fmt::print("Setting up a window for you...\n");
settings.antialiasingLevel = 8; settings.antialiasingLevel = 8;
@ -152,13 +159,6 @@ void SFMLGui::startup() {
window.setFramerateLimit(FPS); window.setFramerateLimit(FPS);
window.setVerticalSyncEnabled(true); window.setVerticalSyncEnabled(true);
ImGui_setup(); 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() { bool SFMLGui::is_open() {

@ -3,6 +3,8 @@
#include <SFML/Graphics/RenderWindow.hpp> #include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Sprite.hpp> #include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Font.hpp> #include <SFML/Graphics/Font.hpp>
#include "game_engine.hpp"
#include <string>
constexpr int FPS=30; constexpr int FPS=30;
constexpr int X_DIM = 1920 / 2; constexpr int X_DIM = 1920 / 2;
@ -22,9 +24,11 @@ class SFMLGui {
bool show_build_log = false; bool show_build_log = false;
int hit_points = 50; int hit_points = 50;
sf::Font font; sf::Font font;
GameEngine &game;
vector<string> log;
public: public:
SFMLGui(); SFMLGui(GameEngine &g);
void startup(); void startup();
@ -33,8 +37,9 @@ public:
void handle_events(); void handle_events();
void update_entities(); void update_entities();
void update_log(vector<string> &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_setup();
void ImGui_update(); void ImGui_update();

@ -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();
}
Loading…
Cancel
Save