1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-09 03:52:19 +02:00

Fix SDLApp in wasm

This commit is contained in:
disarray2077 2022-02-10 12:24:27 -03:00 committed by GitHub
parent e7fe91facb
commit 27e1a2aa7f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -87,6 +87,9 @@ namespace SDL2
public bool* mKeyboardState; public bool* mKeyboardState;
public bool mHasAudio; public bool mHasAudio;
private Stopwatch mStopwatch = new .() ~ delete _;
private int mCurPhysTickCount = 0;
public this() public this()
{ {
gApp = this; gApp = this;
@ -105,7 +108,7 @@ namespace SDL2
String exePath = scope .(); String exePath = scope .();
Environment.GetExecutableFilePath(exePath); Environment.GetExecutableFilePath(exePath);
String exeDir = scope .(); String exeDir = scope .();
Path.GetDirectoryPath(exePath, exeDir); if (Path.GetDirectoryPath(exePath, exeDir) case .Ok)
Directory.SetCurrentDirectory(exeDir); Directory.SetCurrentDirectory(exeDir);
SDL.Init(.Video | .Events | .Audio); SDL.Init(.Video | .Events | .Audio);
@ -205,13 +208,31 @@ namespace SDL2
SDL.RenderPresent(mRenderer); SDL.RenderPresent(mRenderer);
} }
#if BF_PLATFORM_WASM
private function void em_callback_func();
[CLink, CallingConvention(.Stdcall)]
private static extern void emscripten_set_main_loop(em_callback_func func, int32 fps, int32 simulateInfinteLoop);
private static void EmscriptenMainLoop()
{
gApp.RunOneFrame();
}
#endif
public void Run() public void Run()
{ {
Stopwatch sw = scope .(); mStopwatch.Start();
sw.Start();
int curPhysTickCount = 0;
#if BF_PLATFORM_WASM
emscripten_set_main_loop(=> EmscriptenMainLoop, -1, 1);
#else
while (true) while (true)
RunOneFrame();
#endif
}
public void RunOneFrame()
{ {
int32 waitTime = 1; int32 waitTime = 1;
SDL.Event event; SDL.Event event;
@ -240,10 +261,10 @@ namespace SDL2
// Fixed 60 Hz update // Fixed 60 Hz update
double msPerTick = 1000 / 60.0; double msPerTick = 1000 / 60.0;
int newPhysTickCount = (int)(sw.ElapsedMilliseconds / msPerTick); int newPhysTickCount = (int)(mStopwatch.ElapsedMilliseconds / msPerTick);
int addTicks = newPhysTickCount - curPhysTickCount; int addTicks = newPhysTickCount - mCurPhysTickCount;
if (curPhysTickCount == 0) if (mCurPhysTickCount == 0)
{ {
// Initial render // Initial render
Render(); Render();
@ -270,8 +291,7 @@ namespace SDL2
} }
} }
curPhysTickCount = newPhysTickCount; mCurPhysTickCount = newPhysTickCount;
}
} }
} }