Skip to content

Commit 1a79ce1

Browse files
committed
Add weapon
Signed-off-by: Levente Kurusa <[email protected]>
1 parent d6d74d8 commit 1a79ce1

File tree

2 files changed

+140
-11
lines changed

2 files changed

+140
-11
lines changed

src/Game.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ data GameState = GameState {
3232
, enemies :: IORef [Enemy]
3333
, palette :: ColorPalette
3434
, sky :: RenderData
35+
, pWeapon :: RenderData
36+
, ticks :: IORef Int
37+
, lastShot :: IORef Int
3538
}
3639

3740
data RenderData = RenderData {
@@ -40,6 +43,7 @@ data RenderData = RenderData {
4043
, rdVao :: GLuint
4144
, rdTex :: GLuint
4245
, rdProg :: GLuint
46+
, rdExtra :: GLuint
4347
}
4448

4549

src/Main.hs

Lines changed: 136 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Control.Monad.Reader
1414
import Data.IORef
1515
import Data.Foldable
1616
import Data.Maybe
17-
import Data.List
17+
import Data.List hiding (map)
1818
import Data.Vector.V2
1919
import qualified Data.Map as M
2020
import Foreign
@@ -24,6 +24,7 @@ import Graphics.Triangulation.Delaunay
2424
import GLUtils
2525
import Graphics.GL.Core33
2626
import Graphics.UI.GLFW
27+
import Data.CaseInsensitive
2728
import Linear
2829
import Var
2930
import Types
@@ -82,9 +83,9 @@ bindMagic progId (BufferData bdata) = liftIO $ do
8283
(fromIntegral $ totalSize * dataSize)
8384
offset
8485
return (offset `plusPtr` fromIntegral (size * fromIntegral dataSize))
85-
) nullPtr (map (fmap fromIntegral) extracted)
86+
) nullPtr ((<$>) (fmap fromIntegral) extracted)
8687
where extracted = extract (Proxy :: Proxy (BufferData desc a))
87-
totalSize = fromIntegral $ sum . map snd $ extracted
88+
totalSize = fromIntegral $ sum . (<$>) snd $ extracted
8889
dataSize = sizeOf proxy
8990
proxy = undefined :: a
9091

