もう、コードがわかりやすいかどうかはどうでもよくなっている節がありますが。
こんどはHaskellで挑戦。
まずは、ウィンドウ上をなにかが動くところまで。
ghc --make moreComprehensible.hs
ってな感じでコンパイルしてください。
以下、コード。
import Graphics.UI.GLUT import Control.Exception import System.Exit import Data.IORef main = do pos <- newIORef (0, 0) getArgsAndInitialize initialDisplayMode $= [RGBAMode, DoubleBuffered] initialWindowPosition $= Position 100 100 initialWindowSize $= Size 320 240 createWindow "to write more comprehensible code" clearColor $= Color4 1.0 1.0 1.0 1.0 displayCallback $= display pos reshapeCallback $= Just reshape keyboardMouseCallback $= Just keyboardMouse addTimerCallback interval $ timer pos mainLoop interval = 1000 display pos = do drawPoint pos reshape (Size w h) = do viewport $= (Position 0 0, Size w h) loadIdentity ortho (-0.5) ((fromInteger $ toInteger w) - 0.5) ((fromInteger $ toInteger h) - 0.5) (-0.5) (-1.0) (1.0) keyboardMouse key keystate modifiers position = do case key of Char 'q' -> throwIO $ ExitException ExitSuccess _ -> return () timer pos = do modifyIORef pos (\ (x, y) -> (x + 10.0, y + 10.0)) drawPoint pos addTimerCallback interval $ timer pos drawPoint pos = do (x, y) <- readIORef pos clear [ColorBuffer] color $ Color3 (0.0::Double) 0.0 1.0 renderPrimitive Polygon $ mapM_ vertex [ Vertex3 ( -7 + x) ( -7 + y) 0.0, Vertex3 ( 0 + x) (-10 + y) 0.0, Vertex3 ( 7 + x) ( -7 + y) 0.0, Vertex3 ( 10 + x) ( 0 + y) 0.0, Vertex3 ( 7 + x) ( 7 + y) 0.0, Vertex3 ( 0 + x) ( 10 + y) 0.0, Vertex3 ( -7 + x) ( 7 + y) 0.0, Vertex3 (-10 + x) ( 0 + y) (0.0 :: GLfloat) ] swapBuffers