animate-example 0.0.0 → 0.1.0
raw patch · 6 files changed
+59/−18 lines, 6 filesbinary-added
Files
- Main.hs +15/−15
- animate-example.cabal +3/−1
- dino.json +37/−0
- dino.png binary
- package.yaml +3/−1
- stack.yaml +1/−1
Main.hs view
@@ -2,9 +2,9 @@ import qualified SDL import qualified SDL.Image as Image-import qualified Data.Animate as Ani+import qualified Animate -import Control.Monad (unless)+import Control.Monad (unless, when) import Control.Concurrent (threadDelay) import Data.StateVar (($=)) import Foreign.C.Types@@ -18,8 +18,7 @@ | DinoKey'Sneak deriving (Show, Eq, Ord, Bounded, Enum) -instance Ani.Key DinoKey-instance Ani.KeyName DinoKey where+instance Animate.KeyName DinoKey where keyName = \case DinoKey'Idle -> "Idle" DinoKey'Move -> "Move"@@ -27,7 +26,7 @@ DinoKey'Hurt -> "Hurt" DinoKey'Sneak -> "Sneak" -loadSurface :: FilePath -> Maybe Ani.Color -> IO SDL.Surface+loadSurface :: FilePath -> Maybe Animate.Color -> IO SDL.Surface loadSurface path alpha = do surface <- Image.load path case alpha of@@ -42,8 +41,8 @@ window <- SDL.createWindow "Animate Example" SDL.defaultWindow { SDL.windowInitialSize = V2 320 180 } SDL.showWindow window screen <- SDL.getWindowSurface window- spriteSheet <- Ani.readSpriteSheetJSON loadSurface "dino.json" :: IO (Ani.SpriteSheet DinoKey SDL.Surface)- loop window screen spriteSheet (Ani.initPosition DinoKey'Idle)+ spriteSheet <- Animate.readSpriteSheetJSON loadSurface "dino.json" :: IO (Animate.SpriteSheet DinoKey SDL.Surface Float)+ loop window screen spriteSheet (Animate.initPosition DinoKey'Idle) SDL.destroyWindow window SDL.quit @@ -55,27 +54,28 @@ not repeated _ -> False -loop :: SDL.Window -> SDL.Surface -> Ani.SpriteSheet DinoKey SDL.Surface -> Ani.Position DinoKey -> IO ()-loop window screen ss@Ani.SpriteSheet{ssAnimations, ssImage} pos = do+loop :: SDL.Window -> SDL.Surface -> Animate.SpriteSheet DinoKey SDL.Surface Float -> Animate.Position DinoKey Float -> IO ()+loop window screen ss@Animate.SpriteSheet{ssAnimations, ssImage} pos = do events <- map SDL.eventPayload <$> SDL.pollEvents let quit = elem SDL.QuitEvent events let toNextKey = any detectSpacePressed events- let pos' = Ani.stepPosition ssAnimations pos frameDeltaSeconds- let loc = Ani.currentLocation ssAnimations pos'+ let pos' = Animate.stepPosition ssAnimations pos frameDeltaSeconds+ let loc = Animate.currentLocation ssAnimations pos' SDL.surfaceFillRect screen Nothing (V4 0 0 0 0) -- Clear screen _ <- SDL.surfaceBlit ssImage (Just $ rectFromClip loc) screen Nothing SDL.updateWindowSurface window delayMilliseconds frameDeltaMilliseconds- let pos'' = if toNextKey then Ani.initPosition (Ani.nextKey (Ani.pKey pos')) else pos'+ let pos'' = if toNextKey then Animate.initPosition (Animate.nextKey (Animate.pKey pos')) else pos'+ when toNextKey $ print $ Animate.keyName (Animate.pKey pos'') unless quit $ loop window screen ss pos'' where frameDeltaSeconds = 0.016667 frameDeltaMilliseconds = 16 -rectFromClip :: Ani.SpriteClip -> SDL.Rectangle CInt-rectFromClip Ani.SpriteClip{scX,scY,scW,scH} = SDL.Rectangle (SDL.P (V2 (num scX) (num scY))) (V2 (num scW) (num scH))+rectFromClip :: Animate.SpriteClip -> SDL.Rectangle CInt+rectFromClip Animate.SpriteClip{scX,scY,scW,scH} = SDL.Rectangle (SDL.P (V2 (num scX) (num scY))) (V2 (num scW) (num scH)) where num = fromIntegral delayMilliseconds :: Int -> IO ()-delayMilliseconds ms = threadDelay (10000 * ms)+delayMilliseconds ms = threadDelay (1000 * ms)
animate-example.cabal view
@@ -3,7 +3,7 @@ -- see: https://github.com/sol/hpack name: animate-example-version: 0.0.0+version: 0.1.0 synopsis: Animation for sprites description: Prototypical sprite animation with type-safety. category: Game@@ -16,6 +16,8 @@ cabal-version: >= 1.10 extra-source-files:+ dino.json+ dino.png package.yaml README.md stack.yaml
+ dino.json view
@@ -0,0 +1,37 @@+{+ "image": "dino.png",+ "alpha": [255,0,255],+ "clips": [+ [ 0, 0, 48, 48],+ [ 48, 0, 48, 48],+ [ 96, 0, 48, 48],+ [ 144, 0, 48, 48],+ [ 192, 0, 48, 48],+ [ 240, 0, 48, 48],+ [ 288, 0, 48, 48],+ [ 336, 0, 48, 48],+ [ 384, 0, 48, 48],+ [ 432, 0, 48, 48],+ [ 480, 0, 48, 48],+ [ 528, 0, 48, 48],+ [ 576, 0, 48, 48],+ [ 624, 0, 48, 48],+ [ 672, 0, 48, 48],+ [ 720, 0, 48, 48],+ [ 768, 0, 48, 48],+ [ 816, 0, 48, 48],+ [ 864, 0, 48, 48],+ [ 912, 0, 48, 48],+ [ 960, 0, 48, 48],+ [ 1008, 0, 48, 48],+ [ 1056, 0, 48, 48],+ [ 1104, 0, 48, 48]+ ],+ "animations": {+ "Idle": [ [0,0.2], [1,0.2], [2,0.2], [3,0.2] ],+ "Move": [ [4,0.05], [5,0.05], [6,0.05], [7,0.05], [8,0.05], [9,0.05] ],+ "Kick": [ [10,0.1], [11,0.1], [12,0.1] ],+ "Hurt": [ [13,0.1], [14,0.1], [15,0.1], [16,0.1] ],+ "Sneak": [ [17,0.1], [18,0.1], [19,0.1], [20,0.1], [21,0.1], [22,0.1], [23,0.1] ]+ }+}
+ dino.png view
binary file changed (absent → 2282 bytes)
package.yaml view
@@ -1,5 +1,5 @@ name: animate-example-version: '0.0.0'+version: '0.1.0' category: Game synopsis: Animation for sprites description: Prototypical sprite animation with type-safety.@@ -8,6 +8,8 @@ - package.yaml - README.md - stack.yaml+- dino.json+- dino.png ghc-options: -Wall default-extensions: - DuplicateRecordFields
stack.yaml view
@@ -3,6 +3,6 @@ - '.' - '..' extra-deps:-- 'animate-0.2.0'+- 'animate-0.5.0' flags: {} extra-package-dbs: []