{-# LANGUAGE CPP #-}
module Graphics.Rendering.OpenGL.GL.StringQueries (
vendor, renderer, glVersion, glExtensions, extensionSupported,
shadingLanguageVersion, majorMinor, ContextProfile'(..), contextProfile
) where
import Data.Bits
import Data.Char
#if !MIN_VERSION_base(4,8,0)
import Data.Functor( (<$>), (<$) )
#endif
import Data.Set ( member, toList )
import Data.StateVar as S
import Graphics.Rendering.OpenGL.GL.ByteString
import Graphics.Rendering.OpenGL.GL.QueryUtils
import Graphics.GL
import Text.ParserCombinators.ReadP as R
vendor :: GettableStateVar String
vendor = makeStringVar GL_VENDOR
renderer :: GettableStateVar String
renderer = makeStringVar GL_RENDERER
glVersion :: GettableStateVar String
glVersion = makeStringVar GL_VERSION
glExtensions :: GettableStateVar [String]
glExtensions = makeGettableStateVar (toList <$> getExtensions)
extensionSupported :: String -> GettableStateVar Bool
extensionSupported ext =
makeGettableStateVar (getExtensions >>= (return . member ext))
shadingLanguageVersion :: GettableStateVar String
shadingLanguageVersion = makeStringVar GL_SHADING_LANGUAGE_VERSION
data ContextProfile'
= CoreProfile'
| CompatibilityProfile'
deriving ( Eq, Ord, Show )
marshalContextProfile' :: ContextProfile' -> GLbitfield
marshalContextProfile' x = case x of
CoreProfile' -> GL_CONTEXT_CORE_PROFILE_BIT
CompatibilityProfile' -> GL_CONTEXT_COMPATIBILITY_PROFILE_BIT
contextProfile :: GettableStateVar [ContextProfile']
contextProfile = makeGettableStateVar (getInteger1 i2cps GetContextProfileMask)
i2cps :: GLint -> [ContextProfile']
i2cps bitfield =
[ c | c <- [ CoreProfile', CompatibilityProfile' ]
, (fromIntegral bitfield .&. marshalContextProfile' c) /= 0 ]
makeStringVar :: GLenum -> GettableStateVar String
makeStringVar = makeGettableStateVar . getStringWith . glGetString
majorMinor :: GettableStateVar String -> GettableStateVar (Int, Int)
majorMinor =
makeGettableStateVar . (runParser parseVersion (-1, -1) <$>) . S.get
runParser :: ReadP a -> a -> String -> a
runParser parser failed str =
case readP_to_S parser str of
[(v, "")] -> v
_ -> failed
parseVersion :: ReadP (Int, Int)
parseVersion = do
_prefix <-
("CL" <$ string "OpenGL ES-CL ") <++
("CM" <$ string "OpenGL ES-CM ") <++
("ES" <$ string "OpenGL ES " ) <++
("GL" <$ string "" )
major <- read <$> munch1 isDigit
minor <- char '.' >> read <$> munch1 isDigit
_release <- (char '.' >> munch1 (/= ' ')) <++ return ""
_vendorStuff <- (char ' ' >> R.get `manyTill` eof) <++ ("" <$ eof)
return (major, minor)