File: AARGB.hs

package info (click to toggle)
haskell-glut 2.1.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,936 kB
  • ctags: 25
  • sloc: haskell: 10,092; sh: 2,811; ansic: 53; makefile: 2
file content (104 lines) | stat: -rw-r--r-- 3,116 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
{-
   AARGB.hs (adapted from aargb.c which is (c) Silicon Graphics, Inc.)
   Copyright (c) Sven Panne 2002-2006 <sven.panne@aedion.de>
   This file is part of HOpenGL and distributed under a BSD-style license
   See the file libraries/GLUT/LICENSE

   This program draws shows how to draw anti-aliased lines. It draws two
   diagonal lines to form an X; when 'r' is typed in the window, the lines are
   rotated in opposite directions.
-}

import Data.Char ( toLower )
import Data.IORef ( IORef, newIORef )
import System.Exit ( exitWith, ExitCode(ExitSuccess) )
import Graphics.UI.GLUT

data State = State { rotAngle :: IORef Int }

makeState :: IO State
makeState = do
   r <- newIORef 0
   return $ State { rotAngle = r }

-- Initialize antialiasing for RGBA mode, including alpha blending, hint, and
-- line width. Print out implementation specific info on line width granularity
-- and width.
myInit :: IO ()
myInit = do
   g <- get smoothLineWidthGranularity
   putStrLn ("smoothLineWidthGranularity is " ++ show g)

   r <- get smoothLineWidthRange
   putStrLn ("smoothLineWidthRange is " ++ show r)

   lineSmooth $= Enabled

   blend $= Enabled
   blendFunc $= (SrcAlpha, OneMinusSrcAlpha)
   hint LineSmooth $= DontCare
   lineWidth $= 1.5

   clearColor $= Color4 0 0 0 0

-- Draw 2 diagonal lines to form an X
display :: State -> DisplayCallback
display state = do
   r <- get (rotAngle state)
   clear [ ColorBuffer ]

   -- resolve overloading, not needed in "real" programs
   let color3f = color :: Color3 GLfloat -> IO ()
       vertex2f = vertex :: Vertex2 GLfloat -> IO ()

   color3f (Color3 0 1 0)
   preservingMatrix $ do
      rotate (-(fromIntegral r :: GLfloat)) (Vector3 0 0 0.1)
      renderPrimitive Lines $ do
         vertex2f (Vertex2 (-0.5) 0.5)
         vertex2f (Vertex2 0.5 (-0.5))

   color3f (Color3 0 0 1)
   preservingMatrix $ do
      rotate (fromIntegral r :: GLfloat) (Vector3 0 0 0.1)
      renderPrimitive Lines $ do
         vertex2f (Vertex2 0.5 0.5)
         vertex2f (Vertex2 (-0.5) (-0.5))

   flush

reshape :: ReshapeCallback
reshape size@(Size w h) = do
   viewport $= (Position 0 0, size)
   matrixMode $= Projection
   loadIdentity
   let wf = fromIntegral w
       hf = fromIntegral h
   if w <= h
      then ortho2D (-1) 1 (-1*hf/wf) (1*hf/wf)
      else ortho2D (-1*wf/hf) (1*wf/hf) (-1) 1
   matrixMode $= Modelview 0
   loadIdentity

keyboard :: State -> KeyboardMouseCallback
keyboard state (Char c) Down _ _ = case toLower c of
   'r'   -> do rotAngle state  $~ ((`mod` 360) . (+ 30)); postRedisplay Nothing
   '\27' -> exitWith ExitSuccess
   _     -> return ()
keyboard _ _ _ _ _ = return ()

-- Main Loop
-- Open window with initial window size, title bar,
-- RGBA display mode, and handle input events.
main :: IO ()
main = do
   (progName, _args) <- getArgsAndInitialize
   initialDisplayMode $= [ SingleBuffered, RGBMode ]
   initialWindowSize $= Size 200 200
   createWindow progName
   state <- makeState
   myInit
   reshapeCallback $= Just reshape
   keyboardMouseCallback $= Just (keyboard state)
   displayCallback $= display state
   mainLoop