Index: source/main.cpp =================================================================== --- source/main.cpp +++ source/main.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2022 Wildfire Games. +/* Copyright (C) 2023 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -169,6 +169,13 @@ g_Shutdown = ShutdownType::RestartAsAtlas; } +template +static void WhileNoShutdown(Callback callback) +{ + while (g_Shutdown == ShutdownType::None) + callback(); +}; + // main app message handler static InReaction MainInputHandler(const SDL_Event_* ev) { @@ -353,7 +360,7 @@ while (more && timer_Time() - startTime < maxTime); } -static void Frame() +static void Frame(RL::Interface* rlInterface) { g_Profiler2.RecordFrameStart(); PROFILE2("frame"); @@ -419,12 +426,12 @@ g_GUI->TickObjects(); - if (g_RLInterface) - g_RLInterface->TryApplyMessage(); + if (rlInterface) + rlInterface->TryApplyMessage(); if (g_Game && g_Game->IsGameStarted() && need_update) { - if (!g_RLInterface) + if (!rlInterface) g_Game->Update(realTimeSinceLastFrame); g_Game->GetView()->Update(float(realTimeSinceLastFrame)); @@ -482,7 +489,7 @@ in_reset_handlers(); } -static void StartRLInterface(CmdLineArgs args) +static RL::Interface CreateRLInterface(const CmdLineArgs& args) { std::string server_address; CFG_GET_VAL("rlinterface.address", server_address); @@ -490,8 +497,8 @@ if (!args.Get("rl-interface").empty()) server_address = args.Get("rl-interface"); - g_RLInterface = std::make_unique(server_address.c_str()); debug_printf("RL interface listening on %s\n", server_address.c_str()); + return RL::Interface{server_address.c_str()}; } // moved into a helper function to ensure args is destroyed before @@ -667,24 +674,27 @@ if (!InitNonVisual(args)) g_Shutdown = ShutdownType::Quit; else if (isUsingRLInterface) - StartRLInterface(args); - - while (g_Shutdown == ShutdownType::None) - { - if (isUsingRLInterface) - g_RLInterface->TryApplyMessage(); - else - NonVisualFrame(); - } + WhileNoShutdown([rlInterface = CreateRLInterface(args)]() mutable + { + rlInterface.TryApplyMessage(); + }); + else + WhileNoShutdown(&NonVisualFrame); } else { InitGraphics(args, 0, installedMods); MainControllerInit(); if (isUsingRLInterface) - StartRLInterface(args); - while (g_Shutdown == ShutdownType::None) - Frame(); + WhileNoShutdown([rlInterface = CreateRLInterface(args)]() mutable + { + Frame(&rlInterface); + }); + else + WhileNoShutdown([] + { + Frame(nullptr); + }); } // Do not install mods again in case of restart (typically from the mod selector) Index: source/rlinterface/RLInterface.h =================================================================== --- source/rlinterface/RLInterface.h +++ source/rlinterface/RLInterface.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2023 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -162,6 +162,4 @@ } -extern std::unique_ptr g_RLInterface; - #endif // INCLUDED_RLINTERFACE Index: source/rlinterface/RLInterface.cpp =================================================================== --- source/rlinterface/RLInterface.cpp +++ source/rlinterface/RLInterface.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2023 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -38,9 +38,6 @@ #include #include -// Globally accessible pointer to the RL Interface. -std::unique_ptr g_RLInterface; - namespace RL { Interface::Interface(const char* server_address) : m_GameMessage({GameMessageType::None})