Skip to content

Commit 67b0f61

Browse files
committed
No weird CPP macro. Use MonadIO. Cleaned up imports.
1 parent 0c7e6d6 commit 67b0f61

23 files changed

+2306
-433
lines changed

GLUT.cabal

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ extra-source-files:
137137
examples/RedBook8/common/Makefile
138138
examples/RedBook8/Makefile
139139
examples/examples.mk
140-
include/HsGLUTExt.h
141140

142141
flag UseNativeWindowsLibraries
143142
description:
@@ -166,7 +165,6 @@ library
166165
Graphics.UI.GLUT.Callbacks.Registration
167166
Graphics.UI.GLUT.QueryUtils
168167
Graphics.UI.GLUT.Raw
169-
Graphics.UI.GLUT.Raw.APIEntry
170168
Graphics.UI.GLUT.Raw.Callbacks
171169
Graphics.UI.GLUT.Raw.Fonts
172170
Graphics.UI.GLUT.Raw.Functions
@@ -175,14 +173,14 @@ library
175173
c-sources:
176174
cbits/HsGLUT.c
177175
hs-source-dirs: src
178-
include-dirs: include
179176
build-depends:
180-
base >= 3 && < 5,
181-
array >= 0.3 && < 0.6,
182-
containers >= 0.3 && < 0.6,
183-
StateVar >= 1.1 && < 1.2,
184-
OpenGLRaw >= 2.3 && < 2.5,
185-
OpenGL >= 2.12 && < 2.13
177+
base >= 3 && < 5,
178+
array >= 0.3 && < 0.6,
179+
containers >= 0.3 && < 0.6,
180+
transformers >= 0.2 && < 0.5,
181+
StateVar >= 1.1 && < 1.2,
182+
OpenGLRaw >= 2.3 && < 2.5,
183+
OpenGL >= 2.12 && < 2.13
186184
default-language: Haskell2010
187185
other-extensions: CPP
188186
ghc-options: -Wall

include/HsGLUTExt.h

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/Graphics/UI/GLUT/Begin.hs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ module Graphics.UI.GLUT.Begin (
2222
ActionOnWindowClose(..), actionOnWindowClose
2323
) where
2424

25-
import Data.StateVar
26-
import Foreign.C.Types
25+
import Control.Monad.IO.Class ( MonadIO(..) )
26+
import Data.StateVar ( StateVar, makeStateVar )
27+
import Foreign.C.Types ( CInt )
28+
2729
import Graphics.UI.GLUT.QueryUtils
2830
import Graphics.UI.GLUT.Raw
2931

@@ -33,7 +35,7 @@ import Graphics.UI.GLUT.Raw
3335
-- callbacks that have been registered. This routine should be called at most
3436
-- once in a GLUT program.
3537

36-
mainLoop :: IO ()
38+
mainLoop :: MonadIO m => m ()
3739
mainLoop = glutMainLoop
3840

3941
--------------------------------------------------------------------------------
@@ -42,7 +44,7 @@ mainLoop = glutMainLoop
4244
-- This allows the application to control its own event loop and still use the
4345
-- GLUT package.
4446

45-
mainLoopEvent :: IO ()
47+
mainLoopEvent :: MonadIO m => m ()
4648
mainLoopEvent = glutMainLoopEvent
4749

4850
--------------------------------------------------------------------------------
@@ -57,7 +59,7 @@ mainLoopEvent = glutMainLoopEvent
5759
-- for one behaviour over the other he should contact the freeglut Programming
5860
-- Consortium and ask for the code to be fixed.
5961

60-
leaveMainLoop :: IO ()
62+
leaveMainLoop :: MonadIO m => m ()
6163
leaveMainLoop = glutLeaveMainLoop
6264

6365
--------------------------------------------------------------------------------

src/Graphics/UI/GLUT/Callbacks/Global.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ module Graphics.UI.GLUT.Callbacks.Global (
2121
Timeout, TimerCallback, addTimerCallback
2222
) where
2323

