Browse Source

Moved main loop to game engine class

Ivan Savenko 9 months ago
parent
commit
f8c1d217d4
3 changed files with 27 additions and 37 deletions
  1. 19 13
      client/GameEngine.cpp
  2. 3 1
      client/GameEngine.h
  3. 5 23
      clientapp/EntryPoint.cpp

+ 19 - 13
client/GameEngine.cpp

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

+ 3 - 1
client/GameEngine.h

@@ -55,6 +55,8 @@ private:
 
 	IGameEngineUser *engineUser = nullptr;
 
+	void updateFrame();
+
 public:
 	std::mutex interfaceMutex;
 
@@ -107,7 +109,7 @@ public:
 	~GameEngine();
 
 	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
 	void onScreenResize(bool resolutionChanged);

+ 5 - 23
clientapp/EntryPoint.cpp

@@ -68,11 +68,7 @@ namespace po_style = boost::program_options::command_line_style;
 static std::atomic<bool> headlessQuit = false;
 static std::optional<std::string> criticalInitializationError;
 
-#ifndef VCMI_IOS
-void processCommand(const std::string &message);
-#endif
 [[noreturn]] static void quitApplication();
-static void mainLoop();
 
 static CBasicLogConfigurator *logConfig;
 
@@ -387,12 +383,15 @@ int main(int argc, char * argv[])
 			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())
 	{
 		checkForModLoadingFailure();
-		mainLoop();
+		ENGINE->mainLoop();
 	}
 	else
 	{
@@ -404,23 +403,6 @@ int main(int argc, char * argv[])
 		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)
 {
 	// Perform quick exit without executing static destructors and let OS cleanup anything that we did not