module FOV (
Settings,
Shape(..),
Direction(..),
newSettings,
setShape,
setOpaqueApply,
circle,
beam
) where
import Foreign.Ptr
import Foreign.ForeignPtr
type OpacityTestFn a = Ptr a -> Int -> Int -> IO Bool
foreign import ccall "wrapper" mkOpacityTestFn :: OpacityTestFn a -> IO (FunPtr (OpacityTestFn a))
type ApplyFn map src = Ptr map -> Int -> Int -> Int -> Int -> Ptr src -> IO ()
foreign import ccall "wrapper" mkApplyFn :: ApplyFn a b -> IO (FunPtr (ApplyFn a b))
data SettingsRaw
newtype Settings = Settings (ForeignPtr SettingsRaw)
data Shape
= CirclePrecalculate
| Circle
| Octagon
| Square
data Direction = East | NorthEast | North | NorthWest | West | SouthWest | South | SouthEast
rawshape :: (Num t) => Shape -> t
rawshape CirclePrecalculate = 0
rawshape Square = 1
rawshape Circle = 2
rawshape Octagon = 3
rawdirection :: (Num t) => Direction -> t
rawdirection East = 0
rawdirection NorthEast = 1
rawdirection North = 2
rawdirection NorthWest = 3
rawdirection West = 4
rawdirection SouthWest = 5
rawdirection South = 6
rawdirection SouthEast = 7
foreign import ccall unsafe new_fov_settings :: IO (Ptr SettingsRaw)
foreign import ccall unsafe fov_settings_set_shape :: Ptr SettingsRaw -> Int -> IO ()
foreign import ccall unsafe fov_settings_set_opaque_apply :: Ptr SettingsRaw -> Int -> IO ()
foreign import ccall unsafe fov_settings_set_apply_lighting_function :: Ptr SettingsRaw -> FunPtr (ApplyFn a b) -> IO ()
foreign import ccall unsafe fov_settings_set_opacity_test_function :: Ptr SettingsRaw -> FunPtr (OpacityTestFn a) -> IO ()
foreign import ccall unsafe "&fov_settings_free" fov_settings_free :: FunPtr (Ptr SettingsRaw -> IO ())
foreign import ccall fov_circle :: Ptr SettingsRaw -> Ptr map -> Ptr src -> Int -> Int -> Int -> IO ()
foreign import ccall fov_beam :: Ptr SettingsRaw -> Ptr map -> Ptr src -> Int -> Int -> Int -> Int -> Float -> IO ()
newSettings :: IO Settings
newSettings = do
s <- new_fov_settings >>= newForeignPtr fov_settings_free
return $ Settings s
setShape :: Settings -> Shape -> IO ()
setShape (Settings fps) sh = withForeignPtr fps $ \s -> do
fov_settings_set_shape s (rawshape sh)
setOpaqueApply :: Settings -> Bool -> IO ()
setOpaqueApply (Settings fps) a = withForeignPtr fps $ \s -> do
fov_settings_set_opaque_apply s (if a then (0) else (1))
circle :: Settings
-> (Int,Int)
-> Int
-> (Int -> Int -> IO ())
-> (Int -> Int -> IO Bool)
-> IO ()
circle (Settings fps) (x,y) r apply opaque = withForeignPtr fps $ \s -> do
fov_settings_set_apply_lighting_function s =<< mkApplyFn (\_ x' y' _ _ _ -> apply x' y')
fov_settings_set_opacity_test_function s =<< mkOpacityTestFn (\_ x' y' -> opaque x' y')
fov_circle s nullPtr nullPtr x y r
beam :: Settings
-> (Int,Int)
-> Int
-> Direction
-> Float
-> (Int -> Int -> IO ())
-> (Int -> Int -> IO Bool)
-> IO ()
beam (Settings fps) (x,y) r dir angle apply opaque = withForeignPtr fps $ \s -> do
fov_settings_set_apply_lighting_function s =<< mkApplyFn (\_ x' y' _ _ _ -> apply x' y')
fov_settings_set_opacity_test_function s =<< mkOpacityTestFn (\_ x' y' -> opaque x' y')
fov_beam s nullPtr nullPtr x y r (rawdirection dir) angle