24-
import Control.Monad.Fix
25-
import Foreign.C.Types
26-
import Graphics.Rendering.OpenGL ( Position(..)
27-
, SettableStateVar
28-
, makeSettableStateVar )
24+
import Control.Monad.Fix ( mfix )
25+
import Data.StateVar ( SettableStateVar, makeSettableStateVar )
26+
import Foreign.C.Types ( CInt )
27+
import Graphics.Rendering.OpenGL ( Position(..) )
28+
2929
import Graphics.UI.GLUT.Callbacks.Registration
3030
import Graphics.UI.GLUT.Raw
3131

src/Graphics/UI/GLUT/Callbacks/Registration.hs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ module Graphics.UI.GLUT.Callbacks.Registration (
1919

2020
--------------------------------------------------------------------------------
2121

22-
import Control.Monad
23-
import Data.IORef
24-
import qualified Data.Map as Map ( empty, lookup, insert, delete )
25-
import Data.Map ( Map )
26-
import Foreign.Ptr
27-
import Graphics.Rendering.OpenGL ( get )
22+
import Control.Monad ( when )
23+
import Data.IORef ( IORef, newIORef, readIORef, writeIORef, modifyIORef )
24+
import qualified Data.Map as M
25+
import Data.StateVar ( get )
26+
import Foreign.Ptr ( FunPtr, nullFunPtr, freeHaskellFunPtr )
27+
import System.IO.Unsafe ( unsafePerformIO )
28+
2829
import Graphics.UI.GLUT.Raw
2930
import Graphics.UI.GLUT.Window
3031

@@ -86,22 +87,22 @@ modifyCallbackTable = modifyIORef theCallbackTable
8687

8788
--------------------------------------------------------------------------------
8889

89-
type CallbackTable a = Map CallbackID (FunPtr a)
90+
type CallbackTable a = M.Map CallbackID (FunPtr a)
9091

9192
emptyCallbackTable :: CallbackTable a
92-
emptyCallbackTable = Map.empty
93+
emptyCallbackTable = M.empty
9394

9495
lookupInCallbackTable :: CallbackID -> IO (Maybe (FunPtr a))
9596
lookupInCallbackTable callbackID =
96-
fmap (Map.lookup callbackID) getCallbackTable
97+
fmap (M.lookup callbackID) getCallbackTable
9798

9899
deleteFromCallbackTable :: CallbackID -> IO ()
99100
deleteFromCallbackTable callbackID =
100-
modifyCallbackTable (Map.delete callbackID)
101+
modifyCallbackTable (M.delete callbackID)
101102

102103
addToCallbackTable :: CallbackID -> FunPtr a -> IO ()
103104
addToCallbackTable callbackID funPtr =
104-
modifyCallbackTable (Map.insert callbackID funPtr)
105+
modifyCallbackTable (M.insert callbackID funPtr)
105106

106107
--------------------------------------------------------------------------------
107108
-- Another global mutable variable: The list of function pointers ready to be

src/Graphics/UI/GLUT/Callbacks/Window.hs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,13 @@ module Graphics.UI.GLUT.Callbacks.Window (
7474

7575
) where
7676

77-
import Data.Bits hiding ( shift )
78-
import Data.Char
79-
import Data.Maybe
80-
import Foreign.C.Types
81-
import Graphics.Rendering.OpenGL
77+
import Data.Bits ( (.&.) )
78+
import Data.Char ( chr )
79+
import Data.Maybe ( fromJust )
80+
import Data.StateVar ( SettableStateVar, makeSettableStateVar )
81+
import Foreign.C.Types ( CInt, CUInt )
82+
import Graphics.Rendering.OpenGL ( Position(..), Size(..) )
83+
8284
import Graphics.UI.GLUT.Callbacks.Registration
8385
import Graphics.UI.GLUT.Raw
8486
import Graphics.UI.GLUT.State

src/Graphics/UI/GLUT/Colormap.hs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ module Graphics.UI.GLUT.Colormap (
4141
transparentIndex
4242
) where
4343

44-
import Foreign.C.Types
45-
import Graphics.Rendering.OpenGL ( GLfloat, GLint, Color3(..), Index1(..)
46-
, StateVar, makeStateVar, GettableStateVar
47-
, makeGettableStateVar )
44+
import Control.Monad.IO.Class ( MonadIO(..) )
45+
import Data.StateVar ( GettableStateVar, makeGettableStateVar, StateVar, makeStateVar )
46+
import Foreign.C.Types ( CInt )
47+
import Graphics.Rendering.OpenGL.GL.VertexSpec ( Index1(..), Color3(..) )
48+
import Graphics.Rendering.OpenGL.Raw.Types ( GLint, GLfloat )
49+
4850
import Graphics.UI.GLUT.QueryUtils
4951
import Graphics.UI.GLUT.Raw
5052
import Graphics.UI.GLUT.Types
@@ -87,7 +89,7 @@ getColorMapEntry cell = do
8789
-- 'copyColormap' should only be called when both the /current window/ and the
8890
-- specified window are color index windows.
8991

90-
copyColormap :: Window -> IO ()
92+
copyColormap :: MonadIO m => Window -> m ()
9193
copyColormap (Window win) = glutCopyColormap win
9294

9395
--------------------------------------------------------------------------------

src/Graphics/UI/GLUT/Debugging.hs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ module Graphics.UI.GLUT.Debugging (
1717
reportErrors
1818
) where
1919

20-
import Graphics.Rendering.OpenGL ( Error(..), errors, get )
21-
import System.Environment
22-
import System.IO
20+
import Control.Monad.IO.Class ( MonadIO(..) )
21+
import Data.StateVar ( get )
22+
import Graphics.Rendering.OpenGL ( Error(..), errors )
23+
import System.Environment ( getProgName )
24+
import System.IO ( hPutStrLn, stderr )
2325

2426
--------------------------------------------------------------------------------
2527

@@ -28,10 +30,10 @@ import System.IO
2830
-- error flags are reset after this action, i.e. there are no pending errors
2931
-- left afterwards.
3032

31-
reportErrors :: IO ()
33+
reportErrors :: MonadIO m => m ()
3234
reportErrors = get errors >>= mapM_ reportError
3335

34-
reportError :: Error -> IO ()
35-
reportError (Error _ msg) = do
36+
reportError :: MonadIO m => Error -> m ()
37+
reportError (Error _ msg) = liftIO $ do
3638
pn <- getProgName
3739
hPutStrLn stderr ("GLUT: Warning in " ++ pn ++ ": GL error: " ++ msg)

src/Graphics/UI/GLUT/DeviceControl.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ module Graphics.UI.GLUT.DeviceControl (
1919
forceJoystickCallback
2020
) where
2121

22-
import Foreign.C.Types
23-
import Graphics.Rendering.OpenGL ( StateVar, makeStateVar )
22+
import Control.Monad.IO.Class ( MonadIO(..) )
23+
import Data.StateVar ( StateVar, makeStateVar )
24+
import Foreign.C.Types ( CInt )
25+
2426
import Graphics.UI.GLUT.QueryUtils
2527
import Graphics.UI.GLUT.Raw
2628

@@ -118,5 +120,5 @@ perWindowKeyRepeat =
118120
-- This is done in a synchronous fashion within the current context, i.e. when
119121
-- 'forceJoystickCallback' returns, the callback will have already happened.
120122

121-
forceJoystickCallback :: IO ()
123+
forceJoystickCallback :: MonadIO m => m ()
122124
forceJoystickCallback = glutForceJoystickFunc

src/Graphics/UI/GLUT/Fonts.hs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ module Graphics.UI.GLUT.Fonts (
2222
Font(..), BitmapFont(..), StrokeFont(..),
2323
) where
2424

25-
import Data.Char
26-
import Foreign.C.String
27-
import Foreign.C.Types
28-
import Foreign.Ptr
29-
import Graphics.Rendering.OpenGL
25+
import Control.Monad.IO.Class ( MonadIO(..) )
26+
import Data.Char ( ord )
27+
import Foreign.C.String ( withCString )
28+
import Foreign.C.Types ( CInt )
29+
import Foreign.Ptr ( castPtr )
30+
import Graphics.Rendering.OpenGL.Raw.Types ( GLint, GLfloat )
31+
3032
import Graphics.UI.GLUT.Raw
3133

3234
--------------------------------------------------------------------------------
@@ -44,20 +46,20 @@ class Font a where
4446
-- 'Graphics.Rendering.OpenGL.GL.CoordTrans.translate' is used to translate
4547
-- the current model view matrix to advance the width of the string.
4648

47-
renderString :: a -> String -> IO ()
49+
renderString :: MonadIO m => a -> String -> m ()
4850

4951
-- | For a bitmap font, return the width in pixels of a string. For a stroke
5052
-- font, return the width in units. While the width of characters in a font
5153
-- may vary (though fixed width fonts do not vary), the maximum height
5254
-- characteristics of a particular font are fixed.
5355

54-
stringWidth :: a -> String -> IO GLint
56+
stringWidth :: MonadIO m => a -> String -> m GLint
5557

5658
-- | (/freeglut only/) For a bitmap font, return the maximum height of the
5759
-- characters in the given font measured in pixels. For a stroke font,
5860
-- return the width in units.
5961

60-
fontHeight :: a -> IO GLfloat
62+
fontHeight :: MonadIO m => a -> m GLfloat
6163

6264
instance Font BitmapFont where
6365
renderString = bitmapString
@@ -72,49 +74,55 @@ instance Font StrokeFont where
7274

7375
--------------------------------------------------------------------------------
7476

75-
bitmapString :: BitmapFont -> String -> IO ()
77+
bitmapString :: MonadIO m => BitmapFont -> String -> m ()
7678
bitmapString f s = do
7779
i <- marshalBitmapFont f
7880
mapM_ (\c -> withChar c (glutBitmapCharacter i)) s
7981

80-
withChar :: Char -> (CInt -> IO a) -> IO a
82+
withChar :: MonadIO m => Char -> (CInt -> m a) -> m a
8183
withChar c f = f . fromIntegral . ord $ c
8284

8385
--------------------------------------------------------------------------------
8486

85-
strokeString :: StrokeFont -> String -> IO ()
87+
strokeString :: MonadIO m => StrokeFont -> String -> m ()
8688
strokeString f s = do
8789
i <- marshalStrokeFont f
8890
mapM_ (\c -> withChar c (glutStrokeCharacter i)) s
8991

9092
--------------------------------------------------------------------------------
9193

92-
bitmapLength :: BitmapFont -- ^ Bitmap font to use.
94+
bitmapLength :: MonadIO m
95+
=> BitmapFont -- ^ Bitmap font to use.
9396
-> String -- ^ String to return width of (not confined to 8
9497
-- bits).
95-
-> IO GLint -- ^ Width in pixels.
96-
bitmapLength f s = do
98+
-> m GLint -- ^ Width in pixels.
99+
bitmapLength f s = liftIO $ do
97100
i <- marshalBitmapFont f
98101
fmap fromIntegral $ withCString s (glutBitmapLength i . castPtr)
99102

100103
--------------------------------------------------------------------------------
101104

102-
strokeLength :: StrokeFont -- ^ Stroke font to use.
105+
strokeLength :: MonadIO m
106+
=> StrokeFont -- ^ Stroke font to use.
103107
-> String -- ^ String to return width of (not confined to 8
104108
-- bits).
105-
-> IO GLint -- ^ Width in units.
106-
strokeLength f s = do
109+
-> m GLint -- ^ Width in units.
110+
strokeLength f s = liftIO $ do
107111
i <- marshalStrokeFont f
108112
fmap fromIntegral $ withCString s (glutStrokeLength i . castPtr)
109113

110114
--------------------------------------------------------------------------------
111115

112-
bitmapHeight :: BitmapFont -- ^ Bitmap font to use.
113-
-> IO GLfloat -- ^ Height in pixels.
114-
bitmapHeight f = fmap fromIntegral $ glutBitmapHeight =<< marshalBitmapFont f
116+
bitmapHeight :: MonadIO m
117+
=> BitmapFont -- ^ Bitmap font to use.
118+
-> m GLfloat -- ^ Height in pixels.
119+
bitmapHeight f = liftIO $ do
120+
i <- marshalBitmapFont f
121+
fromIntegral `fmap` glutBitmapHeight i
115122

116123
--------------------------------------------------------------------------------
117124

118-
strokeHeight :: StrokeFont -- ^ Stroke font to use.
119-
-> IO GLfloat -- ^ Height in units.
125+
strokeHeight :: MonadIO m
126+
=> StrokeFont -- ^ Stroke font to use.
127+
-> m GLfloat -- ^ Height in units.
120128
strokeHeight f = glutStrokeHeight =<< marshalStrokeFont f

0 commit comments

Comments
 (0)