Debian Codegraph @

src/Graphics/UI/GLUT/GameMode.hs

raw
1 --------------------------------------------------------------------------------
2 -- |
3 -- Module : Graphics.UI.GLUT.GameMode
4 -- Copyright : (c) Sven Panne 2002-2018
5 -- License : BSD3
6 --
7 -- Maintainer : Sven Panne <[email protected]>
8 -- Stability : stable
9 -- Portability : portable
10 --
11 -- In addition to the functionality offered by
12 -- 'Graphics.UI.GLUT.Window.fullScreen', GLUT offers an sub-API to change the
13 -- screen resolution, color depth, and refresh rate of the display for a single
14 -- full screen window. This mode of operation is called /game mode/, and is
15 -- restricted in various ways: No pop-up menus are allowed for this full screen
16 -- window, no other (sub-)windows can be created, and all other applications are
17 -- hidden.
18 --
19 -- /X Implementation Notes:/ Note that game mode is not fully supported in the
20 -- original GLUT for X, it is essentially the same as using
21 -- 'Graphics.UI.GLUT.Window.fullScreen'. The GLUT clone freeglut
22 -- (see <http://freeglut.sourceforge.net/>) does not have this restriction.
23 --
24 --------------------------------------------------------------------------------
25
26 module Graphics.UI.GLUT.GameMode (
27 GameModeCapability(..), GameModeCapabilityDescription(..),
28 gameModeCapabilities, enterGameMode, leaveGameMode,
29 BitsPerPlane, RefreshRate, GameModeInfo(..), gameModeInfo,
30 gameModeActive
31 ) where
32
33 import Control.Monad.IO.Class ( MonadIO(..) )
34 import Data.List ( intersperse )
35 import Data.StateVar ( GettableStateVar, makeGettableStateVar
36 , SettableStateVar, makeSettableStateVar )
37 import Foreign.C.String ( withCString )
38 import Graphics.Rendering.OpenGL ( Size(..), GLenum )
39
40 import Graphics.UI.GLUT.Raw
41 import Graphics.UI.GLUT.Types
42
43 --------------------------------------------------------------------------------
44
45 -- | Capabilities for 'gameModeCapabilities'
46
47 data GameModeCapability
48 = GameModeWidth -- ^ Width of the screen resolution in pixels
49 | GameModeHeight -- ^ Height of the screen resolution in pixels
50 | GameModeBitsPerPlane -- ^ Color depth of the screen in bits
51 | GameModeRefreshRate -- ^ Refresh rate in Hertz
52 | GameModeNum -- ^ Match the Nth frame buffer configuration
53 -- compatible with the given capabilities
54 -- (numbering starts at 1)
55 deriving ( Eq, Ord, Show )
56
57 gameModeCapabilityToString :: GameModeCapability -> String
58 gameModeCapabilityToString x = case x of
59 GameModeWidth -> "width"
60 GameModeHeight -> "height"
61 GameModeBitsPerPlane -> "bpp"
62 GameModeRefreshRate -> "hertz"
63 GameModeNum -> "num"
64
65 -- | A single capability description for 'gameModeCapabilities'.
66
67 data GameModeCapabilityDescription = Where' GameModeCapability Relation Int
68 deriving ( Eq, Ord, Show )
69
70 gameModeCapabilityDescriptionToString :: GameModeCapabilityDescription -> String
71 gameModeCapabilityDescriptionToString (Where' c r i) =
72 gameModeCapabilityToString c ++ relationToString r ++ show i
73
74 --------------------------------------------------------------------------------
75
76 -- | Controls the /game mode/ to be used when 'enterGameMode' is called. It is
77 -- described by a list of zero or more capability descriptions, which are
78 -- translated into a set of criteria used to select the appropriate screen
79 -- configuration. The criteria are matched in strict left to right order of
80 -- precdence. That is, the first specified criterion (leftmost) takes precedence
81 -- over the later criteria for non-exact criteria
82 -- ('Graphics.UI.GLUT.Initialization.IsGreaterThan',
83 -- 'Graphics.UI.GLUT.Initialization.IsLessThan', etc.). Exact criteria
84 -- ('Graphics.UI.GLUT.Initialization.IsEqualTo',
85 -- 'Graphics.UI.GLUT.Initialization.IsNotEqualTo') must match exactly so
86 -- precedence is not relevant.
87 --
88 -- To determine which configuration will actually be tried by 'enterGameMode'
89 -- (if any), use 'gameModeInfo'.
90 --
91 -- Note that even for game mode the current values of
92 -- 'Graphics.UI.GLUT.Initialization.initialDisplayMode'or
93 -- 'Graphics.UI.GLUT.Initialization.initialDisplayCapabilities' will
94 -- determine which buffers are available, if double buffering is used or not,
95 -- etc.
96
97 gameModeCapabilities :: SettableStateVar [GameModeCapabilityDescription]
98 gameModeCapabilities = makeSettableStateVar $ \ds ->
99 withCString (descriptionsToString ds) glutGameModeString
100
101 -- freeglut currently handles only simple game mode descriptions like "WxH:B@R",
102 -- so we try hard to use this format instead of the more general format allowed
103 -- by the "real" GLUT.
104 descriptionsToString :: [GameModeCapabilityDescription] -> String
105 descriptionsToString ds =
106 let ws = [ x | Where' GameModeWidth IsEqualTo x <- ds ]
107 hs = [ x | Where' GameModeHeight IsEqualTo x <- ds ]
108 bs = [ x | Where' GameModeBitsPerPlane IsEqualTo x <- ds ]
109 rs = [ x | Where' GameModeRefreshRate IsEqualTo x <- ds ]
110 allSimple = (length ws + length hs + length bs + length rs) == (length ds)
111 dimensionsOK = (null ws) == (null hs)
112 in if allSimple && dimensionsOK
113 then simpleCapStr ws hs bs rs
114 else generalCapStr ds
115
116 simpleCapStr :: [Int] -> [Int] -> [Int] -> [Int] -> String
117 simpleCapStr ws hs bs rs =
118 showCap "" ws ++ showCap "x" hs ++ showCap ":" bs ++ showCap "@" rs
119 where showCap _ [] = ""
120 showCap prefix (x:_) = prefix ++ show x
121
122 generalCapStr :: [GameModeCapabilityDescription] -> String
123 generalCapStr =
124 concat . intersperse " " . map gameModeCapabilityDescriptionToString
125
126 --------------------------------------------------------------------------------
127
128 -- | Enter /game mode/, trying to change resolution, refresh rate, etc., as
129 -- specified by the current value of 'gameModeCapabilities'. An identifier for
130 -- the game mode window and a flag, indicating if the display mode actually
131 -- changed, are returned. The game mode window is made the /current window/.
132 --
133 -- Re-entering /game mode/ is allowed, the previous game mode window gets
134 -- destroyed by this, and a new one is created.
135
136 enterGameMode :: MonadIO m => m (Window, Bool)
137 enterGameMode = do
138 w <- glutEnterGameMode
139 c <- getBool glut_GAME_MODE_DISPLAY_CHANGED
140 return (Window w, c)
141
142 --------------------------------------------------------------------------------
143
144 -- | Leave /game mode/, restoring the old display mode and destroying the game
145 -- mode window.
146
147 leaveGameMode :: MonadIO m => m ()
148 leaveGameMode = glutLeaveGameMode
149
150 --------------------------------------------------------------------------------
151
152 -- | The color depth of the screen, measured in bits (e.g. 8, 16, 24, 32, ...)
153
154 type BitsPerPlane = Int
155
156 -- | The refresh rate of the screen, measured in Hertz (e.g. 60, 75, 100, ...)
157
158 type RefreshRate = Int
159
160 data GameModeInfo = GameModeInfo Size BitsPerPlane RefreshRate
161 deriving ( Eq, Ord, Show )
162
163 --------------------------------------------------------------------------------
164
165 -- | Return 'Just' the mode which would be tried by the next call to
166 -- 'enterGameMode'. Returns 'Nothing' if the mode requested by the current value
167 -- of 'gameModeCapabilities' is not possible, in which case 'enterGameMode'
168 -- would simply create a full screen window using the current mode.
169
170 gameModeInfo :: GettableStateVar (Maybe GameModeInfo)
171 gameModeInfo = makeGettableStateVar $ do
172 possible <- getBool glut_GAME_MODE_POSSIBLE
173 if possible
174 then do
175 w <- glutGameModeGet glut_GAME_MODE_WIDTH
176 h <- glutGameModeGet glut_GAME_MODE_HEIGHT
177 let size = Size (fromIntegral w) (fromIntegral h)
178 b <- glutGameModeGet glut_GAME_MODE_PIXEL_DEPTH
179 r <- glutGameModeGet glut_GAME_MODE_REFRESH_RATE
180 return $ Just $ GameModeInfo size (fromIntegral b) (fromIntegral r)
181 else return Nothing
182
183 getBool :: MonadIO m => GLenum -> m Bool
184 getBool x = do
185 val <- glutGameModeGet x
186 return $ val /= 0
187
188 --------------------------------------------------------------------------------
189
190 -- | Contains 'True' when the /game mode/ is active, 'False' otherwise.
191
192 gameModeActive :: GettableStateVar Bool
193 gameModeActive = makeGettableStateVar $ getBool glut_GAME_MODE_ACTIVE