Emscripten: Pivot EngineProcess

... such that it ends with a sleep and delegates looping to its caller.
This commit is contained in:
Tamás Bálint Misius 2023-02-12 12:53:34 +01:00
parent 1094cc9dc9
commit bef2fb01d0
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
5 changed files with 72 additions and 91 deletions

View File

@ -183,6 +183,7 @@ int main(int argc, char * argv[])
{ {
Platform::SetupCrt(); Platform::SetupCrt();
Platform::Atexit([]() { Platform::Atexit([]() {
SaveWindowPosition();
// Unregister dodgy error handlers so they don't try to show the blue screen when the window is closed // 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) for (auto *msg = signalMessages; msg->message; ++msg)
{ {
@ -509,8 +510,10 @@ int main(int argc, char * argv[])
} }
} }
while (engine.Running())
{
EngineProcess(); EngineProcess();
SaveWindowPosition(); }
}; };
if (enableBluescreen) if (enableBluescreen)

View File

@ -66,14 +66,14 @@ int main(int argc, char * argv[])
StopTextInput(); 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(); 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.Begin();
engine.SetFastQuit(true); engine.SetFastQuit(true);
@ -87,7 +87,10 @@ int main(int argc, char * argv[])
Platform::Exit(1); Platform::Exit(1);
} }
while (engine.Running())
{
EngineProcess(); EngineProcess();
}
Platform::Exit(0); Platform::Exit(0);
return 0; return 0;
} }

View File

@ -28,6 +28,10 @@ int mouseButton = 0;
bool mouseDown = false; bool mouseDown = false;
bool calculatedInitialMouse = false; bool calculatedInitialMouse = false;
bool hasMouseMoved = false; bool hasMouseMoved = false;
double correctedFrameTimeAvg = 0;
uint64_t drawingTimer = 0;
uint64_t frameStart = 0;
uint64_t oldFrameStart = 0;
void StartTextInput() void StartTextInput()
{ {
@ -341,58 +345,11 @@ void EventProcess(const SDL_Event &event)
void EngineProcess() 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(); auto &engine = ui::Engine::Ref();
while(engine.Running())
{
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; auto correctedFrameTime = frameStart - oldFrameStart;
drawingTimer += correctedFrameTime; drawingTimer += correctedFrameTime;
correctedFrameTimeAvg = correctedFrameTimeAvg + (correctedFrameTime - correctedFrameTimeAvg) * 0.05; correctedFrameTimeAvg = correctedFrameTimeAvg + (correctedFrameTime - correctedFrameTimeAvg) * 0.05;
if (frameStart - lastFpsUpdate > UINT64_C(200'000'000)) if (correctedFrameTime && frameStart - lastFpsUpdate > UINT64_C(200'000'000))
{ {
engine.SetFps(1e9f / correctedFrameTimeAvg); engine.SetFps(1e9f / correctedFrameTimeAvg);
lastFpsUpdate = frameStart; lastFpsUpdate = frameStart;
@ -407,9 +364,42 @@ void EngineProcess()
showLargeScreenDialog = false; showLargeScreenDialog = false;
LargeScreenDialog(); LargeScreenDialog();
} }
}
if constexpr (DEBUG) SDL_Event event;
while (SDL_PollEvent(&event))
{ {
std::cout << "Breaking out of EngineProcess" << std::endl; 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));
} }
} }

View File

@ -19,7 +19,6 @@ Engine::Engine():
resizable(false), resizable(false),
state_(NULL), state_(NULL),
windowTargetPosition(0, 0), windowTargetPosition(0, 0),
break_(false),
FastQuit(1), FastQuit(1),
lastTick(0), lastTick(0),
mouseb_(0), mouseb_(0),
@ -48,16 +47,6 @@ void Engine::Begin()
running_ = true; running_ = true;
} }
void Engine::Break()
{
break_ = true;
}
void Engine::UnBreak()
{
break_ = false;
}
void Engine::Exit() void Engine::Exit()
{ {
onClose(); onClose();

View File

@ -40,12 +40,9 @@ namespace ui
void Begin(); void Begin();
inline bool Running() { return running_; } inline bool Running() { return running_; }
inline bool Broken() { return break_; }
inline long unsigned int LastTick() { return lastTick; } inline long unsigned int LastTick() { return lastTick; }
void Exit(); void Exit();
void ConfirmExit(); void ConfirmExit();
void Break();
void UnBreak();
void SetDrawingFrequencyLimit(int limit) {drawingFrequencyLimit = limit;} void SetDrawingFrequencyLimit(int limit) {drawingFrequencyLimit = limit;}
inline int GetDrawingFrequencyLimit() {return drawingFrequencyLimit;} inline int GetDrawingFrequencyLimit() {return drawingFrequencyLimit;}
@ -115,7 +112,6 @@ namespace ui
std::stack<FrozenGraphics> frozenGraphics; std::stack<FrozenGraphics> frozenGraphics;
bool running_; bool running_;
bool break_;
bool FastQuit; bool FastQuit;
long unsigned int lastTick; long unsigned int lastTick;