packages feed

free-game 1.0.5 → 1.1

raw patch · 8 files changed

+42/−38 lines, 8 filesdep ~boundingboxesdep ~lens

Dependency ranges changed: boundingboxes, lens

Files

FreeGame.hs view
@@ -16,9 +16,9 @@     runGameDefault,
     reGame,
     WindowMode(..),
-    BoundingBox(..),
-    inBoundingBox,
-    _Corners,
+    BoundingBox2,
+    Box(..),
+    isInside,
     delay,
     tick,
     foreverFrame,
@@ -109,7 +109,7 @@ import Data.Color
 import Data.Color.Names
 import Linear
-import Data.BoundingBox.Dim2 (BoundingBox(..), inBoundingBox, _Corners)
+import Data.BoundingBox
 import Control.Monad.Trans.Iter
 
 -- | 'Game' is a kind of procedure but you can also use it like a value.
@@ -119,11 +119,11 @@ --
 -- For more examples, see <https://github.com/fumieval/free-game/tree/master/examples>.
 
-runGame :: WindowMode -> BoundingBox Double -> Game a -> IO (Maybe a)
+runGame :: WindowMode -> BoundingBox2 -> Game a -> IO (Maybe a)
 runGame = GLFW.runGame
 
 runGameDefault :: Game a -> IO (Maybe a)
-runGameDefault = runGame Windowed (BoundingBox 0 0 640 480)
+runGameDefault = runGame Windowed (Box (V2 0 0) (V2 640 480))
 
 instance MonadIO Frame where
     liftIO = embedIO
FreeGame/Backend/GLFW.hs view
@@ -19,7 +19,7 @@ import Control.Monad.Reader
 import Data.IORef
 import Data.Reflection
-import Data.BoundingBox.Dim2
+import Data.BoundingBox
 import FreeGame.Class
 import FreeGame.Internal.Finalizer
 import FreeGame.UI
@@ -48,7 +48,7 @@ mouseButtonCallback mouseBuffer _ btn GLFW.MouseButtonState'Pressed _ = modifyIORef' mouseBuffer $ Map.adjust buttonDown (fromEnum btn)
 mouseButtonCallback mouseBuffer _ btn GLFW.MouseButtonState'Released _ = modifyIORef' mouseBuffer $ Map.adjust buttonUp (fromEnum btn)
 
-runGame :: WindowMode -> BoundingBox Double -> IterT (F UI) a -> IO (Maybe a)
+runGame :: WindowMode -> BoundingBox2 -> IterT (F UI) a -> IO (Maybe a)
 runGame mode bbox m = G.withGLFW mode bbox (execGame m)
 
 initialKeyBuffer :: Map.Map Key ButtonState
@@ -154,7 +154,7 @@         modifyIORef' given (f:)
     cont
 runUI (GetBoundingBox cont) = liftIO (readIORef (G.refRegion given)) >>= cont
-runUI (SetBoundingBox bbox@(view (size C)-> V2 w h) cont) = do
+runUI (SetBoundingBox bbox@(view (size zero)-> V2 w h) cont) = do
     liftIO $ GLFW.setWindowSize (G.theWindow given) (floor w) (floor h)
     liftIO $ writeIORef (G.refRegion given) bbox
     cont
FreeGame/Data/Font.hs view
@@ -25,10 +25,10 @@ import Control.Monad.IO.Class
 import Control.Monad
 import Data.IORef
+import Data.BoundingBox
 import qualified Data.Map as M
 import qualified Data.Vector.Storable as V
 import Linear
-import Data.BoundingBox.Dim2
 import FreeGame.Class
 import FreeGame.Data.Bitmap
 import FreeGame.Internal.Finalizer
@@ -51,7 +51,7 @@ import Codec.Picture.RGBA8
 
 -- | Font object
-data Font = Font FT_Face (Double, Double) (BoundingBox Double) (IORef (M.Map (Double, Char) RenderedChar))
+data Font = Font FT_Face (Double, Double) (Box V2 Double) (IORef (M.Map (Double, Char) RenderedChar))
 
 -- | Create a 'Font' from the given file.
 loadFontFromFile :: MonadIO m => FilePath -> m Font
@@ -62,8 +62,9 @@     asc <- peek (ascender f)
     desc <- peek (descender f)
     u <- fromIntegral <$> peek (units_per_EM f)
