This repository has been archived on 2025-03-03. You can view files and clone it, but cannot push or open issues or pull requests.
htanks/Render.hs

56 lines
1.4 KiB
Haskell
Raw Normal View History

2010-02-23 20:51:30 +01:00
module Render ( setup
, resize
, render
) where
import Game
import Tank
import Control.Monad.State
2010-02-23 20:51:30 +01:00
import Data.Fixed
import Data.Ratio
2010-02-23 20:51:30 +01:00
import Graphics.Rendering.OpenGL.GL (($=), GLfloat)
import Graphics.Rendering.OpenGL.GL.BeginEnd (renderPrimitive, PrimitiveMode(..))
import Graphics.Rendering.OpenGL.GL.CoordTrans (matrixMode, MatrixMode(..), viewport, Position(..), Size(..), loadIdentity, ortho)
import Graphics.Rendering.OpenGL.GL.Framebuffer (clear, ClearBuffer(..))
2010-02-23 20:51:30 +01:00
import Graphics.Rendering.OpenGL.GL.VertexSpec
setup :: Int -> Int -> IO ()
setup w h = do
resize w h
2010-02-23 20:51:30 +01:00
resize :: Int -> Int -> IO ()
resize w h = do
let wn = fromIntegral w
hn = fromIntegral h
aspect = wn/hn
matrixMode $= Projection
loadIdentity
ortho (-aspect) (aspect) (-1) 1 (-1) 1
matrixMode $= Modelview 0
viewport $= ((Position 0 0), (Size (fromIntegral w) (fromIntegral h)))
render :: Game ()
render = do
tank <- liftM head $ gets tanks
let x = toFloat . posx $ tank
y = toFloat . posy $ tank
liftIO $ do
2010-02-23 20:51:30 +01:00
clear [ColorBuffer]
renderPrimitive Triangles $ do
vertex $ Vertex2 (x-0.5 :: GLfloat) (y-0.5 :: GLfloat)
vertex $ Vertex2 (x+0.5 :: GLfloat) (y+0.5 :: GLfloat)
vertex $ Vertex2 (x+0.5 :: GLfloat) (y-0.5 :: GLfloat)
toFloat :: Real a => a -> GLfloat
toFloat = fromRational . toRational