module Graphics.Gloss.Internals.Interface.Window
( createWindow )
where
import Graphics.Gloss.Color
import Graphics.Gloss.Internals.Color
import Graphics.Gloss.Internals.Interface.Debug
import Graphics.Gloss.Internals.Interface.Callback (Callback)
import qualified Graphics.Gloss.Internals.Interface.Callback as Callback
import Graphics.UI.GLUT (($=), get)
import qualified Graphics.Rendering.OpenGL.GL as GL
import qualified Graphics.UI.GLUT as GLUT
import Control.Monad
createWindow
:: String
-> (Int, Int)
-> (Int, Int)
-> Color
-> [Callback]
-> IO ()
createWindow
windowName
size@(sizeX, sizeY)
pos @(posX, posY)
clearColor
callbacks
= do
let debug = False
(progName, args) <- GLUT.getArgsAndInitialize
glutVersion <- get GLUT.glutVersion
when debug
$ do putStr $ "* displayInWindow\n"
putStr $ " glutVersion = " ++ show glutVersion ++ "\n"
GLUT.initialWindowPosition
$= GL.Position
(fromIntegral posX)
(fromIntegral posY)
GLUT.initialWindowSize
$= GL.Size
(fromIntegral sizeX)
(fromIntegral sizeY)
GLUT.initialDisplayMode
$= [ GLUT.RGBMode
, GLUT.DoubleBuffered]
displayMode <- get GLUT.initialDisplayMode
displayModePossible <- get GLUT.displayModePossible
when debug
$ do putStr $ " displayMode = " ++ show displayMode ++ "\n"
++ " possible = " ++ show displayModePossible ++ "\n"
++ "\n"
when debug
$ do putStr $ "* creating window\n\n"
GLUT.createWindow windowName
GLUT.windowSize
$= GL.Size
(fromIntegral sizeX)
(fromIntegral sizeY)
GLUT.displayCallback $= callbackDisplay clearColor callbacks
GLUT.reshapeCallback $= Just (callbackReshape callbacks)
GLUT.keyboardMouseCallback $= Just (callbackKeyMouse callbacks)
GLUT.motionCallback $= Just (callbackMotion callbacks)
GLUT.idleCallback $= Just (callbackIdle callbacks)
GLUT.perWindowKeyRepeat $= GLUT.PerWindowKeyRepeatOff
GL.depthFunc $= Just GL.Always
GL.clearColor $= glColor4OfColor clearColor
when debug
$ do dumpGlutState
dumpFramebufferState
dumpFragmentState
when debug
$ do putStr $ "* entering mainloop..\n"
GLUT.mainLoop
when debug
$ putStr $ "* all done\n"
return ()
callbackDisplay clearColor callbacks
= do
GL.clear [GL.ColorBuffer, GL.DepthBuffer]
GL.color $ GL.Color4 0 0 0 (1 :: GL.GLfloat)
let funs = [f | (Callback.Display f) <- callbacks]
sequence_ funs
GLUT.swapBuffers
GLUT.reportErrors
return ()
callbackReshape callbacks size
= sequence_
$ map (\f -> f size)
[f | Callback.Reshape f <- callbacks]
callbackKeyMouse callbacks key keystate modifiers pos
= sequence_
$ map (\f -> f key keystate modifiers pos)
[f | Callback.KeyMouse f <- callbacks]
callbackMotion callbacks pos
= sequence_
$ map (\f -> f pos)
[f | Callback.Motion f <- callbacks]
callbackIdle callbacks
= sequence_
$ [f | Callback.Idle f <- callbacks]