From bef2fb01d023e7118efebd3443c6755f888f4908 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Sun, 12 Feb 2023 12:53:34 +0100 Subject: [PATCH] Emscripten: Pivot EngineProcess ... such that it ends with a sleep and delegates looping to its caller. --- src/PowderToy.cpp | 7 +- src/PowderToyFontEditor.cpp | 19 +++--- src/PowderToySDL.cpp | 122 ++++++++++++++++------------------- src/gui/interface/Engine.cpp | 11 ---- src/gui/interface/Engine.h | 4 -- 5 files changed, 72 insertions(+), 91 deletions(-) diff --git a/src/PowderToy.cpp b/src/PowderToy.cpp index ed6f5adf0..cd506120f 100644 --- a/src/PowderToy.cpp +++ b/src/PowderToy.cpp @@ -183,6 +183,7 @@ int main(int argc, char * argv[]) { Platform::SetupCrt(); Platform::Atexit([]() { + SaveWindowPosition(); // Unregister dodgy error handlers so they don't try to show the blue screen when the window is closed for (auto *msg = signalMessages; msg->message; ++msg) { @@ -509,8 +510,10 @@ int main(int argc, char * argv[]) } } - EngineProcess(); - SaveWindowPosition(); + while (engine.Running()) + { + EngineProcess(); + } }; if (enableBluescreen) diff --git a/src/PowderToyFontEditor.cpp b/src/PowderToyFontEditor.cpp index a4f140805..f84f9f23b 100644 --- a/src/PowderToyFontEditor.cpp +++ b/src/PowderToyFontEditor.cpp @@ -66,14 +66,14 @@ int main(int argc, char * argv[]) StopTextInput(); - ui::Engine::Ref().g = new Graphics(); - ui::Engine::Ref().Scale = scale; - ui::Engine::Ref().SetResizable(resizable); - ui::Engine::Ref().Fullscreen = fullscreen; - ui::Engine::Ref().SetAltFullscreen(altFullscreen); - ui::Engine::Ref().SetForceIntegerScaling(forceIntegerScaling); - auto &engine = ui::Engine::Ref(); + engine.g = new Graphics(); + engine.Scale = scale; + engine.SetResizable(resizable); + engine.Fullscreen = fullscreen; + engine.SetAltFullscreen(altFullscreen); + engine.SetForceIntegerScaling(forceIntegerScaling); + engine.Begin(); engine.SetFastQuit(true); @@ -87,7 +87,10 @@ int main(int argc, char * argv[]) Platform::Exit(1); } - EngineProcess(); + while (engine.Running()) + { + EngineProcess(); + } Platform::Exit(0); return 0; } diff --git a/src/PowderToySDL.cpp b/src/PowderToySDL.cpp index 4965d4778..378d86da6 100644 --- a/src/PowderToySDL.cpp +++ b/src/PowderToySDL.cpp @@ -28,6 +28,10 @@ int mouseButton = 0; bool mouseDown = false; bool calculatedInitialMouse = false; bool hasMouseMoved = false; +double correctedFrameTimeAvg = 0; +uint64_t drawingTimer = 0; +uint64_t frameStart = 0; +uint64_t oldFrameStart = 0; void StartTextInput() { @@ -341,75 +345,61 @@ void EventProcess(const SDL_Event &event) void EngineProcess() { - double correctedFrameTimeAvg = 0; - SDL_Event event; - - uint64_t drawingTimer = 0; - auto frameStart = uint64_t(SDL_GetTicks()) * UINT64_C(1'000'000); - auto &engine = ui::Engine::Ref(); - while(engine.Running()) + auto correctedFrameTime = frameStart - oldFrameStart; + drawingTimer += correctedFrameTime; + correctedFrameTimeAvg = correctedFrameTimeAvg + (correctedFrameTime - correctedFrameTimeAvg) * 0.05; + if (correctedFrameTime && frameStart - lastFpsUpdate > UINT64_C(200'000'000)) { - if(engine.Broken()) { engine.UnBreak(); break; } - event.type = 0; - while (SDL_PollEvent(&event)) - { - EventProcess(event); - event.type = 0; //Clear last event - } - if(engine.Broken()) { engine.UnBreak(); break; } - - engine.Tick(); - - int drawcap = ui::Engine::Ref().GetDrawingFrequencyLimit(); - if (!drawcap || drawingTimer > 1e9f / drawcap) - { - engine.Draw(); - drawingTimer = 0; - - if (scale != engine.Scale || fullscreen != engine.Fullscreen || - altFullscreen != engine.GetAltFullscreen() || - forceIntegerScaling != engine.GetForceIntegerScaling() || resizable != engine.GetResizable()) - { - SDLSetScreen(engine.Scale, engine.GetResizable(), engine.Fullscreen, engine.GetAltFullscreen(), - engine.GetForceIntegerScaling()); - } - - blit(engine.g->Data()); - } - auto fpsLimit = ui::Engine::Ref().FpsLimit; - auto now = uint64_t(SDL_GetTicks()) * UINT64_C(1'000'000); - auto oldFrameStart = frameStart; - frameStart = now; - if (fpsLimit > 2) - { - auto timeBlockDuration = uint64_t(UINT64_C(1'000'000'000) / fpsLimit); - auto oldFrameStartTimeBlock = oldFrameStart / timeBlockDuration; - auto frameStartTimeBlock = oldFrameStartTimeBlock + 1U; - frameStart = std::max(frameStart, frameStartTimeBlock * timeBlockDuration); - SDL_Delay((frameStart - now) / UINT64_C(1'000'000)); - } - auto correctedFrameTime = frameStart - oldFrameStart; - drawingTimer += correctedFrameTime; - correctedFrameTimeAvg = correctedFrameTimeAvg + (correctedFrameTime - correctedFrameTimeAvg) * 0.05; - if (frameStart - lastFpsUpdate > UINT64_C(200'000'000)) - { - engine.SetFps(1e9f / correctedFrameTimeAvg); - lastFpsUpdate = frameStart; - } - if (frameStart - lastTick > UINT64_C(100'000'000)) - { - lastTick = frameStart; - TickClient(); - } - if (showLargeScreenDialog) - { - showLargeScreenDialog = false; - LargeScreenDialog(); - } + engine.SetFps(1e9f / correctedFrameTimeAvg); + lastFpsUpdate = frameStart; } - if constexpr (DEBUG) + if (frameStart - lastTick > UINT64_C(100'000'000)) { - std::cout << "Breaking out of EngineProcess" << std::endl; + lastTick = frameStart; + TickClient(); + } + if (showLargeScreenDialog) + { + showLargeScreenDialog = false; + LargeScreenDialog(); + } + + SDL_Event event; + while (SDL_PollEvent(&event)) + { + EventProcess(event); + } + + engine.Tick(); + + int drawcap = ui::Engine::Ref().GetDrawingFrequencyLimit(); + if (!drawcap || drawingTimer > 1e9f / drawcap) + { + engine.Draw(); + drawingTimer = 0; + + if (scale != engine.Scale || + fullscreen != engine.Fullscreen || + altFullscreen != engine.GetAltFullscreen() || + forceIntegerScaling != engine.GetForceIntegerScaling() || + resizable != engine.GetResizable()) + { + SDLSetScreen(engine.Scale, engine.GetResizable(), engine.Fullscreen, engine.GetAltFullscreen(), engine.GetForceIntegerScaling()); + } + + blit(engine.g->Data()); + } + auto fpsLimit = ui::Engine::Ref().FpsLimit; + auto now = uint64_t(SDL_GetTicks()) * UINT64_C(1'000'000); + oldFrameStart = frameStart; + frameStart = now; + if (fpsLimit > 2) + { + auto timeBlockDuration = uint64_t(UINT64_C(1'000'000'000) / fpsLimit); + auto oldFrameStartTimeBlock = oldFrameStart / timeBlockDuration; + auto frameStartTimeBlock = oldFrameStartTimeBlock + 1U; + frameStart = std::max(frameStart, frameStartTimeBlock * timeBlockDuration); + SDL_Delay((frameStart - now) / UINT64_C(1'000'000)); } } diff --git a/src/gui/interface/Engine.cpp b/src/gui/interface/Engine.cpp index e9adf3834..8a6768acf 100644 --- a/src/gui/interface/Engine.cpp +++ b/src/gui/interface/Engine.cpp @@ -19,7 +19,6 @@ Engine::Engine(): resizable(false), state_(NULL), windowTargetPosition(0, 0), - break_(false), FastQuit(1), lastTick(0), mouseb_(0), @@ -48,16 +47,6 @@ void Engine::Begin() running_ = true; } -void Engine::Break() -{ - break_ = true; -} - -void Engine::UnBreak() -{ - break_ = false; -} - void Engine::Exit() { onClose(); diff --git a/src/gui/interface/Engine.h b/src/gui/interface/Engine.h index 1eaba3ac5..83d7d3adb 100644 --- a/src/gui/interface/Engine.h +++ b/src/gui/interface/Engine.h @@ -40,12 +40,9 @@ namespace ui void Begin(); inline bool Running() { return running_; } - inline bool Broken() { return break_; } inline long unsigned int LastTick() { return lastTick; } void Exit(); void ConfirmExit(); - void Break(); - void UnBreak(); void SetDrawingFrequencyLimit(int limit) {drawingFrequencyLimit = limit;} inline int GetDrawingFrequencyLimit() {return drawingFrequencyLimit;} @@ -115,7 +112,6 @@ namespace ui std::stack frozenGraphics; bool running_; - bool break_; bool FastQuit; long unsigned int lastTick;