Skip to content

Commit

Permalink
Reworked coroutines to use QCoro instead of Boost.Asio
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrettin committed Feb 24, 2024
1 parent 9373798 commit 4e68cb7
Show file tree
Hide file tree
Showing 36 changed files with 342 additions and 326 deletions.
9 changes: 4 additions & 5 deletions src/editor/editloop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2206,7 +2206,7 @@ static void EditorCallbackExit()
/**
** Create editor.
*/
boost::asio::awaitable<void> CEditor::Init()
QCoro::Task<void> CEditor::Init()
{
// Load and evaluate the editor configuration file
const std::string filename = LibraryFileName(parameters::get()->luaEditorStartFilename.c_str());
Expand Down Expand Up @@ -2301,13 +2301,12 @@ boost::asio::awaitable<void> CEditor::Init()
}

//create the game in another thread, to not block the main one while it is loading
co_await thread_pool::get()->co_spawn_awaitable([]() -> boost::asio::awaitable<void> {
co_await QtConcurrent::run([]() {
if (CurrentMapPath.empty()) {
CreateGame("", CMap::get());
} else {
CreateGame(CurrentMapPath, CMap::get());
}
co_return;
});

ReplayRevealMap = 1;
Expand Down Expand Up @@ -2406,7 +2405,7 @@ std::string get_user_maps_path()
/**
** Editor main event loop.
*/
boost::asio::awaitable<void> EditorMainLoop()
QCoro::Task<void> EditorMainLoop()
{
const centesimal_int &scale_factor = preferences::get()->get_scale_factor();

Expand Down Expand Up @@ -2475,7 +2474,7 @@ boost::asio::awaitable<void> EditorMainLoop()
game::get()->set_running(true); //should use something different instead?

engine_interface::get()->set_waiting_for_interface(true);
co_await thread_pool::get()->await_future(engine_interface::get()->get_map_view_created_future());
co_await engine_interface::get()->get_map_view_created_future();
engine_interface::get()->reset_map_view_created_promise();
engine_interface::get()->set_waiting_for_interface(false);

Expand Down
18 changes: 7 additions & 11 deletions src/editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ CEditor::CEditor() : SelectedPlayer(PlayerNumNeutral), State(EditorStateType::Ed
**
** @param filename Map to load, null to create a new map
*/
boost::asio::awaitable<void> CEditor::start(const std::filesystem::path &filepath)
QCoro::Task<void> CEditor::start(const std::filesystem::path filepath)
{
std::string nc, rc;

Expand Down Expand Up @@ -94,7 +94,7 @@ boost::asio::awaitable<void> CEditor::start(const std::filesystem::path &filepat
CleanPlayers();
}

boost::asio::awaitable<void> CEditor::start_new(const std::string &name, const QSize &map_size)
QCoro::Task<void> CEditor::start_new(const std::string name, const QSize map_size)
{
auto map_info = make_qunique<wyrmgus::map_info>();
map_info->set_name(name);
Expand All @@ -106,23 +106,19 @@ boost::asio::awaitable<void> CEditor::start_new(const std::string &name, const Q
co_await this->start("");
}

void CEditor::start_async(const QString &filepath)
QCoro::QmlTask CEditor::start_async(const QString &filepath)
{
event_loop::get()->co_spawn([this, filepath]() -> boost::asio::awaitable<void> {
co_await this->start(path::from_qstring(filepath));
});
return this->start(path::from_qstring(filepath));
}

void CEditor::start_new_async(const QString &name, const int map_width, const int map_height)
QCoro::QmlTask CEditor::start_new_async(const QString &name, const int map_width, const int map_height)
{
event_loop::get()->co_spawn([this, name, map_width, map_height]() -> boost::asio::awaitable<void> {
co_await this->start_new(name.toStdString(), QSize(map_width, map_height));
});
return this->start_new(name.toStdString(), QSize(map_width, map_height));
}

void CEditor::set_running_async(const bool running)
{
event_loop::get()->post([this, running]() {
QTimer::singleShot(0, [this, running]() {
this->set_running(running);
});
}
29 changes: 12 additions & 17 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ int game::get_cycles_per_year() const
return defines::get()->get_cycles_per_year(this->get_current_year());
}

boost::asio::awaitable<void> game::run_map(const std::filesystem::path &filepath)
QCoro::Task<void> game::run_map(const std::filesystem::path filepath)
{
engine_interface::get()->set_loading_message("Starting Game...");

Expand All @@ -283,24 +283,20 @@ boost::asio::awaitable<void> game::run_map(const std::filesystem::path &filepath
CurrentQuest = nullptr;
}

void game::run_map_async(const QString &filepath)
QCoro::QmlTask game::run_map_async(const QString &filepath)
{
event_loop::get()->co_spawn([this, filepath]() -> boost::asio::awaitable<void> {
co_await this->run_map(path::from_qstring(filepath));
});
return this->run_map(path::from_qstring(filepath));
}

void game::run_campaign_async(campaign *campaign)
QCoro::Task<void> game::run_campaign_coro(campaign *campaign)
{
event_loop::get()->co_spawn([this, campaign]() -> boost::asio::awaitable<void> {
if (this->get_current_campaign() != nullptr) {
//already running
co_return;
}
if (this->get_current_campaign() != nullptr) {
//already running
co_return;
}

this->set_current_campaign(campaign);
co_await this->run_map(database::get()->get_campaign_map_filepath());
});
this->set_current_campaign(campaign);
co_await this->run_map(database::get()->get_campaign_map_filepath());
}

void game::apply_player_history()
Expand Down Expand Up @@ -806,7 +802,7 @@ void SaveGameSettings(CFile &file)
file.printf("\n");
}

boost::asio::awaitable<void> StartMap(const std::filesystem::path &filepath, const bool clean)
QCoro::Task<void> StartMap(const std::filesystem::path &filepath, const bool clean)
{
try {
std::string nc, rc;
Expand All @@ -833,9 +829,8 @@ boost::asio::awaitable<void> StartMap(const std::filesystem::path &filepath, con
GameEstablishing = true;

//create the game in another thread, to not block the main one while it is loading
co_await thread_pool::get()->co_spawn_awaitable([&filepath]() -> boost::asio::awaitable<void> {
co_await QtConcurrent::run([&filepath]() {
CreateGame(filepath, CMap::get());
co_return;
});

//Wyrmgus start
Expand Down
19 changes: 13 additions & 6 deletions src/game/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,17 @@ class game final : public QObject, public singleton<game>
}

[[nodiscard]]
boost::asio::awaitable<void> run_map(const std::filesystem::path &filepath);
QCoro::Task<void> run_map(const std::filesystem::path filepath);

Q_INVOKABLE void run_map_async(const QString &filepath);
Q_INVOKABLE void run_campaign_async(wyrmgus::campaign *campaign);
Q_INVOKABLE QCoro::QmlTask run_map_async(const QString &filepath);

Q_INVOKABLE QCoro::QmlTask run_campaign_async(wyrmgus::campaign *campaign)
{
return this->run_campaign_coro(campaign);
}

[[nodiscard]]
QCoro::Task<void> run_campaign_coro(campaign *campaign);

void apply_player_history();

Expand Down Expand Up @@ -322,10 +329,10 @@ extern void LoadGame(const std::filesystem::path &filepath); /// Load saved game
extern int SaveGame(const std::string &file_url_str); /// Save game

[[nodiscard]]
extern boost::asio::awaitable<void> StartSavedGame(const std::filesystem::path &filepath);
extern QCoro::Task<void> StartSavedGame(const std::filesystem::path &filepath);

[[nodiscard]]
extern boost::asio::awaitable<void> load_game(const std::filesystem::path &filepath);
extern QCoro::Task<void> load_game(const std::filesystem::path &filepath);

extern void set_load_game_file(const std::filesystem::path &filepath);
extern std::filesystem::path load_game_file;
Expand All @@ -341,7 +348,7 @@ extern void FreeAllContainers();
extern void SaveGameSettings(CFile &file); /// Save game settings

[[nodiscard]]
extern boost::asio::awaitable<void> StartMap(const std::filesystem::path &filepath, const bool clean);
extern QCoro::Task<void> StartMap(const std::filesystem::path &filepath, const bool clean);

extern std::string GameName; /// Name of the game
extern std::string FullGameName; /// Full Name of the game
Expand Down
2 changes: 1 addition & 1 deletion src/game/replay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ int SaveReplay(const std::string &filename)
return 0;
}

boost::asio::awaitable<void> StartReplay(const std::filesystem::path &filepath, const bool reveal)
QCoro::Task<void> StartReplay(const std::filesystem::path &filepath, const bool reveal)
{
CleanPlayers();
LoadReplay(filepath);
Expand Down
4 changes: 2 additions & 2 deletions src/game/savegame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ int SaveGame(const std::string &file_url_str)
return 0;
}

boost::asio::awaitable<void> StartSavedGame(const std::filesystem::path &filepath)
QCoro::Task<void> StartSavedGame(const std::filesystem::path &filepath)
{
SaveGameLoading = true;
LoadGame(filepath);

co_await StartMap(filepath, false);
}

boost::asio::awaitable<void> load_game(const std::filesystem::path &filepath)
QCoro::Task<void> load_game(const std::filesystem::path &filepath)
{
engine_interface::get()->set_loading_message("Loading Saved Game...");

Expand Down
12 changes: 6 additions & 6 deletions src/include/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ class CEditor final : public QObject, public singleton<CEditor>
CEditor();

[[nodiscard]]
boost::asio::awaitable<void> start(const std::filesystem::path &filepath);
QCoro::Task<void> start(const std::filesystem::path filepath);

[[nodiscard]]
boost::asio::awaitable<void> start_new(const std::string &name, const QSize &map_size);
QCoro::Task<void> start_new(const std::string name, const QSize map_size);

Q_INVOKABLE void start_async(const QString &filepath);
Q_INVOKABLE void start_new_async(const QString &name, const int map_width, const int map_height);
Q_INVOKABLE QCoro::QmlTask start_async(const QString &filepath);
Q_INVOKABLE QCoro::QmlTask start_new_async(const QString &name, const int map_width, const int map_height);

[[nodiscard]]
boost::asio::awaitable<void> Init();
QCoro::Task<void> Init();

bool is_running() const
{
Expand Down Expand Up @@ -126,7 +126,7 @@ class CEditor final : public QObject, public singleton<CEditor>

/// Editor main event loop
[[nodiscard]]
extern boost::asio::awaitable<void> EditorMainLoop();
extern QCoro::Task<void> EditorMainLoop();

/// Update editor display
extern void EditorUpdateDisplay();
Expand Down
2 changes: 1 addition & 1 deletion src/include/replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ extern void CleanReplayLog();
extern void SaveReplayList(CFile &file);

[[nodiscard]]
extern boost::asio::awaitable<void> StartReplay(const std::filesystem::path &filepath, const bool reveal);
extern QCoro::Task<void> StartReplay(const std::filesystem::path &filepath, const bool reveal);

/// Register ccl functions related to network
extern void ReplayCclRegister();
6 changes: 3 additions & 3 deletions src/include/stratagus.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,18 @@ extern unsigned long GameCycle; /// Game simulation cycle counter
extern unsigned long FastForwardCycle; /// Game Replay Fast Forward Counter

[[nodiscard]]
extern boost::asio::awaitable<void> Exit(const int err); /// Exit
extern QCoro::Task<void> Exit(const int err); /// Exit

extern void UpdateDisplay(); /// Game display update
extern void DrawMapArea(std::vector<std::function<void(wyrmgus::renderer *)>> &render_commands); //draw the map area

[[nodiscard]]
extern boost::asio::awaitable<void> GameMainLoop(); //game main loop
extern QCoro::Task<void> GameMainLoop(); //game main loop

extern void stratagus_on_exit_cleanup();

[[nodiscard]]
extern boost::asio::awaitable<void> stratagusMain(int argc, char **argv); /// main entry
extern QCoro::Task<void> stratagusMain(int argc, char **argv); /// main entry

//Wyrmgus start
enum Difficulties {
Expand Down
4 changes: 2 additions & 2 deletions src/include/title.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class TitleScreen final
{
public:
[[nodiscard]]
boost::asio::awaitable<void> ShowTitleImage(std::vector<std::function<void(renderer *)>> &render_commands) const;
QCoro::Task<void> ShowTitleImage(std::vector<std::function<void(renderer *)>> &render_commands) const;

private:
void ShowLabels(std::vector<std::function<void(renderer *)>> &render_commands) const;
Expand All @@ -68,4 +68,4 @@ class TitleScreen final
extern std::vector<TitleScreen> TitleScreens; /// File for title screen

[[nodiscard]]
extern boost::asio::awaitable<void> ShowTitleScreens(std::vector<std::function<void(renderer *)>> &render_commands);
extern QCoro::Task<void> ShowTitleScreens(std::vector<std::function<void(renderer *)>> &render_commands);
1 change: 1 addition & 0 deletions src/include/widgets.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ class MenuScreen final : public gcn::Container
MenuScreen();

void run(const bool loop = true);
QCoro::Task<void> run_coro(const bool loop);

void stop(bool stopAll = false);

Expand Down
Loading

0 comments on commit 4e68cb7

Please sign in to comment.