mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-08 19:48:20 +02:00
Fix SDLApp in wasm
This commit is contained in:
parent
e7fe91facb
commit
27e1a2aa7f
1 changed files with 74 additions and 54 deletions
|
@ -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,8 +108,8 @@ 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);
|
||||||
SDL.EventState(.JoyAxisMotion, .Disable);
|
SDL.EventState(.JoyAxisMotion, .Disable);
|
||||||
|
@ -205,73 +208,90 @@ 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;
|
||||||
|
SDL.Event event;
|
||||||
|
|
||||||
|
while (SDL.PollEvent(out event) != 0)
|
||||||
{
|
{
|
||||||
int32 waitTime = 1;
|
switch (event.type)
|
||||||
SDL.Event event;
|
|
||||||
|
|
||||||
while (SDL.PollEvent(out event) != 0)
|
|
||||||
{
|
{
|
||||||
switch (event.type)
|
case .Quit:
|
||||||
{
|
return;
|
||||||
case .Quit:
|
case .KeyDown:
|
||||||
return;
|
KeyDown(event.key);
|
||||||
case .KeyDown:
|
case .KeyUp:
|
||||||
KeyDown(event.key);
|
KeyUp(event.key);
|
||||||
case .KeyUp:
|
case .MouseButtonDown:
|
||||||
KeyUp(event.key);
|
MouseDown(event.button);
|
||||||
case .MouseButtonDown:
|
case .MouseButtonUp:
|
||||||
MouseDown(event.button);
|
MouseUp(event.button);
|
||||||
case .MouseButtonUp:
|
default:
|
||||||
MouseUp(event.button);
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
|
|
||||||
HandleEvent(event);
|
|
||||||
|
|
||||||
waitTime = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fixed 60 Hz update
|
HandleEvent(event);
|
||||||
double msPerTick = 1000 / 60.0;
|
|
||||||
int newPhysTickCount = (int)(sw.ElapsedMilliseconds / msPerTick);
|
waitTime = 0;
|
||||||
|
}
|
||||||
|
|
||||||
int addTicks = newPhysTickCount - curPhysTickCount;
|
// Fixed 60 Hz update
|
||||||
if (curPhysTickCount == 0)
|
double msPerTick = 1000 / 60.0;
|
||||||
|
int newPhysTickCount = (int)(mStopwatch.ElapsedMilliseconds / msPerTick);
|
||||||
|
|
||||||
|
int addTicks = newPhysTickCount - mCurPhysTickCount;
|
||||||
|
if (mCurPhysTickCount == 0)
|
||||||
|
{
|
||||||
|
// Initial render
|
||||||
|
Render();
|
||||||
|
// Show initially hidden window, mitigates white flash on slow startups
|
||||||
|
SDL.ShowWindow(mWindow);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
mKeyboardState = SDL.GetKeyboardState(null);
|
||||||
|
|
||||||
|
addTicks = Math.Min(addTicks, 20); // Limit catchup
|
||||||
|
if (addTicks > 0)
|
||||||
{
|
{
|
||||||
// Initial render
|
for (int i < addTicks)
|
||||||
Render();
|
{
|
||||||
// Show initially hidden window, mitigates white flash on slow startups
|
mUpdateCnt++;
|
||||||
SDL.ShowWindow(mWindow);
|
Update();
|
||||||
|
}
|
||||||
|
Render();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
mKeyboardState = SDL.GetKeyboardState(null);
|
Thread.Sleep(1);
|
||||||
|
|
||||||
addTicks = Math.Min(addTicks, 20); // Limit catchup
|
|
||||||
if (addTicks > 0)
|
|
||||||
{
|
|
||||||
for (int i < addTicks)
|
|
||||||
{
|
|
||||||
mUpdateCnt++;
|
|
||||||
Update();
|
|
||||||
}
|
|
||||||
Render();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Thread.Sleep(1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
curPhysTickCount = newPhysTickCount;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mCurPhysTickCount = newPhysTickCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue