1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-07-17 01:32:21 +02:00

Moved main loop to game engine class

This commit is contained in:
Ivan Savenko
2025-03-03 20:30:15 +00:00
parent 222b73bbcd
commit f8c1d217d4
3 changed files with 31 additions and 41 deletions

View File

@ -104,27 +104,33 @@ void GameEngine::fakeMouseMove()
}); });
} }
void GameEngine::renderFrame() [[noreturn]] void GameEngine::mainLoop()
{ {
for (;;)
{ {
std::scoped_lock interfaceLock(ENGINE->interfaceMutex); input().fetchEvents();
updateFrame();
engineUser->onUpdate(); screenHandlerInstance->presentScreenTexture();
framerate().framerateDelay(); // holds a constant FPS
handleEvents();
windows().simpleRedraw();
if (settings["video"]["showfps"].Bool())
drawFPSCounter();
screenHandlerInstance->updateScreenTexture();
windows().onFrameRendered();
ENGINE->cursor().update();
} }
}
screenHandlerInstance->presentScreenTexture(); void GameEngine::updateFrame()
framerate().framerateDelay(); // holds a constant FPS {
std::scoped_lock interfaceLock(ENGINE->interfaceMutex);
engineUser->onUpdate();
handleEvents();
windows().simpleRedraw();
if (settings["video"]["showfps"].Bool())
drawFPSCounter();
screenHandlerInstance->updateScreenTexture();
windows().onFrameRendered();
ENGINE->cursor().update();
} }
GameEngine::GameEngine() GameEngine::GameEngine()

View File

@ -55,6 +55,8 @@ private:
IGameEngineUser *engineUser = nullptr; IGameEngineUser *engineUser = nullptr;
void updateFrame();
public: public:
std::mutex interfaceMutex; std::mutex interfaceMutex;
@ -107,7 +109,7 @@ public:
~GameEngine(); ~GameEngine();
void init(); void init();
void renderFrame(); [[noreturn]] void mainLoop();
/// called whenever SDL_WINDOWEVENT_RESTORED is reported or the user selects a different resolution, requiring to center/resize all windows /// 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 onScreenResize(bool resolutionChanged);

View File

@ -68,11 +68,7 @@ namespace po_style = boost::program_options::command_line_style;
static std::atomic<bool> headlessQuit = false; static std::atomic<bool> headlessQuit = false;
static std::optional<std::string> criticalInitializationError; static std::optional<std::string> criticalInitializationError;
#ifndef VCMI_IOS
void processCommand(const std::string &message);
#endif
[[noreturn]] static void quitApplication(); [[noreturn]] static void quitApplication();
static void mainLoop();
static CBasicLogConfigurator *logConfig; static CBasicLogConfigurator *logConfig;
@ -387,12 +383,15 @@ int main(int argc, char * argv[])
GAME->mainmenu()->playMusic(); GAME->mainmenu()->playMusic();
} }
std::vector<std::string> names; #ifndef VCMI_UNIX
// on Linux, name of main thread is also name of our process. Which we don't want to change
setThreadName("MainGUI");
#endif
if(!settings["session"]["headless"].Bool()) if(!settings["session"]["headless"].Bool())
{ {
checkForModLoadingFailure(); checkForModLoadingFailure();
mainLoop(); ENGINE->mainLoop();
} }
else else
{ {
@ -404,23 +403,6 @@ int main(int argc, char * argv[])
quitApplication(); quitApplication();
} }
return 0;
}
static void mainLoop()
{
#ifndef VCMI_UNIX
// on Linux, name of main thread is also name of our process. Which we don't want to change
setThreadName("MainGUI");
#endif
while(1) //main SDL events loop
{
ENGINE->input().fetchEvents();
ENGINE->renderFrame();
}
}
[[noreturn]] static void quitApplicationImmediately(int error_code) [[noreturn]] static void quitApplicationImmediately(int error_code)
{ {
// Perform quick exit without executing static destructors and let OS cleanup anything that we did not // Perform quick exit without executing static destructors and let OS cleanup anything that we did not