diff --git a/client/GameEngine.cpp b/client/GameEngine.cpp index 67c3f7bd8..30866a60a 100644 --- a/client/GameEngine.cpp +++ b/client/GameEngine.cpp @@ -23,21 +23,17 @@ #include "media/CVideoHandler.h" #include "media/CEmptyVideoPlayer.h" -#include "CPlayerInterface.h" #include "adventureMap/AdventureMapInterface.h" #include "render/Canvas.h" #include "render/Colors.h" -#include "render/Graphics.h" #include "render/IFont.h" #include "render/EFont.h" #include "renderSDL/ScreenHandler.h" #include "renderSDL/RenderHandler.h" -#include "CMT.h" #include "GameEngineUser.h" #include "battle/BattleInterface.h" #include "../lib/AsyncRunner.h" -#include "../lib/CThreadHelper.h" #include "../lib/CConfigHandler.h" #include @@ -84,8 +80,8 @@ GameEngine::GameEngine() soundPlayerInstance = std::make_unique(); musicPlayerInstance = std::make_unique(); - sound().setVolume((ui32)settings["general"]["sound"].Float()); - music().setVolume((ui32)settings["general"]["music"].Float()); + sound().setVolume(settings["general"]["sound"].Integer()); + music().setVolume(settings["general"]["music"].Integer()); cursorHandlerInstance = std::make_unique(); asyncTasks = std::make_unique(); @@ -244,7 +240,7 @@ std::shared_ptr GameEngine::statusbar() return locked; } -void GameEngine::setStatusbar(std::shared_ptr newStatusBar) +void GameEngine::setStatusbar(const std::shared_ptr & newStatusBar) { currentStatusBar = newStatusBar; } diff --git a/client/GameEngine.h b/client/GameEngine.h index 69d99f681..80dee43e2 100644 --- a/client/GameEngine.h +++ b/client/GameEngine.h @@ -58,6 +58,8 @@ private: IGameEngineUser *engineUser = nullptr; void updateFrame(); + void handleEvents(); //takes events from queue and calls interested objects + void drawFPSCounter(); // draws the FPS to the upper left corner of the screen public: std::mutex interfaceMutex; @@ -102,7 +104,9 @@ public: std::shared_ptr statusbar(); /// Set currently active status bar - void setStatusbar(std::shared_ptr); + void setStatusbar(const std::shared_ptr &); + + /// Sets engine user that is used as target of callback for events received by engine void setEngineUser(IGameEngineUser * user); bool captureChildren; //all newly created objects will get their parents from stack and will be added to parents children list @@ -111,15 +115,17 @@ public: GameEngine(); ~GameEngine(); + /// Performs main game loop till game shutdown + /// This method never returns, to abort main loop throw GameShutdownException [[noreturn]] void mainLoop(); /// called whenever SDL_WINDOWEVENT_RESTORED is reported or the user selects a different resolution, requiring to center/resize all windows void onScreenResize(bool resolutionChanged); - void handleEvents(); //takes events from queue and calls interested objects + /// Simulate mouse movement to force refresh UI state that updates on mouse move void fakeMouseMove(); - void drawFPSCounter(); // draws the FPS to the upper left corner of the screen + /// Returns true for calls made from main (GUI) thread, false othervice bool amIGuiThread(); /// Calls provided functor in main thread on next execution frame diff --git a/clientapp/EntryPoint.cpp b/clientapp/EntryPoint.cpp index 235a7e4b3..6ab52e40f 100644 --- a/clientapp/EntryPoint.cpp +++ b/clientapp/EntryPoint.cpp @@ -235,14 +235,14 @@ int main(int argc, char * argv[]) } Settings session = settings.write["session"]; - auto setSettingBool = [&](std::string key, std::string arg) { + auto setSettingBool = [&](const std::string & key, const std::string & arg) { Settings s = settings.write(vstd::split(key, "/")); if(vm.count(arg)) s->Bool() = true; else if(s->isNull()) s->Bool() = false; }; - auto setSettingInteger = [&](std::string key, std::string arg, si64 defaultValue) { + auto setSettingInteger = [&](const std::string & key, const std::string & arg, si64 defaultValue) { Settings s = settings.write(vstd::split(key, "/")); if(vm.count(arg)) s->Integer() = vm[arg].as(); @@ -280,7 +280,7 @@ int main(int argc, char * argv[]) logGlobal->debug("settings = %s", settings.toJsonNode().toString()); // Some basic data validation to produce better error messages in cases of incorrect install - auto testFile = [](std::string filename, std::string message) + auto testFile = [](const std::string & filename, const std::string & message) { if (!CResourceHandler::get()->existsResource(ResourcePath(filename))) handleFatalError(message, false); diff --git a/lib/AsyncRunner.h b/lib/AsyncRunner.h index 94ce6e44b..5d9496bd4 100644 --- a/lib/AsyncRunner.h +++ b/lib/AsyncRunner.h @@ -9,23 +9,27 @@ */ #pragma once -#include #include +#include VCMI_LIB_NAMESPACE_BEGIN +/// Helper class for running asynchronous tasks using TBB thread pool class AsyncRunner : boost::noncopyable { tbb::task_arena arena; tbb::task_group taskGroup; public: - template + /// Runs the provided functor asynchronously on a thread from the TBB worker pool. + template void run(Functor && f) { arena.enqueue(taskGroup.defer(std::forward(f))); } + /// Waits for all previously enqueued task. + /// Re-entrable - waiting for tasks does not prevent submitting new tasks void wait() { taskGroup.wait();