summaryrefslogtreecommitdiffstats
path: root/src/Render.hs
blob: 839859e9e9db419a312cfde712f1b7944adbfe70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
module Render ( setup
              , render
              ) where


import Paths_htanks
import Game
import Level
import Tank
import Texture

import Control.Monad.State

import Data.Fixed
import Data.Maybe
import Data.Ratio
import qualified Data.Map as M

import Bindings.GLPng

import Graphics.Rendering.OpenGL.GL (($=), GLfloat, GLdouble, Capability(..), Vector3(..))
import Graphics.Rendering.OpenGL.GL.BeginEnd (unsafeRenderPrimitive, PrimitiveMode(..))
import Graphics.Rendering.OpenGL.GL.CoordTrans (matrixMode, MatrixMode(..), viewport, Position(..), Size(..), loadIdentity, unsafePreservingMatrix, ortho, translate, rotate)
import Graphics.Rendering.OpenGL.GL.Framebuffer (clear, ClearBuffer(..))
import Graphics.Rendering.OpenGL.GL.PerFragment (blend, blendFunc, BlendingFactor(..))
import Graphics.Rendering.OpenGL.GL.Texturing.Application (texture)
import Graphics.Rendering.OpenGL.GL.Texturing.Objects (textureBinding, TextureObject(..))
import Graphics.Rendering.OpenGL.GL.Texturing.Parameters (Repetition(..), Clamping(..), TextureFilter(..), MinificationFilter, MagnificationFilter)
import Graphics.Rendering.OpenGL.GL.Texturing.Specification (TextureTarget(..))
import Graphics.Rendering.OpenGL.GL.VertexSpec


texturePath :: Texture -> IO FilePath
texturePath t = getDataFileName $ path t
    where
      path TextureWood = "tex/Wood.png"
      path TextureTank = "tex/Tank.png"
      path TextureCannon = "tex/Cannon.png"
      path TextureBullet = "tex/Bullet.png"

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
          path <- liftIO $ texturePath t
          tex <- liftIO $ pngBind path BuildMipmap Alpha (Repeated, Repeat) (Linear', Just Linear') Linear' >>= return . TextureObject . fromIntegral . fst
          modify $ \state -> state {textures = M.insert t tex ts}
          return tex
                                               

setup :: Game ()
setup = do
  liftIO $ do
    blend $= Enabled
    blendFunc $= (SrcAlpha, OneMinusSrcAlpha)
  
  -- cache textures
  getTexture TextureWood
  getTexture TextureTank
  getTexture TextureCannon
  getTexture TextureBullet
  
  return ()


render :: Game ()
render = do
  tanklist <- gets tanks
  bulletlist <- gets bullets
  
  textureWood <- getTexture TextureWood
  textureTank <- getTexture TextureTank
  textureCannon <- getTexture TextureCannon
  textureBullet <- getTexture TextureBullet
  
  (lw, lh) <- gets level >>= \l -> return (fromIntegral . levelWidth $ l :: GLfloat, fromIntegral . levelHeight $ l :: GLfloat)
  
  liftIO $ do
           clear [ColorBuffer]
           
           texture Texture2D $= Enabled
           textureBinding Texture2D $= Just textureWood
           
           unsafeRenderPrimitive Quads $ do
             texCoord $ TexCoord2 (0 :: GLfloat) (0 :: GLfloat)
             vertex $ Vertex2 0 lh
             
             texCoord $ TexCoord2 (lw/2) 0
             vertex $ Vertex2 lw lh
             
             texCoord $ TexCoord2 (lw/2) (lh/2)
             vertex $ Vertex2 lw 0
             
             texCoord $ TexCoord2 0 (lh/2)
             vertex $ Vertex2 (0 :: GLfloat) (0 :: GLfloat)
             
           forM_ tanklist $ \tank -> unsafePreservingMatrix $ do
                                       let x = realToFrac . tankX $ tank
                                           y = realToFrac . tankY $ tank
                                           rotDir = realToFrac . tankDir $ tank
                                           rotAim = realToFrac . tankAim $ tank
                                       
                                       translate $ Vector3 x y (0 :: GLfloat)
                                       rotate rotDir $ Vector3 0 0 (1 :: GLfloat)
                                       
                                       textureBinding Texture2D $= Just textureTank
                                       
                                       unsafeRenderPrimitive 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)
                                       
                                       rotate (rotAim-rotDir) $ Vector3 0 0 (1 :: GLfloat)
                                       
                                       textureBinding Texture2D $= Just textureCannon
                                       
                                       unsafeRenderPrimitive 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)
           
           forM_ bulletlist $ \bullet -> unsafePreservingMatrix $ do
                                       let x = realToFrac . bulletX $ bullet
                                           y = realToFrac . bulletY $ bullet
                                           rotDir = realToFrac . bulletDir $ bullet
                                       
                                       translate $ Vector3 x y (0 :: GLfloat)
                                       rotate rotDir $ Vector3 0 0 (1 :: GLfloat)
                                       
                                       textureBinding Texture2D $= Just textureBullet
                                       
                                       unsafeRenderPrimitive Quads $ do
                                                          texCoord $ TexCoord2 (0 :: GLfloat) (0 :: GLfloat)
                                                          vertex $ Vertex2 (-0.1 :: GLfloat) (-0.1 :: GLfloat)
                                                          
                                                          texCoord $ TexCoord2 (0 :: GLfloat) (1 :: GLfloat)
                                                          vertex $ Vertex2 (-0.1 :: GLfloat) (0.1 :: GLfloat)
                                                          
                                                          texCoord $ TexCoord2 (1 :: GLfloat) (1 :: GLfloat)
                                                          vertex $ Vertex2 (0.1 :: GLfloat) (0.1 :: GLfloat)
                                                          
                                                          texCoord $ TexCoord2 (1 :: GLfloat) (0 :: GLfloat)
                                                          vertex $ Vertex2 (0.1 :: GLfloat) (-0.1 :: GLfloat)