From 0c3aec903c2fa0fbb9db5c04a29f7e1f8529ae77 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sun, 17 Nov 2024 09:48:34 -0800 Subject: Smoother resizing. --- Spear/App.hs | 35 +++++++++++++++-------------------- Spear/Render/Core/State.hs | 1 - 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/Spear/App.hs b/Spear/App.hs index b0c7141..96d45f1 100644 --- a/Spear/App.hs +++ b/Spear/App.hs @@ -55,9 +55,10 @@ loop' :: App s -> Game s () loop' window ddt inputTimer elapsed timeBudget app = do - timer <- gameIO $ tick inputTimer - - inputEvents <- gameIO $ pollInputEvents window + timer <- gameIO $ tick inputTimer + inputEvents <- gameIO $ pollInputEvents window + windowEvents <- gameIO $ pollWindowEvents window + close <- gameIO $ shouldWindowClose window let timeBudgetThisFrame = timeBudget + deltaTime timer let steps = timeBudgetThisFrame `div` ddt @@ -69,27 +70,21 @@ loop' window ddt inputTimer elapsed timeBudget app = do let dt = timeDeltaToSec ddt stepApp app t dt inputEvents) - let elapsed' = elapsed + steps * ddt - let timeBudget' = timeBudgetThisFrame `mod` ddt + let elapsedNextFrame = elapsed + steps * ddt + let timeBudgetNextFrame = timeBudgetThisFrame `mod` ddt + + when (continue && not close) $ do + resized <- or <$> forM windowEvents (\event -> case event of + ResizeEvent {} -> resizeApp app event >> return True) - when continue $ do - windowEvents <- gameIO $ pollWindowEvents window - forM_ windowEvents $ \event -> case event of - ResizeEvent {} -> resizeApp app event - renderApp app - gameIO $ swapBuffers window + -- For smoother resizing, render only while not resizing. + unless resized $ do + renderApp app + gameIO $ swapBuffers window frameEnd <- gameIO now let frameTime = timeDiff (lastTick timer) frameEnd when (frameTime < ddt) $ do gameIO $ Timer.sleep (ddt - frameTime) - close <- gameIO $ shouldWindowClose window - when (continue && not close) $ - loop' - window - ddt - timer - elapsed' - timeBudget' - app + loop' window ddt timer elapsedNextFrame timeBudgetNextFrame app diff --git a/Spear/Render/Core/State.hs b/Spear/Render/Core/State.hs index f7e5627..dac7b9a 100644 --- a/Spear/Render/Core/State.hs +++ b/Spear/Render/Core/State.hs @@ -123,7 +123,6 @@ type ShaderHash = Int type ShaderProgramHash = Int - instance ResourceClass Buffer where getResource = bufferResource -- cgit v1.2.3