module Graphics.UI.GLUT.State (
windowBorderWidth, windowHeaderHeight, skipStaleMotionEvents,
rgba,
BufferDepth, rgbaBufferDepths, colorBufferDepth,
doubleBuffered, stereo,
accumBufferDepths, depthBufferDepth, stencilBufferDepth,
SampleCount, sampleCount, formatID,
fullScreenMode,
geometryVisualizeNormals,
vertexAttribCoord3, vertexAttribNormal, vertexAttribTexCoord2,
damaged,
elapsedTime,
screenSize, screenSizeMM,
hasKeyboard,
ButtonCount, numMouseButtons,
numSpaceballButtons,
DialCount, numDialsAndButtons,
numTabletButtons,
AxisCount, PollRate, joystickInfo,
supportedNumAuxBuffers, supportedSamplesPerPixel,
glutVersion, initState
) where
import Control.Monad ( unless )
import Data.StateVar ( GettableStateVar, makeGettableStateVar
, SettableStateVar, makeSettableStateVar
, StateVar, makeStateVar )
import Foreign.C.Types ( CInt )
import Foreign.Marshal.Alloc ( alloca )
import Foreign.Marshal.Array ( peekArray )
import Foreign.Storable ( peek )
import Graphics.Rendering.OpenGL ( AttribLocation(..), Size(..), GLenum, GLint )
import Graphics.UI.GLUT.Overlay
import Graphics.UI.GLUT.QueryUtils
import Graphics.UI.GLUT.Raw
import Graphics.UI.GLUT.Window
rgba :: GettableStateVar Bool
rgba = makeGettableStateVar$ simpleGet i2b glut_WINDOW_RGBA
type BufferDepth = Int
rgbaBufferDepths ::
GettableStateVar (BufferDepth, BufferDepth, BufferDepth, BufferDepth)
rgbaBufferDepths = makeGettableStateVar $ do
r <- simpleGet fromIntegral glut_WINDOW_RED_SIZE
g <- simpleGet fromIntegral glut_WINDOW_GREEN_SIZE
b <- simpleGet fromIntegral glut_WINDOW_BLUE_SIZE
a <- simpleGet fromIntegral glut_WINDOW_ALPHA_SIZE
return (r, g, b, a)
colorBufferDepth :: GettableStateVar BufferDepth
colorBufferDepth =
makeGettableStateVar $ simpleGet fromIntegral glut_WINDOW_BUFFER_SIZE
doubleBuffered :: GettableStateVar Bool
doubleBuffered = makeGettableStateVar $ simpleGet i2b glut_WINDOW_DOUBLEBUFFER
stereo :: GettableStateVar Bool
stereo = makeGettableStateVar $ simpleGet i2b glut_WINDOW_STEREO
accumBufferDepths ::
GettableStateVar (BufferDepth, BufferDepth, BufferDepth, BufferDepth)
accumBufferDepths = makeGettableStateVar $ do
r <- simpleGet fromIntegral glut_WINDOW_ACCUM_RED_SIZE
g <- simpleGet fromIntegral glut_WINDOW_ACCUM_GREEN_SIZE
b <- simpleGet fromIntegral glut_WINDOW_ACCUM_BLUE_SIZE
a <- simpleGet fromIntegral glut_WINDOW_ACCUM_ALPHA_SIZE
return (r, g, b, a)
depthBufferDepth :: GettableStateVar BufferDepth
depthBufferDepth =
makeGettableStateVar $ simpleGet fromIntegral glut_WINDOW_DEPTH_SIZE
stencilBufferDepth :: GettableStateVar BufferDepth
stencilBufferDepth =
makeGettableStateVar $ simpleGet fromIntegral glut_WINDOW_STENCIL_SIZE
type SampleCount = Int
sampleCount :: GettableStateVar SampleCount
sampleCount =
makeGettableStateVar $ simpleGet fromIntegral glut_WINDOW_NUM_SAMPLES
formatID :: GettableStateVar Int
formatID = makeGettableStateVar $ simpleGet fromIntegral glut_WINDOW_FORMAT_ID
fullScreenMode :: StateVar Bool
fullScreenMode = makeStateVar getFullScreenMode setFullScreenMode
getFullScreenMode :: IO Bool
getFullScreenMode = simpleGet i2b glut_FULL_SCREEN
setFullScreenMode :: Bool -> IO ()
setFullScreenMode newMode = do
oldMode <- getFullScreenMode
unless (newMode == oldMode) fullScreenToggle
geometryVisualizeNormals :: StateVar Bool
geometryVisualizeNormals =
makeStateVar
(simpleGet i2b glut_GEOMETRY_VISUALIZE_NORMALS)
(glutSetOption glut_GEOMETRY_VISUALIZE_NORMALS . b2i)
vertexAttribCoord3 :: SettableStateVar (Maybe AttribLocation)
vertexAttribCoord3 = setVertexAttribWith glutSetVertexAttribCoord3
setVertexAttribWith :: (GLint -> IO ()) -> SettableStateVar (Maybe AttribLocation)
setVertexAttribWith f = makeSettableStateVar $ f . getLocation
where getLocation = maybe (1) (\(AttribLocation l) -> fromIntegral l)
vertexAttribNormal :: SettableStateVar (Maybe AttribLocation)
vertexAttribNormal = setVertexAttribWith glutSetVertexAttribNormal
vertexAttribTexCoord2 :: SettableStateVar (Maybe AttribLocation)
vertexAttribTexCoord2 = setVertexAttribWith glutSetVertexAttribTexCoord2
elapsedTime :: GettableStateVar Int
elapsedTime = makeGettableStateVar $ simpleGet fromIntegral glut_ELAPSED_TIME
damaged :: Layer -> GettableStateVar Bool
damaged l = makeGettableStateVar $ layerGet isDamaged (marshalDamagedLayer l)
where isDamaged d = d /= 0 && d /= 1
marshalDamagedLayer x = case x of
Normal -> glut_NORMAL_DAMAGED
Overlay -> glut_OVERLAY_DAMAGED
screenSize :: GettableStateVar Size
screenSize =
makeGettableStateVar $ do
wpx <- simpleGet fromIntegral glut_SCREEN_WIDTH
hpx <- simpleGet fromIntegral glut_SCREEN_HEIGHT
return $ Size wpx hpx
screenSizeMM :: GettableStateVar Size
screenSizeMM =
makeGettableStateVar $ do
wmm <- simpleGet fromIntegral glut_SCREEN_WIDTH_MM
hmm <- simpleGet fromIntegral glut_SCREEN_HEIGHT_MM
return $ Size wmm hmm
hasKeyboard :: GettableStateVar Bool
hasKeyboard = makeGettableStateVar $ deviceGet i2b glut_HAS_KEYBOARD
type ButtonCount = Int
numMouseButtons :: GettableStateVar (Maybe ButtonCount)
numMouseButtons =
getDeviceInfo glut_HAS_MOUSE $
deviceGet fromIntegral glut_NUM_MOUSE_BUTTONS
numSpaceballButtons :: GettableStateVar (Maybe ButtonCount)
numSpaceballButtons =
getDeviceInfo glut_HAS_SPACEBALL $
deviceGet fromIntegral glut_NUM_SPACEBALL_BUTTONS
type DialCount = Int
numDialsAndButtons :: GettableStateVar (Maybe (DialCount, ButtonCount))
numDialsAndButtons =
getDeviceInfo glut_HAS_DIAL_AND_BUTTON_BOX $ do
d <- deviceGet fromIntegral glut_NUM_DIALS
b <- deviceGet fromIntegral glut_NUM_BUTTON_BOX_BUTTONS
return (d, b)
numTabletButtons :: GettableStateVar (Maybe ButtonCount)
numTabletButtons =
getDeviceInfo glut_HAS_TABLET $
deviceGet fromIntegral glut_NUM_TABLET_BUTTONS
type AxisCount = Int
type PollRate = Int
joystickInfo :: GettableStateVar (Maybe (ButtonCount, PollRate, AxisCount))
joystickInfo =
getDeviceInfo glut_HAS_JOYSTICK $ do
b <- deviceGet fromIntegral glut_JOYSTICK_BUTTONS
a <- deviceGet fromIntegral glut_JOYSTICK_AXES
r <- deviceGet fromIntegral glut_JOYSTICK_POLL_RATE
return (b, a, r)
supportedNumAuxBuffers :: GettableStateVar [Int]
supportedNumAuxBuffers = getModeValues glut_AUX
supportedSamplesPerPixel :: GettableStateVar [SampleCount]
supportedSamplesPerPixel = getModeValues (fromIntegral glut_MULTISAMPLE)
getModeValues :: Integral a => GLenum -> GettableStateVar [a]
getModeValues what = makeGettableStateVar $
alloca $ \sizeBuffer -> do
valuesBuffer <- glutGetModeValues what sizeBuffer
size <- peek sizeBuffer
fmap (map fromIntegral) $ peekArray (fromIntegral size) valuesBuffer
i2b :: CInt -> Bool
i2b = (/= 0)
b2i :: Bool -> CInt
b2i False = 0
b2i True = 1
getDeviceInfo :: GLenum -> IO a -> GettableStateVar (Maybe a)
getDeviceInfo dev act =
makeGettableStateVar $ do
hasDevice <- deviceGet i2b dev
if hasDevice then fmap Just act else return Nothing
glutVersion :: GettableStateVar String
glutVersion = makeGettableStateVar $ do
let isGLUT = not `fmap` isKnown "glutSetOption"
isFreeglut = not `fmap` isKnown "glutSetWindowStayOnTop"
showVersionPart x = shows (x `mod` 100)
showVersion v = showVersionPart (v `div` 10000) . showChar '.' .
showVersionPart (v `div` 100) . showChar '.' .
showVersionPart v
g <- isGLUT
if g
then return "GLUT 3.7"
else do f <- isFreeglut
v <- simpleGet id glut_VERSION
let prefix = if f then "freeglut" else "OpenGLUT"
return $ showString prefix . showChar ' ' . showVersion v $ ""
windowBorderWidth :: GettableStateVar Int
windowBorderWidth =
makeGettableStateVar (simpleGet fromIntegral glut_WINDOW_BORDER_WIDTH)
windowHeaderHeight :: GettableStateVar Int
windowHeaderHeight =
makeGettableStateVar (simpleGet fromIntegral glut_WINDOW_HEADER_HEIGHT)
skipStaleMotionEvents :: StateVar Bool
skipStaleMotionEvents =
makeStateVar
(simpleGet i2b glut_SKIP_STALE_MOTION_EVENTS)
(glutSetOption glut_SKIP_STALE_MOTION_EVENTS . b2i)
initState :: GettableStateVar Bool
initState = makeGettableStateVar$ simpleGet i2b glut_INIT_STATE