259 lines
7.2 KiB
Diff
259 lines
7.2 KiB
Diff
From 1fdea8fe6fd1caf7b37de3e0ad423121b73672e4 Mon Sep 17 00:00:00 2001
|
|
From: Andrew Copland <fluffyfreak@users.noreply.github.com>
|
|
Date: Wed, 26 Dec 2018 21:58:54 +0000
|
|
Subject: [PATCH] Queue requests in response to UI events, never process them
|
|
immediately.
|
|
|
|
---
|
|
src/LuaEngine.cpp | 4 +--
|
|
src/Pi.cpp | 57 +++++++++++++++++++++++++++++--------------
|
|
src/Pi.h | 15 +++++++++---
|
|
src/galaxy/Galaxy.cpp | 6 ++---
|
|
src/main.cpp | 1 -
|
|
5 files changed, 54 insertions(+), 29 deletions(-)
|
|
|
|
diff --git src/LuaEngine.cpp src/LuaEngine.cpp
|
|
index ebfe2b5eb1..858dcbf75f 100644
|
|
--- src/LuaEngine.cpp
|
|
+++ src/LuaEngine.cpp
|
|
@@ -152,9 +152,7 @@ static int l_engine_attr_version(lua_State *l)
|
|
*/
|
|
static int l_engine_quit(lua_State *l)
|
|
{
|
|
- if (Pi::game)
|
|
- Pi::EndGame();
|
|
- Pi::Quit();
|
|
+ Pi::RequestQuit();
|
|
return 0;
|
|
}
|
|
|
|
diff --git src/Pi.cpp src/Pi.cpp
|
|
index db4b6721b4..4066e44e7b 100644
|
|
--- src/Pi.cpp
|
|
+++ src/Pi.cpp
|
|
@@ -152,7 +152,7 @@ Graphics::RenderTarget *Pi::renderTarget;
|
|
RefCountedPtr<Graphics::Texture> Pi::renderTexture;
|
|
std::unique_ptr<Graphics::Drawables::TexturedQuad> Pi::renderQuad;
|
|
Graphics::RenderState *Pi::quadRenderState = nullptr;
|
|
-bool Pi::bRequestEndGame = false;
|
|
+std::vector<Pi::InternalRequests> Pi::internalRequests;
|
|
bool Pi::isRecordingVideo = false;
|
|
FILE *Pi::ffmpegFile = nullptr;
|
|
|
|
@@ -774,6 +774,9 @@ bool Pi::IsConsoleActive()
|
|
|
|
void Pi::Quit()
|
|
{
|
|
+ if (Pi::game) { // always end the game if there is one before quitting
|
|
+ Pi::EndGame();
|
|
+ }
|
|
if (Pi::ffmpegFile != nullptr) {
|
|
_pclose(Pi::ffmpegFile);
|
|
}
|
|
@@ -837,9 +840,7 @@ void Pi::HandleKeyDown(SDL_Keysym *key)
|
|
if (CTRL) {
|
|
switch (key->sym) {
|
|
case SDLK_q: // Quit
|
|
- if (Pi::game)
|
|
- Pi::EndGame();
|
|
- Pi::Quit();
|
|
+ Pi::RequestQuit();
|
|
break;
|
|
case SDLK_PRINTSCREEN: // print
|
|
case SDLK_KP_MULTIPLY: // screen
|
|
@@ -1039,9 +1040,7 @@ void Pi::HandleEvents()
|
|
Pi::input.mouseMotion[0] = Pi::input.mouseMotion[1] = 0;
|
|
while (SDL_PollEvent(&event)) {
|
|
if (event.type == SDL_QUIT) {
|
|
- if (Pi::game)
|
|
- Pi::EndGame();
|
|
- Pi::Quit();
|
|
+ Pi::RequestQuit();
|
|
}
|
|
|
|
Pi::pigui->ProcessEvent(&event);
|
|
@@ -1095,6 +1094,26 @@ void Pi::HandleEvents()
|
|
}
|
|
}
|
|
|
|
+void Pi::HandleRequests()
|
|
+{
|
|
+ for (auto request : internalRequests)
|
|
+ {
|
|
+ switch (request)
|
|
+ {
|
|
+ case END_GAME:
|
|
+ EndGame();
|
|
+ break;
|
|
+ case QUIT_GAME:
|
|
+ Quit();
|
|
+ break;
|
|
+ default:
|
|
+ Output("Pi::HandleRequests, unhandled request type processed.\n");
|
|
+ break;
|
|
+ }
|
|
+ }
|
|
+ internalRequests.clear();
|
|
+}
|
|
+
|
|
void Pi::TombStoneLoop()
|
|
{
|
|
std::unique_ptr<Tombstone> tombstone(new Tombstone(Pi::renderer, Graphics::GetScreenWidth(), Graphics::GetScreenHeight()));
|
|
@@ -1115,6 +1134,8 @@ void Pi::TombStoneLoop()
|
|
Pi::DrawRenderTarget();
|
|
Pi::renderer->SwapBuffers();
|
|
|
|
+ Pi::HandleRequests();
|
|
+
|
|
Pi::frameTime = 0.001f * (SDL_GetTicks() - last_time);
|
|
_time += Pi::frameTime;
|
|
last_time = SDL_GetTicks();
|
|
@@ -1162,8 +1183,6 @@ void Pi::StartGame()
|
|
|
|
void Pi::Start(const SystemPath &startPath)
|
|
{
|
|
- Pi::bRequestEndGame = false;
|
|
-
|
|
Pi::intro = new Intro(Pi::renderer, Graphics::GetScreenWidth(), Graphics::GetScreenHeight());
|
|
if (startPath != SystemPath(0, 0, 0, 0, 0)) {
|
|
Pi::game = new Game(startPath, 0.0);
|
|
@@ -1179,7 +1198,7 @@ void Pi::Start(const SystemPath &startPath)
|
|
SDL_Event event;
|
|
while (SDL_PollEvent(&event)) {
|
|
if (event.type == SDL_QUIT)
|
|
- Pi::Quit();
|
|
+ Pi::RequestQuit();
|
|
else {
|
|
Pi::pigui->ProcessEvent(&event);
|
|
|
|
@@ -1260,6 +1279,8 @@ void Pi::Start(const SystemPath &startPath)
|
|
_time += Pi::frameTime;
|
|
last_time = SDL_GetTicks();
|
|
|
|
+ Pi::HandleRequests();
|
|
+
|
|
#ifdef ENABLE_SERVER_AGENT
|
|
Pi::serverAgent->ProcessResponses();
|
|
#endif
|
|
@@ -1276,14 +1297,16 @@ void Pi::Start(const SystemPath &startPath)
|
|
// request that the game is ended as soon as safely possible
|
|
void Pi::RequestEndGame()
|
|
{
|
|
- Pi::bRequestEndGame = true;
|
|
+ internalRequests.push_back(END_GAME);
|
|
}
|
|
|
|
-void Pi::EndGame()
|
|
+void Pi::RequestQuit()
|
|
{
|
|
- // always reset this, otherwise we can never play again
|
|
- Pi::bRequestEndGame = false;
|
|
+ internalRequests.push_back(QUIT_GAME);
|
|
+}
|
|
|
|
+void Pi::EndGame()
|
|
+{
|
|
Pi::SetMouseGrab(false);
|
|
|
|
Pi::musicPlayer.Stop();
|
|
@@ -1423,10 +1446,6 @@ void Pi::MainLoop()
|
|
Pi::luaConsole->HandleTCPDebugConnections();
|
|
#endif
|
|
|
|
- if (Pi::bRequestEndGame) {
|
|
- Pi::EndGame();
|
|
- }
|
|
-
|
|
Pi::renderer->EndFrame();
|
|
|
|
Pi::renderer->ClearDepthBuffer();
|
|
@@ -1510,6 +1529,8 @@ void Pi::MainLoop()
|
|
asyncJobQueue->FinishJobs();
|
|
syncJobQueue->FinishJobs();
|
|
|
|
+ HandleRequests();
|
|
+
|
|
#if WITH_DEVKEYS
|
|
if (Pi::showDebugInfo && SDL_GetTicks() - last_stats > 1000) {
|
|
size_t lua_mem = Lua::manager->GetMemoryUsage();
|
|
diff --git src/Pi.h src/Pi.h
|
|
index e7dfa78877..8924536e4e 100644
|
|
--- src/Pi.h
|
|
+++ src/Pi.h
|
|
@@ -65,10 +65,10 @@ class Pi {
|
|
static void RequestEndGame(); // request that the game is ended as soon as safely possible
|
|
static void EndGame();
|
|
static void Start(const SystemPath &startPath);
|
|
+ static void RequestQuit();
|
|
static void MainLoop();
|
|
static void TombStoneLoop();
|
|
static void OnChangeDetailLevel();
|
|
- static void Quit() __attribute((noreturn));
|
|
static float GetFrameTime() { return frameTime; }
|
|
static float GetGameTickAlpha() { return gameTickAlpha; }
|
|
|
|
@@ -160,10 +160,20 @@ class Pi {
|
|
static bool DrawGUI;
|
|
|
|
private:
|
|
+ // msgs/requests that can be posted which the game processes at the end of a game loop in HandleRequests
|
|
+ enum InternalRequests
|
|
+ {
|
|
+ END_GAME = 0,
|
|
+ QUIT_GAME,
|
|
+ };
|
|
+ static void Quit() __attribute((noreturn));
|
|
static void HandleKeyDown(SDL_Keysym *key);
|
|
static void HandleEvents();
|
|
- // Handler for ESC key press
|
|
+ static void HandleRequests();
|
|
static void HandleEscKey();
|
|
+
|
|
+ // private members
|
|
+ static std::vector<InternalRequests> internalRequests;
|
|
static const Uint32 SYNC_JOBS_PER_LOOP = 1;
|
|
static std::unique_ptr<AsyncJobQueue> asyncJobQueue;
|
|
static std::unique_ptr<SyncJobQueue> syncJobQueue;
|
|
@@ -193,7 +203,6 @@ class Pi {
|
|
static Graphics::RenderState *quadRenderState;
|
|
|
|
static bool doingMouseGrab;
|
|
- static bool bRequestEndGame;
|
|
|
|
static bool isRecordingVideo;
|
|
static FILE *ffmpegFile;
|
|
diff --git src/galaxy/Galaxy.cpp src/galaxy/Galaxy.cpp
|
|
index d19d615f3b..312fec6ded 100644
|
|
--- src/galaxy/Galaxy.cpp
|
|
+++ src/galaxy/Galaxy.cpp
|
|
@@ -113,15 +113,13 @@ DensityMapGalaxy::DensityMapGalaxy(RefCountedPtr<GalaxyGenerator> galaxyGenerato
|
|
{
|
|
RefCountedPtr<FileSystem::FileData> filedata = FileSystem::gameDataFiles.ReadFile(mapfile);
|
|
if (!filedata) {
|
|
- Output("Galaxy: couldn't load '%s'\n", mapfile.c_str());
|
|
- Pi::Quit();
|
|
+ Error("Galaxy: couldn't load '%s'\n", mapfile.c_str());
|
|
}
|
|
|
|
SDL_RWops *datastream = SDL_RWFromConstMem(filedata->GetData(), filedata->GetSize());
|
|
SDL_Surface *galaxyImg = SDL_LoadBMP_RW(datastream, 1);
|
|
if (!galaxyImg) {
|
|
- Output("Galaxy: couldn't load: %s (%s)\n", mapfile.c_str(), SDL_GetError());
|
|
- Pi::Quit();
|
|
+ Error("Galaxy: couldn't load: %s (%s)\n", mapfile.c_str(), SDL_GetError());
|
|
}
|
|
|
|
// now that we have our raw image loaded
|
|
diff --git src/main.cpp src/main.cpp
|
|
index 864ae2a737..f59ca02336 100644
|
|
--- src/main.cpp
|
|
+++ src/main.cpp
|
|
@@ -197,7 +197,6 @@ int main(int argc, char** argv)
|
|
if (filename != "-" && fclose(file) != 0) {
|
|
Output("pioneer: writing to \"%s\" failed: %s\n", filename.c_str(), strerror(errno));
|
|
}
|
|
- Pi::Quit();
|
|
}
|
|
break;
|
|
}
|