{-
   Simple functions for drawing bitmapped text or stroke text.
   Sven Panne 2000.   mailto:Sven.Panne@informatik.uni-muenchen.de
-}

module Text(
   with2D, bitmapStringAt, strokeStringAt
) where

import GL
import GLU
import Graphics.UI.GLUT

with2D :: [IO ()] -> IO ()
with2D actions = do
   -- save parts of current state
   pushAttrib [DepthBufferBit', EnableBit, FogBit]

   -- nothing fancy here
   disable Texture2d'
   disable Lighting
   disable Fog
   disable DepthTest

   -- orthographic 1-to-1 projection
   matrixMode Projection
   pushMatrix
   loadIdentity
   ((_::GLint,_::GLint), (width, height)) <- get VarViewport
   ortho2D 0 ((fromIntegral :: GLsizei -> GLdouble) width)
           0 ((fromIntegral :: GLsizei -> GLdouble) height)
   
   -- small offset ensures that integer coordinates are mapped to pixels
   -- (see Red Book)
   matrixMode Modelview
   pushMatrix
   loadIdentity
   translate (Vector3 0.375 0.375 0 :: Vector3 GLfloat)

   sequence_ actions
  
   -- restore state
   popMatrix
   matrixMode Projection
   popMatrix
   matrixMode Modelview
   popAttrib

-- Attention: On some architectures raster ops have to wait until
-- the 3D pipeline has finished => performance drop!
bitmapStringAt :: RasterPos2 GLfloat -> String -> IO ()
bitmapStringAt pos str = do
   rasterPos pos
   renderString (BitmapFont Helvetica18) str

strokeStringAt :: RasterPos2 GLfloat -> String -> IO ()
strokeStringAt (RasterPos2 x y) str = do
   pushMatrix
   translate (Vector3 x y 0)
   scale (0.12 :: GLfloat) 0.12 1
   renderString (StrokeFont Roman) str
   popMatrix
