{- Font.hs (adapted from font.c which is (c) Silicon Graphics, Inc) Copyright (c) Sven Panne 2002-2005 This file is part of HOpenGL and distributed under a BSD-style license See the file libraries/GLUT/LICENSE Draws some text in a bitmapped font. Uses bitmap and other pixel routines. Also demonstrates use of display lists. -} import Control.Monad ( zipWithM_ ) import Data.List ( genericDrop, genericLength ) import Foreign.C.String ( castCharToCChar ) import Foreign.Marshal.Array ( withArray ) import System.Exit ( exitWith, ExitCode(ExitSuccess) ) import Graphics.UI.GLUT space :: [GLubyte] space = [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ] letters :: [[GLubyte]] letters = [ [ 0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0x66, 0x3c, 0x18 ], [ 0x00, 0x00, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe ], [ 0x00, 0x00, 0x7e, 0xe7, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xe7, 0x7e ], [ 0x00, 0x00, 0xfc, 0xce, 0xc7, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc7, 0xce, 0xfc ], [ 0x00, 0x00, 0xff, 0xc0, 0xc0, 0xc0, 0xc0, 0xfc, 0xc0, 0xc0, 0xc0, 0xc0, 0xff ], [ 0x00, 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xfc, 0xc0, 0xc0, 0xc0, 0xff ], [ 0x00, 0x00, 0x7e, 0xe7, 0xc3, 0xc3, 0xcf, 0xc0, 0xc0, 0xc0, 0xc0, 0xe7, 0x7e ], [ 0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3 ], [ 0x00, 0x00, 0x7e, 0x18, 0x18, 0x18, 0x18, 0x18, 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xc3 ], [ 0x00, 0x00, 0xc3, 0xe7, 0xff, 0xff, 0xdb, 0xdb, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3 ], [ 0x00, 0x00, 0xc3, 0x66, 0x66, 0x3c, 0x3c, 0x18, 0x3c, 0x3c, 0x66, 0x66, 0xc3 ], [ 0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3c, 0x3c, 0x66, 0x66, 0xc3 ], [ 0x00, 0x00, 0xff, 0xc0, 0xc0, 0x60, 0x30, 0x7e, 0x0c, 0x06, 0x03, 0x03, 0xff ]] charToGLubyte :: Char -> GLubyte charToGLubyte = fromIntegral . castCharToCChar makeRasterFont :: IO DisplayList makeRasterFont = do rowAlignment Unpack $= 1 fontDisplayLists@(fontOffset:_) <- genObjectNames 128 let listsStartingWith ch = genericDrop (charToGLubyte ch) fontDisplayLists makeLetter dl letter = defineList dl Compile $ withArray letter $ bitmap (Size 8 13) (Vertex2 0 2) (Vector2 10 0) zipWithM_ makeLetter (listsStartingWith 'A') letters makeLetter (head (listsStartingWith ' ')) space return fontOffset myInit :: IO DisplayList myInit = do shadeModel $= Flat makeRasterFont printString :: DisplayList -> String -> IO () printString fontOffset s = preservingAttrib [ ListAttributes ] $ do listBase $= fontOffset withArray (map charToGLubyte s) $ callLists (genericLength s) UnsignedByte -- Everything above this line could be in a library -- that defines a font. To make it work, you've got -- to call makeRasterFont before you start making -- calls to printString. display :: DisplayList -> DisplayCallback display fontOffset = do let white = Color3 1 1 1 clear [ ColorBuffer ] -- resolve overloading, not needed in "real" programs let color3f = color :: Color3 GLfloat -> IO () rasterPos2i = rasterPos :: Vertex2 GLint -> IO () color3f white rasterPos2i (Vertex2 20 60) printString fontOffset "THE QUICK BROWN FOX JUMPS" rasterPos2i (Vertex2 20 40) printString fontOffset "OVER A LAZY DOG" flush reshape :: ReshapeCallback reshape size@(Size w h) = do viewport $= (Position 0 0, size) matrixMode $= Projection loadIdentity ortho 0 (fromIntegral w) 0 (fromIntegral h) (-1) 1 matrixMode $= Modelview 0 keyboard :: KeyboardMouseCallback keyboard (Char '\27') Down _ _ = exitWith ExitSuccess 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 300 100 initialWindowPosition $= Position 100 100 createWindow progName fontOffset <- myInit reshapeCallback $= Just reshape keyboardMouseCallback $= Just keyboard displayCallback $= display fontOffset mainLoop