apecs-physics-gloss (empty) → 0.1.0.0
raw patch · 5 files changed
+138/−0 lines, 5 filesdep +apecsdep +apecs-physicsdep +basesetup-changed
Dependencies added: apecs, apecs-physics, base, gloss
Files
- LICENSE +30/−0
- README.md +2/−0
- Setup.hs +2/−0
- apecs-physics-gloss.cabal +25/−0
- src/Apecs/Physics/Gloss.hs +79/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Jonas Carpay (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 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,2 @@+# apecs-physics-gloss+Tools for simple gloss-based rendering, split out to avoid unnecessary dependencies if you roll our own rendering
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ apecs-physics-gloss.cabal view
@@ -0,0 +1,25 @@+name: apecs-physics-gloss+version: 0.1.0.0+synopsis: Gloss rendering for apecs-physics+description: Gloss rendering for apecs-physics+homepage: https://github.com/jonascarpay/apecs-physics#readme+license: BSD3+license-file: LICENSE+author: Jonas Carpay+maintainer: jonascarpay@gmail.com+copyright: MIT+category: Web+build-type: Simple+extra-source-files: README.md+cabal-version: >=1.10++library+ hs-source-dirs: src+ exposed-modules: Apecs.Physics.Gloss+ ghc-options: -O2+ build-depends: base >= 4.7 && < 5, apecs-physics, gloss, apecs >= 0.6+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/jonascarpay/apecs-physics
+ src/Apecs/Physics/Gloss.hs view
@@ -0,0 +1,79 @@+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE GeneralizedNewtypeDeriving #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE TypeFamilies #-}++module Apecs.Physics.Gloss+ ( BodyPicture (..), Camera (..),+ toPicture, drawWorld, applyView, mouseToWorld, v2ToTuple,+ defaultSimulate,+ ) where++import Apecs+import Apecs.Physics+import Data.Foldable (fold)+import Data.Semigroup (Semigroup(..))+import qualified Graphics.Gloss as G+import Graphics.Gloss.Geometry.Angle (radToDeg)+import qualified Graphics.Gloss.Interface.IO.Simulate as GS++newtype BodyPicture = BodyPicture G.Picture deriving Monoid+instance Semigroup BodyPicture where+ BodyPicture pa <> BodyPicture pb = BodyPicture (mappend pa pb)++instance Component BodyPicture where+ type Storage BodyPicture = Map BodyPicture++data Camera = Camera+ { gvOffset :: V2 Double+ , gvScale :: Double+ }++instance Semigroup Camera where+ Camera p1 z1 <> Camera p2 z2 = Camera (p1 + p2) (z1 * z2)+instance Monoid Camera where+ mempty = Camera 0 1+ mappend = (<>)++instance Component Camera where+ type Storage Camera = Global Camera++applyView :: Camera -> G.Picture -> G.Picture+applyView (Camera (V2 x y) scale) =+ G.Scale (realToFrac scale) (realToFrac scale) . G.Translate (realToFrac . negate $ x) (realToFrac . negate $ y)++mouseToWorld :: (Float,Float) -> Camera -> V2 Double+mouseToWorld (x,y) (Camera offset scale) = (/scale) <$> (V2 (realToFrac x) (realToFrac y))-offset++toPicture :: Convex -> G.Picture+toPicture (Convex [V2 x y] radius) = G.Translate (realToFrac x) (realToFrac y) $ G.Circle (realToFrac radius)+toPicture (Convex [a,b] _) = G.Line [v2ToTuple a, v2ToTuple b]+toPicture (Convex verts _) = G.Polygon (v2ToTuple <$> verts)++v2ToTuple :: V2 Double -> (Float, Float)+v2ToTuple (V2 x y) = (realToFrac x, realToFrac y)++drawWorld+ :: ( Has w IO Physics+ , Has w IO BodyPicture+ , Has w IO Camera)+ => System w G.Picture+drawWorld = do+ pics <- flip cfold [] $+ \ps ((Position (V2 x y), Angle theta, BodyPicture pic)) ->+ (G.Translate (realToFrac x) (realToFrac y) .+ G.Rotate (negate . radToDeg . realToFrac $ theta) $+ pic) : ps+ view <- get global+ return . applyView view . G.pictures $ pics++defaultSimulate+ :: ( Has w IO Physics+ , Has w IO BodyPicture+ , Has w IO Camera)+ => w -> String -> IO ()+defaultSimulate w name =+ GS.simulateIO (GS.InWindow name (640,480) (100,100)) G.black 60 w render stepper+ where+ render w = runSystem drawWorld w+ stepper _ dT w = runSystem (stepPhysics (1/60)) w >> return w