-    let box = BoundingBox (fromIntegral (xMin b)/u) (fromIntegral (yMin b)/u)
-                          (fromIntegral (xMax b)/u) (fromIntegral (yMax b)/u)
+    let box = fmap (/u) $ Box
+            (V2 (fromIntegral (xMin b)) (fromIntegral (yMin b)))
+            (V2 (fromIntegral (xMax b)) (fromIntegral (yMax b)))
     Font f (fromIntegral asc/u, fromIntegral desc/u) box <$> newIORef M.empty
 
 loadFont :: MonadIO m => FilePath -> m Font
@@ -78,7 +79,7 @@ metricsDescent (Font _ (_, d) _ _) = d
 
 -- | Get the font's boundingbox.
-fontBoundingBox :: Font -> BoundingBox Double
+fontBoundingBox :: Font -> Box V2 Double
 fontBoundingBox (Font _ _ b _) = b
 
 runFreeType :: IO CInt -> IO ()
FreeGame/Internal/GLFW.hs view
@@ -4,10 +4,11 @@ import Control.Bool
 import Control.Applicative
 import Control.Monad.IO.Class
+import Control.Lens
 import Data.Color
 import Data.IORef
 import FreeGame.Types
-import Data.BoundingBox.Dim2
+import Data.BoundingBox
 import Graphics.Rendering.OpenGL.GL.StateVar
 import Graphics.Rendering.OpenGL.Raw.ARB.Compatibility
 import Linear
@@ -18,13 +19,14 @@ import qualified Data.Vector.Storable.Mutable as MV
 import Codec.Picture
 import Codec.Picture.RGBA8
+import qualified GHC.IO.Encoding as Encoding
 
 data System = System
     { refFrameCounter :: IORef Int
     , refFPS :: IORef Int
     , theFPS :: IORef Int
     , currentFPS :: IORef Int
-    , refRegion :: IORef (BoundingBox Double)
+    , refRegion :: IORef BoundingBox2
     , theWindow :: GLFW.Window
     }
 
@@ -137,7 +139,7 @@ 
 beginFrame :: System -> IO ()
 beginFrame sys = do
-    BoundingBox wl wt wr wb <- fmap realToFrac <$> readIORef (refRegion sys)
+    Box (V2 wl wt) (V2 wr wb) <- fmap realToFrac <$> readIORef (refRegion sys)
     GL.viewport $= (GL.Position 0 0, GL.Size (floor $ wr - wl) (floor $ wb - wt))
     GL.matrixMode $= GL.Projection
     GL.loadIdentity
@@ -158,8 +160,9 @@         else writeIORef (refFrameCounter sys) (succ n)
     GLFW.windowShouldClose (theWindow sys)
 
-withGLFW :: WindowMode -> BoundingBox Double -> (System -> IO a) -> IO a
-withGLFW mode bbox@(BoundingBox x0 y0 x1 y1) m = do
+withGLFW :: WindowMode -> BoundingBox2 -> (System -> IO a) -> IO a
+withGLFW mode bbox@(Box (V2 x0 y0) (V2 x1 y1)) m = do
+    Encoding.setForeignEncoding Encoding.utf8
     let title = "free-game"
         ww = floor $ x1 - x0
         wh = floor $ y1 - y0
@@ -175,7 +178,7 @@     GL.lineSmooth $= GL.Enabled
     GL.blend      $= GL.Enabled
     GL.blendFunc  $= (GL.SrcAlpha, GL.OneMinusSrcAlpha)
-    GL.shadeModel $= GL.Flat
+    -- GL.shadeModel $= GL.Flat
     GL.textureFunction $= GL.Combine
 
     GLFW.swapInterval 1
@@ -184,8 +187,7 @@     rbox <- newIORef bbox
 
     GLFW.setFramebufferSizeCallback win $ Just $ \_ w h -> do
-        BoundingBox x y _ _ <- readIORef rbox
-        writeIORef rbox $ BoundingBox x y (x + fromIntegral w) (y + fromIntegral h)
+        modifyIORef rbox $ size zero .~ fmap fromIntegral (V2 w h)
 
     sys <- System
         <$> newIORef 0
@@ -203,10 +205,7 @@ 
 screenshotFlipped :: System -> IO (Image PixelRGBA8)
 screenshotFlipped sys = do
