animate-sdl2 (empty) → 0.0.0
raw patch · 7 files changed
+209/−0 lines, 7 filesdep +aesondep +animatedep +basesetup-changed
Dependencies added: aeson, animate, base, sdl2, sdl2-image
Files
- LICENSE +30/−0
- README.md +5/−0
- Setup.hs +2/−0
- animate-sdl2.cabal +45/−0
- library/Animate/SDL.hs +91/−0
- package.yaml +28/−0
- stack.yaml +8/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Author name here (c) 2018++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Author name here nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,5 @@+# animate-sdl2++[`sdl2`](https://github.com/haskell-game/sdl2) is a commonly used media library.+[`animate`](https://github.com/jxv/animate) is a general animation library.+Combining `sdl2` and `animate`, `animate-sdl2` provides accessible glue-code to load and draw sprites.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ animate-sdl2.cabal view
@@ -0,0 +1,45 @@+-- This file has been generated from package.yaml by hpack version 0.20.0.+--+-- see: https://github.com/sol/hpack+--+-- hash: 448a6b0a9ac1bc24221e667d9613ddae1ca51be2436c5b31f763e2d2f4de9326++name: animate-sdl2+version: 0.0.0+synopsis: sdl2 + animate auxiliary library+description: sdl2 is a commonly used media library. animate is a general animation library. Combining animate and sdl2, animate-sdl2 provides accessible glue-code to load and draw sprites.+category: Game+homepage: https://github.com/jxv/animate-sdl2#readme+bug-reports: https://github.com/jxv/animate-sdl2/issues+maintainer: Joe Vargas+copyright: 2018 Joe Vargas+license: BSD3+license-file: LICENSE+build-type: Simple+cabal-version: >= 1.10++extra-source-files:+ package.yaml+ README.md+ stack.yaml++source-repository head+ type: git+ location: https://github.com/jxv/animate-sdl2++library+ hs-source-dirs:+ library+ default-extensions: GeneralizedNewtypeDeriving LambdaCase NamedFieldPuns ScopedTypeVariables OverloadedStrings+ ghc-options: -Wall+ build-depends:+ aeson >=0.11 && <2+ , animate >=0.6.1 && <1+ , base >=4.7 && <5+ , sdl2 >=2.4.0.1 && <2.5+ , sdl2-image >=2.0.0 && <3+ exposed-modules:+ Animate.SDL+ other-modules:+ Paths_animate_sdl2+ default-language: Haskell2010
+ library/Animate/SDL.hs view
@@ -0,0 +1,91 @@+module Animate.SDL+ ( loadTexture+ , loadSpriteSheetJSON+ , loadSpriteSheetYAML+ , unloadSpriteSheet+ , drawSprite+ , drawSpriteWithScalar+ , rectFromClip+ , offsetFromClip+ ) where++import qualified SDL+import qualified SDL.Raw.Video as Raw+import qualified SDL.Internal.Numbered as Numbered+import qualified SDL.Image as Image+import Foreign.C.Types+import Animate+import Data.Aeson (FromJSON)+import Data.Maybe (fromMaybe)+import SDL.Vect (V4(..), V2(..))++-- | Produce a new 'SDL.Surface' based on an existing one, but+-- optimized for blitting to the specified 'SDL.PixelFormat'.+convertSurface :: SDL.Surface -> SDL.PixelFormat -> IO SDL.Surface+convertSurface (SDL.Surface s _) pixFmt = do+ fmt <- Raw.allocFormat (Numbered.toNumber pixFmt)+ surface <- SDL.Surface <$> Raw.convertSurface s fmt 0 <*> pure Nothing+ surface <$ Raw.freeFormat fmt++loadSurface :: FilePath -> Maybe Color -> IO SDL.Surface+loadSurface path alpha = do+ surface0 <- Image.load path+ surface <- convertSurface surface0 SDL.RGBA8888+ SDL.freeSurface surface0+ case alpha of+ Just (r,g,b) -> SDL.surfaceColorKey surface SDL.$= (Just $ V4 r g b 0x00)+ Nothing -> return ()+ return surface++loadTexture :: SDL.Renderer -> FilePath -> Maybe Color -> IO SDL.Texture+loadTexture ren filePath color = do+ s <- loadSurface filePath color+ t <- SDL.createTextureFromSurface ren s+ SDL.freeSurface s+ return t++loadSpriteSheetJSON+ :: (KeyName key, Ord key, Bounded key, Enum key, FromJSON delay)+ => SDL.Renderer+ -> FilePath -- ^ Path of the sprite sheet info JSON file+ -> IO (SpriteSheet key SDL.Texture delay)+loadSpriteSheetJSON ren = readSpriteSheetJSON (loadTexture ren)++loadSpriteSheetYAML+ :: (KeyName key, Ord key, Bounded key, Enum key, FromJSON delay)+ => SDL.Renderer+ -> FilePath -- ^ Path of the sprite sheet info YAML file+ -> IO (SpriteSheet key SDL.Texture delay)+loadSpriteSheetYAML ren = readSpriteSheetYAML (loadTexture ren)++unloadSpriteSheet :: SpriteSheet key SDL.Texture delay -> IO ()+unloadSpriteSheet ss = SDL.destroyTexture $ ssImage ss++drawSprite :: SDL.Renderer -> SpriteSheet key SDL.Texture delay -> SpriteClip key -> V2 Int -> IO ()+drawSprite ren SpriteSheet{ssImage=sheet} clip loc = do+ let clip'@(SDL.Rectangle _ dim) = rectFromClip clip+ let offset = offsetFromClip clip+ let loc' = (+) <$> offset <*> fmap fromIntegral loc+ SDL.copy ren sheet (Just clip') (Just $ SDL.Rectangle (SDL.P loc') dim)++drawSpriteWithScalar :: SDL.Renderer -> V2 Float -> SpriteSheet key SDL.Texture delay -> SpriteClip key -> V2 Int -> IO ()+drawSpriteWithScalar ren scalar SpriteSheet{ssImage=sheet} clip loc = do+ let clip'@(SDL.Rectangle _ dim) = rectFromClip clip+ let offset = offsetFromClip clip+ let loc' =+ (+)+ <$> fmap round ((*) <$> fmap fromIntegral offset <*> scalar)+ <*> fmap fromIntegral loc+ SDL.rendererScale ren SDL.$= fmap CFloat scalar+ SDL.copy ren sheet (Just clip') (Just $ SDL.Rectangle (SDL.P loc') dim)+ SDL.rendererScale ren SDL.$= (V2 1 1)++rectFromClip :: SpriteClip key -> SDL.Rectangle CInt+rectFromClip SpriteClip{scX,scY,scW,scH} = SDL.Rectangle (SDL.P (V2 (num scX) (num scY))) (V2 (num scW) (num scH))+ where+ num = fromIntegral++offsetFromClip :: SpriteClip key -> V2 CInt+offsetFromClip SpriteClip{scOffset} = fromMaybe+ (V2 0 0)+ ((\(x,y) -> fromIntegral <$> V2 (-x) (-y)) <$> scOffset)
+ package.yaml view
@@ -0,0 +1,28 @@+name: animate-sdl2+version: '0.0.0'+github: jxv/animate-sdl2+license: BSD3+category: Game+synopsis: sdl2 + animate auxiliary library+description: sdl2 is a commonly used media library. animate is a general animation library. Combining animate and sdl2, animate-sdl2 provides accessible glue-code to load and draw sprites.+maintainer: Joe Vargas+copyright: 2018 Joe Vargas+extra-source-files:+- package.yaml+- README.md+- stack.yaml+ghc-options: -Wall+default-extensions:+- GeneralizedNewtypeDeriving+- LambdaCase+- NamedFieldPuns+- ScopedTypeVariables+- OverloadedStrings+library:+ dependencies:+ - base >=4.7 && <5+ - aeson >= 0.11 && <2+ - animate >= 0.6.1 && <1+ - sdl2 >= 2.4.0.1 && <2.5+ - sdl2-image >=2.0.0 && <3+ source-dirs: library
+ stack.yaml view
@@ -0,0 +1,8 @@+resolver: lts-11.3+packages:+- '.'+extra-deps:+- 'sdl2-ttf-2.0.2'+- 'animate-0.6.1'+flags: {}+extra-package-dbs: []