From cacf72604f21097db020a155908180fca039750b Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Fri, 6 Sep 2024 23:35:17 -0400 Subject: [PATCH] Very janky barely working coroutine version of the game, the UI is more responsive, but not as much as it should be. Still, take a look and study the coro.hpp and other files in this commit for some coroutine stuff. --- builder.cpp | 39 +++++++++++++++++++++++++++++---------- builder.hpp | 3 ++- coro.hpp | 3 +++ corotest.cpp | 3 +-- gui.cpp | 2 +- meson.build | 3 ++- 6 files changed, 38 insertions(+), 15 deletions(-) diff --git a/builder.cpp b/builder.cpp index e2390b0..4ff47b4 100644 --- a/builder.cpp +++ b/builder.cpp @@ -2,6 +2,7 @@ #include "dbc.hpp" #include "watcher.hpp" #include "game_engine.hpp" +#include "coro.hpp" #include // for milliseconds #include #include @@ -16,7 +17,6 @@ #include #include // for EXIT_SUCCESS #include // for operator+, to_string -#include // for sleep_for #include #include #include @@ -36,24 +36,32 @@ Builder::Builder(GUI &g, GameEngine &engine) : gui(g), game(engine) { config["build_cmd"].template get_to(build_cmd); } -void Builder::run_build() { +Task Builder::run_build() { regex err_re("(.*?):([0-9]+):([0-9]+):\\s*(.*?):\\s*(.*)\n*"); char buffer[BUF_MAX]; // BUF_MAX is a define already? ofstream stats_out; + + co_await Pass{}; + stats_out.open("stats.csv", ios::out | ios::app); std::time_t tstamp = std::time(nullptr); dbc::check(stats_out.good(), "Error opening stats.csv file."); dbc::pre("simple test", [&]() { return stats_out.good(); }); + co_yield 1; + // need to catch the error message when the command is bad FILE *build_out = popen(build_cmd.c_str(), "r"); dbc::check(build_out != nullptr, "Failed to run command."); + co_yield 2; + game.start_round(); while(fgets(buffer, BUF_MAX, build_out) != nullptr) { + co_yield 3; string line(buffer); // yeah, that's probably a problem cerr << buffer; @@ -82,8 +90,11 @@ void Builder::run_build() { gui.you_died(); } } + co_yield 4; } + co_yield 3; + game.end_round(); int rc = pclose(build_out); @@ -96,6 +107,8 @@ void Builder::run_build() { stats_out.close(); dbc::post("a post test", [&]() { return !stats_out.is_open(); }); + + co_return 1000; } void Builder::run() { @@ -117,22 +130,28 @@ void Builder::run() { gui.output(format("Watching directory {} for changes...", git_path)); efsw::WatchID wid = fileWatcher->addWatch(git_path, listener, true); + auto build_task = run_build(); + int rc = gui.main_loop(game, [&] { fileWatcher->watch(); if(listener->changes) { gui.building(); - std::this_thread::sleep_for(std::chrono::milliseconds(100)); gui.output(format("CHANGES! Running build {}", build_cmd)); - run_build(); - if(game.is_dead()) { - gui.output("!!!! YOU DIED! !!!! Learn to code luser."); - game.reset(); + if(!build_task.done()) { + unsigned point = build_task(); + } else { + if(game.is_dead()) { + gui.output("!!!! YOU DIED! !!!! Learn to code luser."); + game.reset() + } + + listener->reset_state(); + gui.output("^^^^^^^^^^^ END ^^^^^^^^^^^"); + build_task.destroy(); + build_task = run_build(); } - - listener->reset_state(); - gui.output("^^^^^^^^^^^ END ^^^^^^^^^^^"); } return 0; diff --git a/builder.hpp b/builder.hpp index 1df588d..2f4d412 100644 --- a/builder.hpp +++ b/builder.hpp @@ -1,6 +1,7 @@ #pragma once #include "gui.hpp" #include "game_engine.hpp" +#include "coro.hpp" class Builder { GUI gui; @@ -12,7 +13,7 @@ class Builder { Builder(GUI &g, GameEngine &engine); - void run_build(); + Task run_build(); void run(); }; diff --git a/coro.hpp b/coro.hpp index c06eb2c..c863d3f 100644 --- a/coro.hpp +++ b/coro.hpp @@ -102,3 +102,6 @@ struct Task { } } }; + +struct Pass : std::suspend_always { +}; diff --git a/corotest.cpp b/corotest.cpp index fb8ea7b..6b140a6 100644 --- a/corotest.cpp +++ b/corotest.cpp @@ -5,7 +5,7 @@ Task task_test() { - co_await std::suspend_always{}; + co_await Pass{}; for (unsigned i = 0; i < 3; ++i) co_yield i; @@ -15,7 +15,6 @@ Task task_test() int main() { - const int task_count = 4; vector> tasks; diff --git a/gui.cpp b/gui.cpp index bd19618..695fa37 100644 --- a/gui.cpp +++ b/gui.cpp @@ -55,7 +55,7 @@ int GUI::main_loop(GameEngine &game, std::function runner) { gui.startup(); - while (gui.is_open()) { + while(gui.is_open()) { bool result = runner(); gui.handle_events(); gui.update_entities(); diff --git a/meson.build b/meson.build index ae63084..e81e092 100644 --- a/meson.build +++ b/meson.build @@ -32,7 +32,8 @@ executable('escape_turings_tarpit', 'builder.cpp', 'sfmlgui.cpp', 'escape_turings_tarpit.cpp'], - dependencies: dependencies) + dependencies: dependencies, + cpp_args: '-fcoroutines') executable('regtest', 'regtest.cpp', dependencies: [fmt])