-    BoundingBox x0 y0 x1 y1 <- readIORef (refRegion sys)
-    let w = floor $ x1 - x0
-        h = floor $ y1 - y0
-    
+    V2 w h <- fmap floor <$> view (size zero) <$> readIORef (refRegion sys)
     mv <- MV.unsafeNew (w * h * 4)
     GL.readBuffer $= GL.FrontBuffers
     MV.unsafeWith mv
FreeGame/Text.hs view
@@ -3,7 +3,7 @@ 
 import Control.Lens
 import Data.String
-import Data.BoundingBox.Dim2
+import Data.BoundingBox
 import FreeGame.Data.Font
 import FreeGame.Class
 import FreeGame.Instances ()
@@ -19,7 +19,7 @@     fromString str = mapM_ (\c -> liftF (TypeChar c ())) str
 
 -- | Render a 'TextT'.
-runTextT :: (FromFinalizer m, Monad m, Picture2D m) => Maybe (BoundingBox Double) -> Font -> Double -> TextT m a -> m a
+runTextT :: (FromFinalizer m, Monad m, Picture2D m) => Maybe (Box V2 Double) -> Font -> Double -> TextT m a -> m a
 runTextT bbox font siz = flip evalStateT (V2 x0 y0) . go where
     go m = lift (runFreeT m) >>= \r -> case r of
         Pure a -> return a
@@ -37,9 +37,9 @@                 else V2 x0 (view _y pen + advV)
             go cont
     advV = siz * (metricsAscent font - metricsDescent font) * 1.1
-    (V2 x0 y0, cond) = maybe (zero, const True) (\b -> (b ^. position TL, flip inBoundingBox b)) bbox
+    (V2 x0 y0, cond) = maybe (zero, const True) (\b -> (b ^. position zero, flip isInside b)) bbox
 
-runTextT_ :: (FromFinalizer m, Monad m, Picture2D m) => Maybe (BoundingBox Double) -> Font -> Double -> TextT m () -> m ()
+runTextT_ :: (FromFinalizer m, Monad m, Picture2D m) => Maybe (Box V2 Double) -> Font -> Double -> TextT m () -> m ()
 runTextT_ = runTextT
 {-# INLINE runTextT_ #-}
 
FreeGame/Types.hs view
@@ -13,16 +13,20 @@ module FreeGame.Types (
     WindowMode(..)
     , Vec2
+    , BoundingBox2
     , Key(..)
     , BlendMode(..)
     ) where
 
 import Linear.V2
 import Data.Typeable
+import Data.BoundingBox
 
 data WindowMode = Windowed | Resizable | FullScreen deriving (Show, Eq, Ord, Read)
 
 type Vec2 = V2 Double
+
+type BoundingBox2 = Box V2 Double
 
 data Key =
       KeyUnknown
FreeGame/UI.hs view
@@ -27,7 +27,7 @@ import qualified Data.Map as Map
 import FreeGame.Data.Bitmap (Bitmap)
 import Data.Color
-import Data.BoundingBox.Dim2
+import Data.BoundingBox
 import Control.Monad.Free.Church (F, iterM)
 import Control.Monad.Trans.Iter (IterT, foldM)
 import Control.Monad (join)
@@ -48,8 +48,8 @@     | ClearColor Color a
     | GetFPS (Int -> a)
     | ForkFrame (Frame ()) a
-    | GetBoundingBox (BoundingBox Double -> a)
-    | SetBoundingBox (BoundingBox Double) a
+    | GetBoundingBox (BoundingBox2 -> a)
+    | SetBoundingBox BoundingBox2 a
     deriving Functor
 
 type Game = IterT Frame
@@ -106,8 +106,8 @@     clearColor :: Color -> m ()
     -- | Get the actual FPS value.
     getFPS :: m Int
-    getBoundingBox :: m (BoundingBox Double)
-    setBoundingBox :: BoundingBox Double -> m ()
+    getBoundingBox :: m BoundingBox2
+    setBoundingBox :: BoundingBox2 -> m ()
     
 instance FreeGame UI where
     draw = Draw
free-game.cabal view
@@ -1,5 +1,5 @@ name:                free-game
-version:             1.0.5
+version:             1.1
 synopsis:            Create games for free
 description:
     free-game defines a monad that integrates features to create 2D games.
@@ -73,5 +73,5 @@     transformers >= 0.3,
     vector >= 0.9 && <0.12,
     void >= 0.5,
-    boundingboxes >= 0.1.1 && < 0.2,
-    lens >= 3.8
+    boundingboxes >= 0.2 && < 0.4,
+    lens >= 3.8 && < 5