@@ -108,7 +109,7 @@ constructSectors WAD.Level{..}
108109
(insert sectors res linedef, res)
109110
) (emptySectors, result) levelLineDefs
110111
in result
111-
where emptySectors = map (\WAD.Sector{..} -> Sector {
112+
where emptySectors = (<$>) (\WAD.Sector{..} -> Sector {
112113
sectorWalls = []
113114
, sectorCeiling = fromIntegral sectorCeilingHeight / scale
114115
, sectorFloor = fromIntegral sectorFloorHeight / scale
@@ -164,10 +165,10 @@ constructSectors WAD.Level{..}
164165
-- these are evil
165166
constructSubSectors :: WAD.Level -> [Subsector]
166167
constructSubSectors WAD.Level{..}
167-
= map (Subsector . subsectorPoints) levelSSectors
168+
= (<$>) (Subsector . subsectorPoints) levelSSectors
168169
where subsectorPoints :: WAD.SSector -> [Vertex2D]
169170
subsectorPoints WAD.SSector{..}
170-
= map (\WAD.Seg{..} ->
171+
= (<$>) (\WAD.Seg{..} ->
171172
vertexToVect $ levelVertices !! fromIntegral segStartVertex)
172173
$ take (fromIntegral ssectorSegCount)
173174
. drop (fromIntegral ssectorSegStart)
@@ -217,7 +218,7 @@ main = do
217218
]
218219
textToVert'
219220
= M.fromList
220-
$ map (\xs@((tex, _) : _) -> (tex, concatMap snd xs))
221+
$ (<$>) (\xs@((tex, _) : _) -> (tex, concatMap snd xs))
221222
$ groupBy (\(t1, _) (t2, _) -> t1 == t2)
222223
$ sortOn fst vertexBufferData'
223224
textToVert
@@ -227,7 +228,7 @@ main = do
227228
sideDefCount = length dat
228229
elementBufferData
229230
= concat $ take sideDefCount $
230-
iterate (map (+4)) ([0,1,2] ++ [2,1,3])
231+
iterate ((<$>) (+4)) ([0,1,2] ++ [2,1,3])
231232

232233
elementBufferId <- withNewPtr (glGenBuffers 1)
233234
glBindBuffer GL_ELEMENT_ARRAY_BUFFER elementBufferId
@@ -293,7 +294,7 @@ main = do
293294
-- -- !ys = traceShowId $ triangulation ts
294295
-- -- !asd = error $ show $ map wallPoints (chainWalls sectorWalls)
295296
-- ts = triangulation $ nub . concat $ map wallPoints (chainWalls sectorWalls)
296-
let ts = triangulate' $ nub . concat $ map wallPoints sectorWalls
297+
let ts = triangulate' $ nub . concat $ (<$>) wallPoints sectorWalls
297298
in concatMap (\(V2 x y) ->
298299
[x, sectorFloor, y]
299300
) ts ++
@@ -302,8 +303,8 @@ main = do
302303
) ts
303304
) sectors
304305
triangulate' points
305-
= map vector2Tov2 . concatMap (\(a, b, c) -> [a, b, c])
306-
$ triangulate (map v2ToVector2 points)
306+
= (<$>) vector2Tov2 . concatMap (\(a, b, c) -> [a, b, c])
307+
$ triangulate ((<$>) v2ToVector2 points)
307308
v2ToVector2 (V2 a b) = Vector2 (realToFrac a) (realToFrac b)
308309
wallPoints Wall{..} = [wallStart, wallEnd]
309310
findItem f [] = error "findItem: item not found"
@@ -367,6 +368,7 @@ main = do
367368
, rdVao = floorVertexArrayId
368369
}
369370
sprites <- createLevelThings wad progId (WAD.levelThings level)
371+
let palette' = loadPalettes wad
370372
initState <- GameState <$> return progId
371373
<*> return wad
372374
<*> return sideDefCount
@@ -379,8 +381,107 @@ main = do
379381
<*> newIORef levelEnemies
380382
<*> pure (loadPalettes wad)
381383
<*> fillSkyTextureData wad
384+
<*> pistolWeapon wad palette'
385+
<*> newIORef 0
386+
<*> newIORef 0
382387
mainLoop (\w -> runGame (loop w) initState)
383388

389+
pistolWeapon :: WAD.Wad -> ColorPalette -> IO RenderData
390+
pistolWeapon wad palette = do
391+
wepVert <- loadShader GL_VERTEX_SHADER "src/shaders/sprite.vert"
392+
wepFrag <- loadShader GL_FRAGMENT_SHADER "src/shaders/sprite.frag"
393+
wepProgId <- glCreateProgram
394+
glAttachShader wepProgId wepVert
395+
glAttachShader wepProgId wepFrag
396+
glLinkProgram wepProgId
397+
glUseProgram wepProgId
398+
399+
vaoId <- withNewPtr (glGenVertexArrays 1)
400+
glBindVertexArray vaoId
401+
402+
vboId <- withNewPtr (glGenBuffers 1)
403+
glBindBuffer GL_ARRAY_BUFFER vboId
404+
withArrayLen vbo $ \len vertices ->
405+
glBufferData GL_ARRAY_BUFFER
406+
(fromIntegral $ len * sizeOf (0 :: GLfloat))
407+
(vertices :: Ptr GLfloat)
408+
GL_STATIC_DRAW
409+
410+
eboId <- withNewPtr (glGenBuffers 1)
411+
glBindBuffer GL_ELEMENT_ARRAY_BUFFER eboId
412+
withArrayLen ebo $ \len vertices ->
413+
glBufferData GL_ELEMENT_ARRAY_BUFFER
414+
(fromIntegral $ len * sizeOf (0 :: GLuint))
415+
(vertices :: Ptr GLuint)
416+
GL_STATIC_DRAW
417+
418+
--still
419+
let wepSprite = fromMaybe (error "wep not found")
420+
(M.lookup (mk "PISGA0") (WAD.wadSprites wad))
421+
let (tW, tH) = (fromIntegral $ WAD.pictureWidth $ WAD.spritePicture wepSprite,
422+
fromIntegral $ WAD.pictureHeight $ WAD.spritePicture wepSprite)
423+
txt <- loadSpriteColor wepSprite palette
424+
stillTexId <- withNewPtr (glGenTextures 1)
425+
glBindTexture GL_TEXTURE_2D stillTexId
426+
427+
glTexParameteri GL_TEXTURE_2D GL_TEXTURE_WRAP_S (fromIntegral GL_REPEAT)
428+
glTexParameteri GL_TEXTURE_2D GL_TEXTURE_WRAP_T (fromIntegral GL_REPEAT)
429+
glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER (fromIntegral GL_NEAREST)
430+
glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER (fromIntegral GL_NEAREST)
431+
432+
withArray txt $
433+
glTexImage2D GL_TEXTURE_2D 0 (fromIntegral GL_RGBA) tW tH 0 GL_RGBA GL_FLOAT
434+
435+
--firing
436+
let fwepSprite = fromMaybe (error "fwep not found")
437+
(M.lookup (mk "PISFA0") (WAD.wadSprites wad))
438+
let (fW, fH) = (fromIntegral $ WAD.pictureWidth $ WAD.spritePicture fwepSprite,
439+
fromIntegral $ WAD.pictureHeight $ WAD.spritePicture fwepSprite)
440+
ftxt <- loadSpriteColor fwepSprite palette
441+
firingTexId <- withNewPtr (glGenTextures 1)
442+
glBindTexture GL_TEXTURE_2D firingTexId
443+
444+
glTexParameteri GL_TEXTURE_2D GL_TEXTURE_WRAP_S (fromIntegral GL_REPEAT)
445+
glTexParameteri GL_TEXTURE_2D GL_TEXTURE_WRAP_T (fromIntegral GL_REPEAT)
446+
glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER (fromIntegral GL_NEAREST)
447+
glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER (fromIntegral GL_NEAREST)
448+
449+
withArray ftxt $
450+
glTexImage2D GL_TEXTURE_2D 0 (fromIntegral GL_RGBA) fW fH 0 GL_RGBA GL_FLOAT
451+
452+
posAttrib <- get $ AttribLocation wepProgId "position"
453+
glEnableVertexAttribArray posAttrib
454+
glVertexAttribPointer posAttrib
455+
3
456+
GL_FLOAT
457+
(fromBool False)
458+
(fromIntegral $ 5 * sizeOf (0 :: GLfloat))
459+
nullPtr
460+
461+
colAttrib <- get $ AttribLocation wepProgId "texcoord"
462+
glEnableVertexAttribArray colAttrib
463+
glVertexAttribPointer colAttrib
464+
2
465+
GL_FLOAT
466+
(fromBool False)
467+
(fromIntegral $ 5 * sizeOf (0 :: GLfloat))
468+
(offsetPtr 3 (0 :: GLfloat))
469+
470+
return $ RenderData { rdVbo = vboId,
471+
rdEbo = eboId,
472+
rdTex = stillTexId,
473+
rdExtra = firingTexId,
474+
rdVao = vaoId,
475+
rdProg = wepProgId}
476+
where
477+
vbo = [-0.2, -0.1, 0.0, 0.0, 0.0,
478+
0.2, -0.1, 0.0, 1.0, 0.0,
479+
-0.2, -0.7, 0.0, 0.0, 1.0,
480+
0.2, -0.7, 0.0, 1.0, 1.0]
481+
482+
ebo = [0, 1, 2,
483+
2, 1, 3]
484+
384485
getTextureId :: WAD.Wad -> WAD.LumpName -> IO GLuint
385486
getTextureId wad name = do
386487
(tW, tH, txt) <- loadTexture wad name
@@ -398,6 +499,8 @@ getTextureId wad name = do
398499
loop :: Window -> Game ()
399500
loop w = do
400501
-- TODO: this is not very nice...
502+
ticks' <- asks ticks
503+
io $ modifyIORef' ticks' (+ 1)
401504
rot' <- get rot
402505
(V3 px pz py) <- get player
403506
let ax = axisAngle (V3 0 1 0) rot'
@@ -486,6 +589,17 @@ updateView w initV modelM = do
486589
bindRenderData (spriteRenderData sprite)
487590
glDrawElements GL_TRIANGLES 6 GL_UNSIGNED_INT nullPtr
488591

592+
-- render wep
593+
weapon <- asks pWeapon
594+
bindRenderData weapon
595+
ticks' <- asks ticks
596+
lastShot' <- asks lastShot
597+
ticks'' <- io $ readIORef ticks'
598+
lastShot'' <- io $ readIORef lastShot'
599+
when (ticks'' - lastShot'' <= 25) $
600+
glBindTexture GL_TEXTURE_2D (rdExtra weapon)
601+
glDrawElements GL_TRIANGLES 6 GL_UNSIGNED_INT nullPtr
602+
489603
-- this is a huge mess
490604
--
491605

@@ -497,9 +611,20 @@ multAndProject m v =
497611
let (V4 x y z _) = m !* (extendToV4 v)
498612
in V3 x y z
499613

614+
applyShot :: Game ()
615+
applyShot = return ()
500616

501617
keyEvents :: Window -> V3 GLfloat -> Game ()
502618
keyEvents w move = do
619+
keyP <- io $ getKey w Key'Space
620+
when (keyP == KeyState'Pressed) $ do
621+
ticks' <- asks ticks
622+
ticks'' <- io $ readIORef ticks'
623+
lastShot' <- asks lastShot
624+
io $ writeIORef lastShot' ticks''
625+
applyShot
626+
627+
503628
keyW <- io $ getKey w Key'W
504629
when (keyW == KeyState'Pressed) $ do
505630
let moveM = mkTransformationMat identity move

0 commit comments

Comments
 (0)