module SFML.Window.Window
(
module SFML.Window.WindowHandle
, WindowStyle(..)
, createWindow
, windowFromHandle
, destroy
, close
, isWindowOpen
, getWindowSettings
, pollEvent
, waitEvent
, getWindowPosition
, setWindowPosition
, getWindowSize
, setWindowSize
, setWindowTitle
, setWindowIcon
, setWindowVisible
, setMouseVisible
, setVSync
, setKeyRepeat
, setWindowActive
, requestFocus
, hasFocus
, display
, setFramerateLimit
, setJoystickThreshold
, getSystemHandle
, getMousePosition
, setMousePosition
)
where
import Control.Applicative ((<$>), (<*>))
import SFML.SFDisplayable
import SFML.SFResource
import SFML.System.Vector2
import SFML.Window.ContextSettings
import SFML.Window.Event
import SFML.Window.SFWindow
import SFML.Window.Types
import SFML.Window.VideoMode
import SFML.Window.WindowHandle
import Data.Bits ((.|.))
import Data.List (foldl')
import Foreign.C.String (CString, withCAString)
import Foreign.C.Types
import Foreign.Marshal.Alloc (alloca)
import Foreign.Marshal.Utils (with)
import Foreign.Ptr
import Foreign.Storable
data WindowStyle
= SFNone
| SFTitlebar
| SFResize
| SFClose
| SFFullscreen
| SFDefaultStyle
deriving (Eq, Bounded, Show)
instance Enum WindowStyle where
fromEnum SFNone = 0
fromEnum SFTitlebar = 1
fromEnum SFResize = 2
fromEnum SFClose = 4
fromEnum SFFullscreen = 8
fromEnum SFDefaultStyle = 7
toEnum 0 = SFNone
toEnum 1 = SFTitlebar
toEnum 2 = SFResize
toEnum 4 = SFClose
toEnum 8 = SFFullscreen
toEnum 7 = SFDefaultStyle
createWindow
:: VideoMode
-> String
-> [WindowStyle]
-> Maybe ContextSettings
-> IO Window
createWindow vm title styles ctxSettings =
with vm $ \ptrVM ->
withCAString title $ \ptrTitle ->
let style = foldl' (.|.) 0 $ fmap fromEnum styles
in case ctxSettings of
Nothing -> sfWindow_create_helper ptrVM ptrTitle (fromIntegral style) nullPtr
Just ctx -> with ctx $ sfWindow_create_helper ptrVM ptrTitle (fromIntegral style)
foreign import ccall unsafe "sfWindow_create_helper"
sfWindow_create_helper :: Ptr VideoMode -> CString -> CUInt -> Ptr ContextSettings -> IO Window
windowFromHandle
:: WindowHandle
-> Maybe ContextSettings
-> IO Window
windowFromHandle hwnd ctxSettings =
case ctxSettings of
Nothing -> sfWindow_createFromHandle hwnd nullPtr
Just ctx -> with ctx $ sfWindow_createFromHandle hwnd
foreign import ccall unsafe "sfWindow_createFromHandle"
sfWindow_createFromHandle :: WindowHandle -> Ptr ContextSettings -> IO Window
instance SFResource Window where
destroy = sfWindow_destroy
foreign import ccall unsafe "sfWindow_destroy"
sfWindow_destroy :: Window -> IO ()
instance SFDisplayable Window where
display = sfWindow_display
instance SFWindow Window where
close = sfWindow_close
isWindowOpen wnd = sfWindow_isOpen wnd >>= return . (/=0)
getWindowSettings wnd =
alloca $ \ptrCtxSettings -> do
sfWindow_getSettings_helper wnd ptrCtxSettings
peek ptrCtxSettings
pollEvent wnd =
alloca $ \ptrEvt -> do
result <- return . (/=0) =<< sfWindow_pollEvent wnd ptrEvt
case result of
True -> peek ptrEvt >>= return . Just
False -> return Nothing
waitEvent wnd =
alloca $ \ptr -> do
result <- sfWindow_waitEvent wnd ptr
case result of
0 -> return Nothing
_ -> peek ptr >>= return . Just
getWindowPosition wnd = alloca $ \vecPtr -> sfWindow_getPosition_helper wnd vecPtr >> peek vecPtr
setWindowPosition wnd pos = with pos $ \posPtr -> sfWindow_setPosition_helper wnd posPtr
getWindowSize wnd = alloca $ \vecPtr -> sfWindow_getSize_helper wnd vecPtr >> peek vecPtr
setWindowSize wnd size = with size $ \ptrSize -> sfWindow_setSize_helper wnd ptrSize
setWindowTitle wnd title = withCAString title $ \ptrTitle -> sfWindow_setTitle wnd ptrTitle
setWindowIcon = sfWindow_setIcon
setWindowVisible wnd val = sfWindow_setVisible wnd (fromIntegral . fromEnum $ val)
setMouseVisible wnd val = sfWindow_setMouseCursorVisible wnd (fromIntegral . fromEnum $ val)
setVSync wnd val = sfWindow_setVerticalSyncEnabled wnd (fromIntegral . fromEnum $ val)
setKeyRepeat wnd val = sfWindow_setKeyRepeatEnabled wnd (fromIntegral . fromEnum $ val)
setWindowActive wnd val = sfWindow_setActive wnd (fromIntegral . fromEnum $ val)
requestFocus wnd = sfWindow_requestFocus wnd
hasFocus wnd = ((/=0) . fromIntegral) <$> sfWindow_hasFocus wnd
setFramerateLimit wnd val = sfWindow_setFramerateLimit wnd (fromIntegral val)
setJoystickThreshold w t = sfWindow_setJoystickThreshold w (realToFrac t)
getSystemHandle = sfWindow_getSystemHandle
getMousePosition Nothing = alloca $ \ptr -> sfMouse_getPosition_helper (Window nullPtr) ptr >> peek ptr
getMousePosition (Just wnd) = alloca $ \ptr -> sfMouse_getPosition_helper wnd ptr >> peek ptr
setMousePosition pos Nothing = with pos $ \ptr -> sfMouse_setPosition_helper ptr (Window nullPtr)
setMousePosition pos (Just wnd) = with pos $ \ptr -> sfMouse_setPosition_helper ptr wnd
foreign import ccall unsafe "sfWindow_close"
sfWindow_close :: Window -> IO ()
foreign import ccall unsafe "sfWindow_isOpen"
sfWindow_isOpen :: Window -> IO CChar
foreign import ccall unsafe "sfWindow_getSettings_helper"
sfWindow_getSettings_helper :: Window -> Ptr ContextSettings -> IO ()
foreign import ccall unsafe "sfWindow_pollEvent"
sfWindow_pollEvent :: Window -> Ptr SFEvent -> IO CChar
foreign import ccall unsafe "sfWindow_waitEvent"
sfWindow_waitEvent :: Window -> Ptr SFEvent -> IO CInt
foreign import ccall unsafe "sfWindow_getPosition_helper"
sfWindow_getPosition_helper :: Window -> Ptr Vec2i -> IO ()
foreign import ccall unsafe "sfWindow_setPosition_helper"
sfWindow_setPosition_helper :: Window -> Ptr Vec2i -> IO ()
foreign import ccall unsafe "sfWindow_getSize_helper"
sfWindow_getSize_helper :: Window -> Ptr Vec2u -> IO ()
foreign import ccall unsafe "sfWindow_setSize_helper"
sfWindow_setSize_helper :: Window -> Ptr Vec2u -> IO ()
foreign import ccall unsafe "sfWindow_setTitle"
sfWindow_setTitle :: Window -> CString -> IO ()
foreign import ccall unsafe "sfWindow_setIcon"
sfWindow_setIcon :: Window -> Int -> Int -> Ptr a -> IO ()
foreign import ccall unsafe "sfWindow_setVisible"
sfWindow_setVisible :: Window -> CChar -> IO ()
foreign import ccall unsafe "sfWindow_setMouseCursorVisible"
sfWindow_setMouseCursorVisible :: Window -> CChar -> IO ()
foreign import ccall unsafe "sfWindow_setVerticalSyncEnabled"
sfWindow_setVerticalSyncEnabled :: Window -> CChar -> IO ()
foreign import ccall unsafe "sfWindow_setKeyRepeatEnabled"
sfWindow_setKeyRepeatEnabled :: Window -> CChar -> IO ()
foreign import ccall unsafe "sfWindow_setActive"
sfWindow_setActive :: Window -> CChar -> IO ()
foreign import ccall unsafe "sfWindow_requestFocus"
sfWindow_requestFocus :: Window -> IO ()
foreign import ccall unsafe "sfWindow_hasFocus"
sfWindow_hasFocus :: Window -> IO CInt
foreign import ccall unsafe "sfWindow_display"
sfWindow_display :: Window -> IO ()
foreign import ccall unsafe "sfWindow_setFramerateLimit"
sfWindow_setFramerateLimit :: Window -> CUInt -> IO ()
foreign import ccall unsafe "sfWindow_setJoystickThreshold"
sfWindow_setJoystickThreshold :: Window -> CFloat -> IO ()
foreign import ccall unsafe "sfWindow_getSystemHandle"
sfWindow_getSystemHandle :: Window -> IO WindowHandle
foreign import ccall unsafe "sfMouse_getPosition_helper"
sfMouse_getPosition_helper :: Window -> Ptr Vec2i -> IO ()
foreign import ccall unsafe "sfMouse_setPosition_helper"
sfMouse_setPosition_helper :: Ptr Vec2i -> Window -> IO ()