Language-Haskell

 view release on metacpan or  search on metacpan

hugs98-Nov2003/fptools/libraries/GLUT/Graphics/UI/GLUT/Initialization.hs  view on Meta::CPAN

initialWindowSize = makeStateVar getInitialWindowSize setInitialWindowSize

getInitialWindowSize :: IO Size
getInitialWindowSize = do
   w <- simpleGet fromIntegral glut_INIT_WINDOW_WIDTH
   h <- simpleGet fromIntegral glut_INIT_WINDOW_HEIGHT
   return $ Size w h

setInitialWindowSize :: Size -> IO ()
setInitialWindowSize (Size w h) =
   glutInitWindowSize (fromIntegral w) (fromIntegral h)

foreign import CALLCONV unsafe "glutInitWindowSize" glutInitWindowSize ::
   CInt -> CInt -> IO ()

--------------------------------------------------------------------------------

-- | A single aspect of a window which is to be created, used in conjunction
-- with 'initialDisplayMode'.

data DisplayMode
   = RGBAMode           -- ^ Select an RGBA mode window. This is the default if neither 'RGBAMode' nor 'IndexMode' are specified.
   | RGBMode            -- ^ An alias for 'RGBAMode'.
   | IndexMode          -- ^ Select a color index mode window. This overrides 'RGBAMode' if it is also specified.
   | LuminanceMode      -- ^ Select a window with a \"luminance\" color model. This model provides the functionality of OpenGL\'s
                        --   RGBA color model, but the green and blue components are not maintained in the frame buffer. Instead
                        --   each pixel\'s red component is converted to an index between zero and  'Graphics.UI.GLUT.Colormap.numColorMapEntries'
                        --   and looked up in a per-window color map to determine the color of pixels within the window. The initial
                        --   colormap of 'LuminanceMode' windows is initialized to be a linear gray ramp, but can be modified with GLUT\'s
                        --   colormap actions. /Implementation Notes:/ 'LuminanceMode' is not supported on most OpenGL platforms.
   | WithAlphaComponent -- ^ Select a window with an alpha component to the color buffer(s).
   | WithAccumBuffer    -- ^ Select a window with an accumulation buffer.
   | WithDepthBuffer    -- ^ Select a window with a depth buffer.
   | WithStencilBuffer  -- ^ Select a window with a stencil buffer.
   | SingleBuffered     -- ^ Select a single buffered window. This is the default if neither 'DoubleBuffered' nor 'SingleBuffered' are specified.
   | DoubleBuffered     -- ^ Select a double buffered window. This overrides 'SingleBuffered' if it is also specified.
   | Multisampling      -- ^ Select a window with multisampling support. If multisampling is not available, a non-multisampling
                        --   window will automatically be chosen. Note: both the OpenGL client-side and server-side implementations
                        --   must support the @GLX_SAMPLE_SGIS@ extension for multisampling to be available.
   | Stereoscopic       -- ^ Select A Stereo Window.
   deriving ( Eq, Ord, Show )

marshalDisplayMode :: DisplayMode -> CUInt
marshalDisplayMode m = case m of
   RGBAMode           -> glut_RGBA
   RGBMode            -> glut_RGB
   IndexMode          -> glut_INDEX
   SingleBuffered     -> glut_SINGLE
   DoubleBuffered     -> glut_DOUBLE
   WithAccumBuffer    -> glut_ACCUM
   WithAlphaComponent -> glut_ALPHA
   WithDepthBuffer    -> glut_DEPTH
   WithStencilBuffer  -> glut_STENCIL
   Multisampling      -> glut_MULTISAMPLE
   Stereoscopic       -> glut_STEREO
   LuminanceMode      -> glut_LUMINANCE

--------------------------------------------------------------------------------

-- | Controls the /initial display mode/ used when creating top-level windows,
-- subwindows, and overlays to determine the OpenGL display mode for the
-- to-be-created window or overlay.
--
-- Note that 'RGBAMode' selects the RGBA color model, but it does not request any
-- bits of alpha (sometimes called an /alpha buffer/ or /destination alpha/)
-- be allocated. To request alpha, specify 'WithAlphaComponent'. The same
-- applies to 'LuminanceMode'.

