Score rendering
This commit is contained in:
parent
1dd23d6e62
commit
ffb2a087e1
8 changed files with 203 additions and 119 deletions
64
app/Main.hs
64
app/Main.hs
|
|
@ -1,25 +1,28 @@
|
|||
module Main where
|
||||
|
||||
import SDL
|
||||
import qualified SDL.Image as IMG
|
||||
import qualified SDL.Font as FONT
|
||||
import qualified Data.Text as Text
|
||||
import Control.Monad (unless)
|
||||
import Graphics
|
||||
import SnakeLib
|
||||
import SDL.Raw (getTicks)
|
||||
import Data.Word (Word32)
|
||||
import Foreign.C (CInt)
|
||||
|
||||
import Snake
|
||||
|
||||
import Assets
|
||||
import Graphics
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
initializeAll
|
||||
window <- createWindow (Text.pack "Hello, World") defaultWindow
|
||||
FONT.initialize
|
||||
window <- createWindow (Text.pack "Haskell Snake") defaultWindow {windowInitialSize = snakeWindowSize}
|
||||
renderer <- createRenderer window (-1) defaultRenderer
|
||||
|
||||
texture <- IMG.loadTexture renderer "assets/spritesheet.png"
|
||||
|
||||
appLoop renderer texture (initialState (10, 10) (2, 2))
|
||||
|
||||
destroyTexture texture
|
||||
assets <- loadAssets renderer
|
||||
appLoop renderer assets (initialState boardSize (2, 2))
|
||||
freeAssets assets
|
||||
destroyRenderer renderer
|
||||
destroyWindow window
|
||||
|
||||
|
|
@ -29,32 +32,47 @@ targetFps = 5
|
|||
targetFrameMs :: Word32
|
||||
targetFrameMs = 1000 `div` targetFps
|
||||
|
||||
appLoop :: Renderer -> Texture -> Game -> IO ()
|
||||
appLoop renderer texture game = do
|
||||
boardSize :: (Int, Int)
|
||||
boardSize = (12, 8)
|
||||
|
||||
boardWindowSize :: (Int, Int) -> V2 CInt
|
||||
boardWindowSize (w, h) = V2 (fromIntegral w * tileSize) (fromIntegral h * tileSize)
|
||||
|
||||
snakeWindowSize :: V2 CInt
|
||||
snakeWindowSize = boardWindowSize boardSize
|
||||
|
||||
appLoop :: Renderer -> Assets -> Game -> IO ()
|
||||
appLoop renderer assets game = do
|
||||
frameStart <- getTicks
|
||||
events <- pollEvents
|
||||
let eventIsExitPress event = case eventPayload event of
|
||||
KeyboardEvent keyboardEvent ->
|
||||
keyboardEventKeyMotion keyboardEvent == Pressed &&
|
||||
keysymKeycode (keyboardEventKeysym keyboardEvent) == KeycodeQ
|
||||
WindowClosedEvent _ -> True
|
||||
_ -> False
|
||||
KeyboardEvent keyboardEvent ->
|
||||
keyboardEventKeyMotion keyboardEvent == Pressed &&
|
||||
keysymKeycode (keyboardEventKeysym keyboardEvent) == KeycodeQ
|
||||
WindowClosedEvent _ -> True
|
||||
_ -> False
|
||||
exitPressed = any eventIsExitPress events
|
||||
let eventDirectionPressed event = case eventPayload event of
|
||||
let eventIsRestartPress event = case eventPayload event of
|
||||
KeyboardEvent keyboardEvent ->
|
||||
keyboardEventKeyMotion keyboardEvent == Pressed &&
|
||||
keysymKeycode (keyboardEventKeysym keyboardEvent) == KeycodeR
|
||||
_ -> False
|
||||
restartPresssed = any eventIsRestartPress events
|
||||
let eventDirectionPress event = case eventPayload event of
|
||||
KeyboardEvent keyboardEvent ->
|
||||
if keyboardEventKeyMotion keyboardEvent == Pressed
|
||||
then getInputByKeycode $ keysymKeycode (keyboardEventKeysym keyboardEvent)
|
||||
else NothingPressed
|
||||
_ -> NothingPressed
|
||||
input = foldMap eventDirectionPressed events
|
||||
frameEnd <- getTicks
|
||||
let elapsed = frameEnd - frameStart
|
||||
(_, updatedGame) <- runTick game input
|
||||
renderFrame renderer texture updatedGame
|
||||
input = foldMap eventDirectionPress events
|
||||
(_, updatedGame) <- if restartPresssed then pure (IntoEmpty, initialState (10, 8) (2, 2)) else runTick game input
|
||||
renderFrame renderer assets updatedGame
|
||||
|
||||
unless exitPressed $ do
|
||||
frameEnd <- getTicks
|
||||
let elapsed = frameEnd - frameStart
|
||||
delay (targetFrameMs - elapsed)
|
||||
appLoop renderer texture updatedGame
|
||||
appLoop renderer assets updatedGame
|
||||
|
||||
getInputByKeycode :: Keycode -> MovementInput
|
||||
getInputByKeycode KeycodeW = UpPressed
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue