2010-02-23 20:51:30 +01:00
|
|
|
module Render ( setup
|
|
|
|
, resize
|
|
|
|
, render
|
2010-02-23 15:05:31 +01:00
|
|
|
) where
|
|
|
|
|
|
|
|
|
|
|
|
import Game
|
2010-02-25 02:15:26 +01:00
|
|
|
import Level
|
2010-02-23 23:31:11 +01:00
|
|
|
import Tank
|
2010-02-25 03:16:44 +01:00
|
|
|
import Texture
|
2010-02-25 02:15:26 +01:00
|
|
|
|
2010-02-23 15:05:31 +01:00
|
|
|
import Control.Monad.State
|
2010-02-23 20:51:30 +01:00
|
|
|
|
2010-02-24 02:42:10 +01:00
|
|
|
import Data.Fixed
|
2010-02-25 03:16:44 +01:00
|
|
|
import Data.Maybe
|
2010-02-24 02:42:10 +01:00
|
|
|
import Data.Ratio
|
2010-02-25 03:16:44 +01:00
|
|
|
import qualified Data.Map as M
|
|
|
|
|
|
|
|
import Bindings.GLPng
|
2010-02-24 02:42:10 +01:00
|
|
|
|
2010-02-25 04:38:14 +01:00
|
|
|
import Graphics.Rendering.OpenGL.GL (($=), GLfloat, GLdouble, Capability(..), Vector3(..))
|
2010-02-23 20:51:30 +01:00
|
|
|
import Graphics.Rendering.OpenGL.GL.BeginEnd (renderPrimitive, PrimitiveMode(..))
|
2010-03-02 23:22:44 +01:00
|
|
|
import Graphics.Rendering.OpenGL.GL.CoordTrans (matrixMode, MatrixMode(..), viewport, Position(..), Size(..), loadIdentity, preservingMatrix, ortho, translate, rotate)
|
2010-02-23 15:05:31 +01:00
|
|
|
import Graphics.Rendering.OpenGL.GL.Framebuffer (clear, ClearBuffer(..))
|
2010-02-25 04:38:14 +01:00
|
|
|
import Graphics.Rendering.OpenGL.GL.PerFragment (blend, blendFunc, BlendingFactor(..))
|
2010-02-25 03:48:17 +01:00
|
|
|
import Graphics.Rendering.OpenGL.GL.Texturing.Application (texture)
|
|
|
|
import Graphics.Rendering.OpenGL.GL.Texturing.Objects (textureBinding, TextureObject(..))
|
2010-02-25 02:15:26 +01:00
|
|
|
import Graphics.Rendering.OpenGL.GL.Texturing.Parameters (Repetition(..), Clamping(..), TextureFilter(..), MinificationFilter, MagnificationFilter)
|
2010-02-25 03:48:17 +01:00
|
|
|
import Graphics.Rendering.OpenGL.GL.Texturing.Specification (TextureTarget(..))
|
2010-02-23 20:51:30 +01:00
|
|
|
import Graphics.Rendering.OpenGL.GL.VertexSpec
|
|
|
|
|
|
|
|
|
2010-02-25 03:16:44 +01:00
|
|
|
texturePath :: Texture -> String
|
|
|
|
texturePath t
|
|
|
|
| t == TextureWood = "tex/Wood.png"
|
2010-02-25 04:38:14 +01:00
|
|
|
| t == TextureTank = "tex/Tank.png"
|
2010-02-25 03:16:44 +01:00
|
|
|
|
|
|
|
getTexture :: Texture -> Game TextureObject
|
|
|
|
getTexture t = do
|
|
|
|
ts <- gets textures
|
|
|
|
let tobj = M.lookup t ts
|
|
|
|
|
|
|
|
if (isJust tobj)
|
|
|
|
then
|
|
|
|
return $ fromJust tobj
|
|
|
|
else do
|
2010-02-25 04:38:14 +01:00
|
|
|
tex <- liftIO $ pngBind (texturePath t) BuildMipmap Alpha (Repeated, Repeat) (Linear', Just Linear') Linear' >>= return . TextureObject . fromIntegral . fst
|
2010-02-25 03:16:44 +01:00
|
|
|
modify $ \state -> state {textures = M.insert t tex ts}
|
|
|
|
return tex
|
|
|
|
|
|
|
|
|
2010-02-25 02:15:26 +01:00
|
|
|
setup :: Int -> Int -> Game ()
|
2010-02-23 23:31:11 +01:00
|
|
|
setup w h = do
|
|
|
|
resize w h
|
2010-02-25 04:38:14 +01:00
|
|
|
liftIO $ do
|
|
|
|
blend $= Enabled
|
|
|
|
blendFunc $= (SrcAlpha, OneMinusSrcAlpha)
|
2010-02-25 02:15:26 +01:00
|
|
|
|
2010-02-25 03:16:44 +01:00
|
|
|
-- cache textures
|
|
|
|
getTexture TextureWood
|
2010-02-25 04:38:14 +01:00
|
|
|
getTexture TextureTank
|
2010-02-25 03:16:44 +01:00
|
|
|
|
|
|
|
return ()
|
2010-02-23 23:31:11 +01:00
|
|
|
|
2010-02-25 02:15:26 +01:00
|
|
|
resize :: Int -> Int -> Game ()
|
2010-02-23 20:51:30 +01:00
|
|
|
resize w h = do
|
|
|
|
let wn = fromIntegral w
|
|
|
|
hn = fromIntegral h
|
2010-02-25 02:15:26 +01:00
|
|
|
aspect = fromReal (wn/hn)
|
2010-02-23 20:51:30 +01:00
|
|
|
|
2010-02-25 02:15:26 +01:00
|
|
|
lvl <- gets level
|
|
|
|
let s = max (0.5*(fromIntegral $ levelWidth lvl)/aspect) (0.5*(fromIntegral $ levelHeight lvl)) :: GLdouble
|
2010-02-23 20:51:30 +01:00
|
|
|
|
2010-02-25 02:15:26 +01:00
|
|
|
liftIO $ do
|
|
|
|
matrixMode $= Projection
|
|
|
|
loadIdentity
|
|
|
|
ortho (-s*aspect) (s*aspect) (-s) s (-1) 1
|
|
|
|
|
|
|
|
matrixMode $= Modelview 0
|
|
|
|
|
|
|
|
viewport $= ((Position 0 0), (Size (fromIntegral w) (fromIntegral h)))
|
2010-02-23 15:05:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
render :: Game ()
|
2010-02-23 23:31:11 +01:00
|
|
|
render = do
|
2010-03-02 23:22:44 +01:00
|
|
|
tanklist <- gets tanks
|
2010-02-25 03:48:17 +01:00
|
|
|
textureWood <- getTexture TextureWood
|
2010-02-25 04:38:14 +01:00
|
|
|
textureTank <- getTexture TextureTank
|
2010-02-23 23:31:11 +01:00
|
|
|
|
2010-03-02 23:22:44 +01:00
|
|
|
(lw, lh) <- gets level >>= \l -> return (fromIntegral . levelWidth $ l :: GLfloat, fromIntegral . levelHeight $ l :: GLfloat)
|
|
|
|
|
2010-02-23 23:31:11 +01:00
|
|
|
liftIO $ do
|
2010-02-23 20:51:30 +01:00
|
|
|
clear [ColorBuffer]
|
|
|
|
|
2010-02-25 03:48:17 +01:00
|
|
|
texture Texture2D $= Enabled
|
|
|
|
textureBinding Texture2D $= Just textureWood
|
|
|
|
|
|
|
|
renderPrimitive Quads $ do
|
|
|
|
texCoord $ TexCoord2 (0 :: GLfloat) (0 :: GLfloat)
|
|
|
|
vertex $ Vertex2 (-0.5*lw) (-0.5*lh)
|
|
|
|
|
|
|
|
texCoord $ TexCoord2 0 lw
|
|
|
|
vertex $ Vertex2 (-0.5*lw) (0.5*lh)
|
|
|
|
|
|
|
|
texCoord $ TexCoord2 lh lw
|
|
|
|
vertex $ Vertex2 (0.5*lw) (0.5*lh)
|
|
|
|
|
|
|
|
texCoord $ TexCoord2 lh (0 :: GLfloat)
|
|
|
|
vertex $ Vertex2 (0.5*lw) (-0.5*lh)
|
2010-03-02 23:22:44 +01:00
|
|
|
|
2010-02-25 04:38:14 +01:00
|
|
|
textureBinding Texture2D $= Just textureTank
|
|
|
|
|
2010-03-02 23:22:44 +01:00
|
|
|
forM_ tanklist $ \tank -> preservingMatrix $ do
|
|
|
|
let x = fromReal . posx $ tank
|
|
|
|
y = fromReal . posy $ tank
|
|
|
|
|
|
|
|
translate $ Vector3 x y (0 :: GLfloat)
|
|
|
|
rotate (90 + (fromReal . dir $ tank)) $ Vector3 0 0 (1 :: GLfloat)
|
|
|
|
|
|
|
|
renderPrimitive Quads $ do
|
|
|
|
texCoord $ TexCoord2 (0 :: GLfloat) (0 :: GLfloat)
|
|
|
|
vertex $ Vertex2 (-0.5 :: GLfloat) (-0.5 :: GLfloat)
|
|
|
|
|
|
|
|
texCoord $ TexCoord2 (0 :: GLfloat) (1 :: GLfloat)
|
|
|
|
vertex $ Vertex2 (-0.5 :: GLfloat) (0.5 :: GLfloat)
|
|
|
|
|
|
|
|
texCoord $ TexCoord2 (1 :: GLfloat) (1 :: GLfloat)
|
|
|
|
vertex $ Vertex2 (0.5 :: GLfloat) (0.5 :: GLfloat)
|
|
|
|
|
|
|
|
texCoord $ TexCoord2 (1 :: GLfloat) (0 :: GLfloat)
|
|
|
|
vertex $ Vertex2 (0.5 :: GLfloat) (-0.5 :: GLfloat)
|
2010-02-24 02:42:10 +01:00
|
|
|
|
2010-02-25 02:15:26 +01:00
|
|
|
fromReal :: (Real a, Fractional b) => a -> b
|
|
|
|
fromReal = fromRational . toRational
|