initialDisplayMode :: StateVar [DisplayMode]
initialDisplayMode = makeStateVar getInitialDisplayMode setInitialDisplayMode

getInitialDisplayMode :: IO [DisplayMode]
getInitialDisplayMode = simpleGet i2dms glut_INIT_DISPLAY_MODE

i2dms :: CInt -> [DisplayMode]
i2dms bitfield =
   [ c | c <- [ RGBAMode, RGBMode, IndexMode, SingleBuffered, DoubleBuffered,
                WithAccumBuffer, WithAlphaComponent, WithDepthBuffer,
                WithStencilBuffer, Multisampling, Stereoscopic, LuminanceMode ]
       , (fromIntegral bitfield .&. marshalDisplayMode c) /= 0 ]

setInitialDisplayMode :: [DisplayMode] -> IO ()
setInitialDisplayMode = glutInitDisplayMode . toBitfield marshalDisplayMode

toBitfield :: (Bits b) => (a -> b) -> [a] -> b
toBitfield marshal = foldl (.|.) 0 . map marshal

foreign import CALLCONV unsafe "glutInitDisplayMode" glutInitDisplayMode ::
   CUInt -> IO ()

-- | Contains 'True' if the /current display mode/ is supported, 'False'
-- otherwise.

displayModePossible :: GettableStateVar Bool
displayModePossible =
   makeGettableStateVar $ simpleGet (/= 0) glut_DISPLAY_MODE_POSSIBLE

--------------------------------------------------------------------------------

-- | Capabilities for 'initialDisplayCapabilities', most of them are extensions
-- of 'DisplayMode'\'s constructors.

data DisplayCapability
   = DisplayRGBA  -- ^ Number of bits of red, green, blue, and alpha in the RGBA
                  --   color buffer. Default is \"'IsAtLeast' @1@\" for red,
                  --   green, blue, and alpha capabilities, and \"'IsEqualTo'
                  --   @1@\" for the RGBA color model capability.
   | DisplayRGB   -- ^ Number of bits of red, green, and blue in the RGBA color
                  --   buffer and zero bits of alpha color buffer precision.
                  --   Default is \"'IsAtLeast' @1@\" for the red, green, and
                  --   blue capabilities, and \"'IsNotLessThan' @0@\" for alpha
                  --   capability, and \"'IsEqualTo' @1@\" for the RGBA color
                  --   model capability.
   | DisplayRed   -- ^ Red color buffer precision in bits. Default is
                  --   \"'IsAtLeast' @1@\".
   | DisplayGreen -- ^ Green color buffer precision in bits. Default is
                  --   \"'IsAtLeast' @1@\".
   | DisplayBlue  -- ^ Blue color buffer precision in bits. Default is
                  --   \"'IsAtLeast' @1@\".
   | DisplayIndex -- ^ Boolean if the color model is color index or not. True is
                  --   color index. Default is \"'IsAtLeast' @1@\".
   | DisplayBuffer -- ^ Number of bits in the color index color buffer. Default

hugs98-Nov2003/fptools/libraries/GLUT/Graphics/UI/GLUT/Initialization.hs  view on Meta::CPAN

                  --   Default is \"'IsEqualTo' @1@\".
   | DisplayXTrueColor -- ^ Only recongized on GLUT implementations for the X Window
                  --   System, boolean indicating if the frame buffer
                  --   configuration\'s X visual is of type @TrueColor@. Default
                  --   is \"'IsEqualTo' @1@\".
   | DisplayXDirectColor -- ^ Only recongized on GLUT implementations for the X Window
                  --   System, boolean indicating if the frame buffer
                  --   configuration\'s X visual is of type @DirectColor@.
                  --   Default is \"'IsEqualTo' @1@\".
   deriving ( Eq, Ord, Show )

