1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-08 11:38:21 +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 mHasAudio;
private Stopwatch mStopwatch = new .() ~ delete _;
private int mCurPhysTickCount = 0;
public this()
{
gApp = this;
@ -105,7 +108,7 @@ namespace SDL2
String exePath = scope .();
Environment.GetExecutableFilePath(exePath);
String exeDir = scope .();
Path.GetDirectoryPath(exePath, exeDir);
if (Path.GetDirectoryPath(exePath, exeDir) case .Ok)
Directory.SetCurrentDirectory(exeDir);
SDL.Init(.Video | .Events | .Audio);
@ -205,13 +208,31 @@ namespace SDL2
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()
{
Stopwatch sw = scope .();
sw.Start();
int curPhysTickCount = 0;
mStopwatch.Start();
#if BF_PLATFORM_WASM
emscripten_set_main_loop(=> EmscriptenMainLoop, -1, 1);
#else
while (true)
RunOneFrame();
#endif
}
public void RunOneFrame()
{
int32 waitTime = 1;
SDL.Event event;
@ -240,10 +261,10 @@ namespace SDL2
// Fixed 60 Hz update
double msPerTick = 1000 / 60.0;
int newPhysTickCount = (int)(sw.ElapsedMilliseconds / msPerTick);
int newPhysTickCount = (int)(mStopwatch.ElapsedMilliseconds / msPerTick);
int addTicks = newPhysTickCount - curPhysTickCount;
if (curPhysTickCount == 0)
int addTicks = newPhysTickCount - mCurPhysTickCount;
if (mCurPhysTickCount == 0)
{
// Initial render
Render();
@ -270,8 +291,7 @@ namespace SDL2
}
}
curPhysTickCount = newPhysTickCount;
}
mCurPhysTickCount = newPhysTickCount;
}
}