spice 0.1.0.0 → 0.1.0.1
raw patch · 10 files changed
+126/−26 lines, 10 files
Files
- spice.cabal +1/−1
- src/FRP/Spice.hs +13/−5
- src/FRP/Spice/Config.hs +16/−4
- src/FRP/Spice/Engine.hs +10/−1
- src/FRP/Spice/Game.hs +15/−2
- src/FRP/Spice/Input.hs +28/−4
- src/FRP/Spice/Input/Keyboard.hs +17/−4
- src/FRP/Spice/Input/Mouse.hs +3/−0
- src/FRP/Spice/Input/MousePosition.hs +3/−0
- src/FRP/Spice/Math/Vector.hs +20/−5
spice.cabal view
@@ -1,5 +1,5 @@ name: spice-version: 0.1.0.0+version: 0.1.0.1 synopsis: An FRP-based game engine written in Haskell. description: An FRP-based game engine written in Haskell. - See the homepage for more information. homepage: http://github.com/crockeo/spice
src/FRP/Spice.hs view
@@ -1,8 +1,16 @@-module FRP.Spice (module Rexport) where+{-|+ This module re-exports some other modules in the spice library so that you+ needn't import all of them explicitly.+-}+module FRP.Spice ( module FRP.Spice.Config+ , module FRP.Spice.Engine+ , module FRP.Spice.Input+ , module FRP.Spice.Game+ ) where ----------------------- -- Rexported Imports ---import FRP.Spice.Config as Rexport-import FRP.Spice.Engine as Rexport-import FRP.Spice.Input as Rexport-import FRP.Spice.Game as Rexport+import FRP.Spice.Config+import FRP.Spice.Engine+import FRP.Spice.Input+import FRP.Spice.Game
src/FRP/Spice/Config.hs view
@@ -1,3 +1,6 @@+{-|+ This module handles the configuration for the project.+-} module FRP.Spice.Config where --------------------@@ -7,7 +10,10 @@ ---------- -- Code -- --- The window config+{-|+ A datatype to configure the window settings when creating an OpenGL context+ using @'FRP.Spice.Engine.startEngine'@ in the engine.+-} data WindowConfig = WindowConfig { getWindowWidth :: Int , getWindowHeight :: Int , getWindowFullscreen :: Bool@@ -16,7 +22,15 @@ } deriving (Eq, Show, Read) --- The default window config+{-|+ The default for @'WindowConfig'@++ > getWindowWidth = 640+ > getWindowHeight = 480+ > getWindowFullscreen = False+ > getWindowResizeable = False+ > getWindowTitle = "Spice Application"+-} defaultWindowConfig :: WindowConfig defaultWindowConfig = WindowConfig { getWindowWidth = 640 , getWindowHeight = 480@@ -25,7 +39,5 @@ , getWindowTitle = "Spice Application" } --- A default instance for the window--- config instance Default WindowConfig where def = defaultWindowConfig
src/FRP/Spice/Engine.hs view
@@ -1,3 +1,7 @@+{-|+ This module handles starting the engine. This is done via the use of the+ @'startEngine'@ function.+-} module FRP.Spice.Engine where --------------------@@ -19,7 +23,12 @@ ---------- -- Code -- --- Starting the engine+{-|+ Starting the spice engine with the parameters prescribed in the+ @'WindowConfig'@. It updates and renders the @'Game'@ automatically so all+ you need to to is set up the @'WindowConfig'@ and make a datatype with an+ instance of @'Game'@.+-} startEngine :: Game a => WindowConfig -> a -> IO () startEngine wc game = do -- Opening the window
src/FRP/Spice/Game.hs view
@@ -1,3 +1,7 @@+{-|+ This module provides the API which people must use to define a game within+ the scope of Spice.+-} module FRP.Spice.Game where -------------------@@ -7,7 +11,16 @@ ---------- -- Code -- --- The game instance, to be used with updating and rendering+{-|+ A synonym to make the update function more self-documenting.+-}+type DeltaTime = Float++{-|+ The class which is to be used in the @'FRP.Spice.Engine.startEngine'@+ function. @'update'@ provides the API to update on every tick (purely), an+ @'render'@ provides the API to render every frame.+-} class Game a where- update :: Float -> Input -> a -> a+ update :: DeltaTime -> Input -> a -> a render :: a -> IO ()
src/FRP/Spice/Input.hs view
@@ -1,3 +1,6 @@+{-|+ This module provides everything relating to input in the scope of spice.+-} module FRP.Spice.Input ( module Rexport , Sinks (..) , Input (..)@@ -31,23 +34,36 @@ ---------- -- Code -- --- The sinks+{-|+ A wrapper around the sinks for the mouse position, the key states, and the+ mouse button states.+-} data Sinks = Sinks { mousePositionSinks :: Vector Float -> IO () , keyboardSinks :: Map Key (Bool -> IO ()) , mouseSinks :: Map MouseButton (Bool -> IO ()) } +{-|+ A container for all of the states themselves. It is used as a @'Signal'@+ @'Input'@ in the @'InputContainer'@ (which is necessary to use it within+ Elerea's FRP network).+-} data Input = Input { mousePosition :: Vector Float , keyboard :: Map Key Bool , mouse :: Map MouseButton Bool } --- The container to go around the+{-|+ A container around @'Sinks'@ and @'Signal'@ @'Input'@ so that one needn't+ pass around a tuple.+-} data InputContainer = InputContainer { getSinks :: Sinks , getInput :: Signal Input } --- Creating the input container for use in the program+{-|+ Making an @'InputContainer'@ filled with all necessary externals.+-} makeInputContainer :: IO InputContainer makeInputContainer = do mousePositionExternals <- MousePosition.externals@@ -72,17 +88,25 @@ , getInput = input } --- Creating callbacks for each type+{-|+ Creating a callback to update the mouse position's state.+-} makeMousePositionCallback :: InputContainer -> MousePosCallback makeMousePositionCallback ic (Position x y) = mousePositionSinks (getSinks ic) $ Vector (fromIntegral x / 320 - 1) ((-fromIntegral y) / 240 - 1) +{-|+ Creating a callback to update the keyboard's states.+-} makeKeyboardCallback :: InputContainer -> KeyCallback makeKeyboardCallback ic key state = keyboardSinks (getSinks ic) ! key $ case state of Press -> True Release -> False +{-|+ Creating a callback to update the mouse buttons' states.+-} makeMouseCallback :: InputContainer -> MouseButtonCallback makeMouseCallback ic button state = mouseSinks (getSinks ic) ! button $ case state of
src/FRP/Spice/Input/Keyboard.hs view
@@ -1,3 +1,6 @@+{-|+ The keyboard specific section of input.+-} module FRP.Spice.Input.Keyboard where --------------------@@ -11,18 +14,28 @@ ---------- -- Code -- --- A set of all keys available via GLFW+{-|+ A list of all possible keys used in the GLFW API.+-} keys :: [Key] keys = map toEnum [0 .. 318] --- Getting the externals for all of the keys+{-|+ A Map from @'Key'@ to the externals created for every single+ key.+-} externals :: IO (Map Key (Signal Bool, Bool -> IO ())) externals = liftM fromList $ mapM (\k -> liftM ((,) k) $ external False) keys --- Getting the signals from the externals+{-|+ Creating the @'Signal'@ of a @'Map'@ from @'Key'@ to @'Bool'@ from a @'Map'@+ of @'externals'@.+-} signals :: Map Key (Signal Bool, Bool -> IO ()) -> Signal (Map Key Bool) signals = T.sequence . fmap fst --- Getting the sinks from the externals+{-|+ Making a @'Map'@ from @'Key'@ to sink from a @'Map'@ of @'externals'@.+-} sinks :: Map Key (Signal Bool, Bool -> IO ()) -> Map Key (Bool -> IO ()) sinks = fmap snd
src/FRP/Spice/Input/Mouse.hs view
@@ -1,3 +1,6 @@+{-|+ The mouse (button) specific section of input.+-} module FRP.Spice.Input.Mouse where --------------------
src/FRP/Spice/Input/MousePosition.hs view
@@ -1,3 +1,6 @@+{-|+ The mouse (position) specific section of input.+-} module FRP.Spice.Input.MousePosition where --------------------
src/FRP/Spice/Math/Vector.hs view
@@ -1,3 +1,6 @@+{-|+ This module provides a @'Vector'@ datatype as described below.+-} module FRP.Spice.Math.Vector where --------------------@@ -8,18 +11,28 @@ ---------- -- Code -- --- The vector datatype+{-|+ A datatype that houses two values of a given type. It is provided with a+ @'Num'@ instance so that, when used with number types it can function+ similarly (though not exactly) to a mathematical vector.+-} data Vector a = Vector a a --- Default instance+{-|+ The default for the @'Vector'@.+-} instance Default a => Default (Vector a) where def = Vector def def --- Show instance+{-|+ Displaying the @'Vector'@.+-} instance Show a => Show (Vector a) where show (Vector x y) = mconcat ["Vector ", show x, " ", show y] --- Num instance+{-|+ Performs operations on the matching fields of the other @'Vector'@.+-} instance Num a => Num (Vector a) where (Vector x1 y1) + (Vector x2 y2) = Vector (x1 + x2) (y1 + y2) (Vector x1 y1) * (Vector x2 y2) = Vector (x1 * x2) (y1 * y2)@@ -30,6 +43,8 @@ signum (Vector x y) = Vector (signum x) (signum y) negate (Vector x y) = Vector (negate x) (negate y) --- Functor instance+{-|+ Maps over both values in the @'Vector'@+-} instance Functor Vector where fmap fn (Vector x y) = Vector (fn x) (fn y)