Skip to content

Commit

Permalink
Update the UI on game start
Browse files Browse the repository at this point in the history
* update UI even when paused
* send frame events earlier

* closes #2081
  • Loading branch information
xsebek committed Aug 4, 2024
1 parent 3cf62ab commit d56aa46
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/game/Swarm/App.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ appMain opts = do
chan <- newBChan 5
_ <- forkIO $
forever $ do
threadDelay 33_333 -- cap maximum framerate at 30 FPS
writeBChan chan Frame
threadDelay 33_333 -- cap maximum framerate at 30 FPS

_ <- forkIO $ do
upRel <- getNewerReleaseVersion (repoGitInfo opts)
Expand Down
12 changes: 6 additions & 6 deletions src/swarm-tui/Swarm/TUI/Controller.hs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ import Swarm.Language.Value (Value (VKey), envTypes)
import Swarm.Log
import Swarm.TUI.Controller.EventHandlers
import Swarm.TUI.Controller.SaveScenario (saveScenarioInfoOnQuit)
import Swarm.TUI.Controller.UpdateUI (updateAndRedrawUI)
import Swarm.TUI.Controller.Util
import Swarm.TUI.Editor.Controller qualified as EC
import Swarm.TUI.Editor.Model
Expand Down Expand Up @@ -287,13 +288,12 @@ handleMainEvent forceRedraw ev = do
let keyHandler = s ^. keyEventHandling . keyDispatchers . to mainGameDispatcher
case ev of
AppEvent ae -> case ae of
Frame
-- If the game is paused, don't run any game ticks, but do redraw the screen
-- if a redraw is forced.
| s ^. gameState . temporal . paused -> unless forceRedraw continueWithoutRedraw
| otherwise -> runFrameUI forceRedraw
-- If the game is paused, don't run any game ticks, but do redraw if needed.
Frame -> if s ^. gameState . temporal . paused
then updateAndRedrawUI forceRedraw
else runFrameUI forceRedraw
Web (RunWebCode c) -> runBaseWebCode c
_ -> continueWithoutRedraw
UpstreamVersion _ -> error "version event should be handled by top-level handler"
VtyEvent (V.EvResize _ _) -> invalidateCache
EscapeKey | Just m <- s ^. uiState . uiGameplay . uiModal -> closeModal m
-- Pass to key handler (allows users to configure bindings)
Expand Down
5 changes: 1 addition & 4 deletions src/swarm-tui/Swarm/TUI/Controller/EventHandlers/Frame.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ ticksPerFrameCap = 30
-- this may involve stepping the game any number of ticks (including
-- zero).
runFrameUI :: Bool -> EventM Name AppState ()
runFrameUI forceRedraw = do
runFrame
redraw <- updateUI
unless (forceRedraw || redraw) continueWithoutRedraw
runFrameUI forceRedraw = runFrame >> updateAndRedrawUI forceRedraw

-- | Run the game for a single frame, without updating the UI.
runFrame :: EventM Name AppState ()
Expand Down
9 changes: 9 additions & 0 deletions src/swarm-tui/Swarm/TUI/Controller/UpdateUI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
-- The main TUI update logic that is called from other controller parts.
module Swarm.TUI.Controller.UpdateUI (
updateUI,
updateAndRedrawUI,
) where

import Brick hiding (Direction, Location)
Expand Down Expand Up @@ -43,6 +44,14 @@ import Swarm.TUI.View.Objective qualified as GR
import Witch (into)
import Prelude hiding (Applicative (..))

-- | Update the UI and redraw if needed.
--
-- This function is used after running the game for some number of ticks.
updateAndRedrawUI :: Bool -> EventM Name AppState ()
updateAndRedrawUI forceRedraw = do
redraw <- updateUI
unless (forceRedraw || redraw) continueWithoutRedraw

-- | Update the UI. This function is used after running the
-- game for some number of ticks.
updateUI :: EventM Name AppState Bool
Expand Down

0 comments on commit d56aa46

Please sign in to comment.