displayCapabilityToString :: DisplayCapability -> String
displayCapabilityToString x = case x of
   DisplayRGBA         -> "rgba"
   DisplayRGB          -> "rgb"
   DisplayRed          -> "red"
   DisplayGreen        -> "green"
   DisplayBlue         -> "blue"
   DisplayIndex        -> "index"
   DisplayBuffer       -> "buffer"
   DisplaySingle       -> "single"
   DisplayDouble       -> "double"
   DisplayAccA         -> "acca"
   DisplayAcc          -> "acc"
   DisplayAlpha        -> "alpha"
   DisplayDepth        -> "depth"
   DisplayStencil      -> "stencil"
   DisplaySamples      -> "samples"
   DisplayStereo       -> "stereo"
   DisplayLuminance    -> "luminance"
   DisplayNum          -> "num"
   DisplayConformant   -> "conformant"
   DisplaySlow         -> "slow"
   DisplayWin32PFD     -> "win32pfd"
   DisplayXVisual      -> "xvisual"
   DisplayXStaticGray  -> "xstaticgray"
   DisplayXGrayScale   -> "xgrayscale"
   DisplayXStaticColor -> "xstaticcolor"
   DisplayXPseudoColor -> "xpseudocolor"
   DisplayXTrueColor   -> "xtruecolor"
   DisplayXDirectColor -> "xdirectcolor"

-- | A single capability description for 'initialDisplayCapabilities'.

data DisplayCapabilityDescription
   = Where DisplayCapability Relation Int
     -- ^ A description of a capability with a specific relation to a numeric
     --   value.
   | With  DisplayCapability
     -- ^ When the relation and numeric value are not specified, each capability
     --   has a different default, see the different constructors of
     --   'DisplayCapability'.
   deriving ( Eq, Ord, Show )

displayCapabilityDescriptionToString ::  DisplayCapabilityDescription -> String
displayCapabilityDescriptionToString (Where c r i) =
   displayCapabilityToString c ++ relationToString r ++ show i
displayCapabilityDescriptionToString (With c) = displayCapabilityToString c

-- | Controls the /initial display mode/ used when creating top-level windows,
-- subwindows, and overlays to determine the OpenGL display mode for the
-- to-be-created window or overlay. It is described by a list of zero or more
-- capability descriptions, which are translated into a set of criteria used to
-- select the appropriate frame buffer configuration. The criteria are matched
-- in strict left to right order of precdence. That is, the first specified
-- criterion (leftmost) takes precedence over the later criteria for non-exact
-- criteria ('IsGreaterThan', 'IsLessThan', etc.). Exact criteria ('IsEqualTo',
-- 'IsNotEqualTo') must match exactly so precedence is not relevant.
--
-- Unspecified capability descriptions will result in unspecified criteria being
-- generated. These unspecified criteria help 'initialDisplayCapabilities'
-- behave sensibly with terse display mode descriptions.
--
-- Here is an example using 'initialDisplayCapabilities':
--
-- @
--    initialDisplayCapabilities $= [ With  DisplayRGB,
--                                    Where DisplayDepth IsAtLeast 16,
--                                    With  DisplaySamples,
--                                    Where DisplayStencil IsNotLessThan 2,
--                                    With  DisplayDouble ]
-- @
--
-- The above call requests a window with an RGBA color model (but requesting
-- no bits of alpha), a depth buffer with at least 16 bits of precision but
-- preferring more, multisampling if available, at least 2 bits of stencil
-- (favoring less stencil to more as long as 2 bits are available), and double
-- buffering.

initialDisplayCapabilities :: SettableStateVar [DisplayCapabilityDescription]
initialDisplayCapabilities =
   makeSettableStateVar $ \caps ->
      withCString
         (concat . intersperse " " . map displayCapabilityDescriptionToString $
          caps)
         glutInitDisplayString

foreign import CALLCONV unsafe "glutInitDisplayString" glutInitDisplayString ::
  CString -> IO ()



( run in 0.582 second using v1.01-cache-2.11-cpan-39bf76dae61 )