packages feed

apecs-brillo (empty) → 0.1.0

raw patch · 6 files changed

+205/−0 lines, 6 filesdep +apecsdep +apecs-physicsdep +base

Dependencies added: apecs, apecs-physics, base, brillo, containers, linear

Files

+ CHANGELOG.md view
@@ -0,0 +1,2 @@+## [0.1.0]+
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Jonas Carpay and Execute (c) 2017++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 Jonas Carpay nor Execute 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,3 @@+# apecs-brillo++Tools for simple [brillo](http://hackage.haskell.org/package/brillo)-based rendering.
+ apecs-brillo.cabal view
@@ -0,0 +1,37 @@+name:               apecs-brillo+version:            0.1.0+synopsis:           Simple brillo renderer for apecs+homepage:           https://github.com/fl215/apecs-brillo+license:            BSD3+license-file:       LICENSE+author:             Execute+maintainer:         execute@erisws.com+copyright:          Jonas Carpay, Execute+category:           Game engine+build-type:         Simple+extra-source-files:+    README.md+  , CHANGELOG.md+cabal-version:      >=1.10+description:+  apecs-gloss but instead it's for brillo.++library+  hs-source-dirs:   src+  exposed-modules:+    Apecs.Brillo+    Apecs.Physics.Brillo++  build-depends:+      apecs          >=0.7  && <0.10+    , apecs-physics  >=0.3  && <0.5+    , base           >=4.9  && <5+    , containers     >=0.5  && <0.8+    , brillo         ==1.13.3+    , linear         >=1.20 && <2++  default-language: Haskell2010++source-repository head+  type:     git+  location: https://github.com/fl215/apecs-brillo
+ src/Apecs/Brillo.hs view
@@ -0,0 +1,67 @@+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE TypeFamilies     #-}++module Apecs.Brillo+  ( module Apecs.Brillo+  , module G+  ) where++import           Data.Semigroup                   (Semigroup (..))+import           Brillo.Interface.IO.Game         as G+import           Linear.V2+import           Linear.Vector++import           Apecs++data Camera = Camera+  { camOffset :: V2 Float+  , camScale  :: Float+  } deriving (Eq, Show, Read)++instance Semigroup Camera where+  Camera x1 r1 <> Camera x2 r2 = Camera (x1 + x2) (r1 * r2)+instance Monoid Camera where+  mempty = Camera 0 1+  mappend = (<>)++-- | Apply a camera transformation to a picture+cameraTransform :: Camera -> Picture -> Picture+cameraTransform (Camera (V2 cx cy) cs) =+    Scale cs cs .  Translate (-cx) (-cy)++windowToWorld :: Camera -> (Float,Float) -> V2 Float+windowToWorld (Camera cx cs) (x,y) = V2 x y ^/ cs + cx++instance Component Camera where+  type Storage Camera = Global Camera++play+  :: (Has w IO Camera)+  => Display            -- ^ Display mode+  -> Color              -- ^ Background color+  -> Int                  -- ^ Desired FPS+  -> System w Picture -- ^ Drawing function+  -> (Event -> System w ()) -- ^ Event handling function+  -> (Float  -> System w ()) -- ^ Stepping function, with a time delta argument.+  -> System w ()+play disp col fps draw handle step = do+  w <- ask+  liftIO$ playIO disp col fps w draw' handle' step'+    where+      handle' event = runSystem $ handle event >> ask+      step'   dT    = runSystem $ step dT >> ask+      draw'         = runSystem $ do+        cam <- get global+        cameraTransform cam <$> draw++-- | Renders a picture given a component rendering function.+foldDraw :: (Get w IO c, Members w IO c)+         => (c -> Picture) -- ^ Component render function.+         -> System w Picture+foldDraw fn = cfold (\pic -> mappend pic . fn) mempty++-- | Monadically renders a picture given a component rendering function.+foldDrawM :: (Get w IO c, Members w IO c)+          => (c -> System w Picture) -- ^ Component render function.+          -> System w Picture+foldDrawM fn = cfoldM (\pic -> fmap (mappend pic) . fn) mempty
+ src/Apecs/Physics/Brillo.hs view
@@ -0,0 +1,66 @@+{-# LANGUAGE FlexibleContexts      #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE TypeFamilies          #-}++module Apecs.Physics.Brillo+  ( convexToPicture+  , Transform, worldTransform, drawBody+  , simulate+  , module Apecs.Brillo+  ) where++import           Control.Monad                         (foldM)+import           Data.Semigroup                        (Semigroup (..))+import           Brillo.Geometry.Angle                 (radToDeg)+import           Brillo.Interface.IO.Simulate          (simulateIO)++import           Apecs+import           Apecs.Physics++import           Apecs.Brillo++convexToPicture :: Convex -> Picture+convexToPicture (Convex [V2 x y] radius) = Translate (realToFrac x) (realToFrac y) $ Circle (realToFrac radius)+convexToPicture (Convex [a,b] _) = Line [v2ToTuple a, v2ToTuple b]+convexToPicture (Convex verts _) = Polygon (v2ToTuple <$> verts)++v2ToTuple :: V2 Double -> (Float, Float)+v2ToTuple (V2 x y) = (realToFrac x, realToFrac y)++type Transform = (Position, Angle)+worldTransform :: Transform -> Picture -> Picture+worldTransform (Position (V2 x y), Angle theta) =+ Translate (realToFrac x) (realToFrac y) .+ Rotate (negate . radToDeg . realToFrac $ theta)++drawBody :: Has w IO Physics => (Body, Transform, ShapeList) -> System w Picture+drawBody (btype, transform, ShapeList shapes) = color shColor . worldTransform transform <$> foldM foldfn mempty shapes+  where foldfn pic shapeEty = do+          Shape _ convex <- get shapeEty+          return . mappend pic $ convexToPicture convex+        shColor = case btype of+                    DynamicBody   -> red+                    KinematicBody -> green+                    StaticBody    -> blue++simulate+  :: ( Has w IO Camera+     , Has w IO Physics+     )+  => Display+  -> System w ()+simulate disp = do+  w <- ask+  liftIO $+     simulateIO disp+                black+                60+                w+                (\_     -> runSystem draw w)+                (\_ _ _ -> runSystem (stepPhysics (1/60)) w >> return w)++  where+    draw = do+      pic <- foldDrawM drawBody+      cam <- get global+      return $ cameraTransform cam pic