@@ -14,7 +14,7 @@ import Control.Monad.Reader
14
14
import Data.IORef
15
15
import Data.Foldable
16
16
import Data.Maybe
17
- import Data.List
17
+ import Data.List hiding ( map )
18
18
import Data.Vector.V2
19
19
import qualified Data.Map as M
20
20
import Foreign
@@ -24,6 +24,7 @@ import Graphics.Triangulation.Delaunay
24
24
import GLUtils
25
25
import Graphics.GL.Core33
26
26
import Graphics.UI.GLFW
27
+ import Data.CaseInsensitive
27
28
import Linear
28
29
import Var
29
30
import Types
@@ -82,9 +83,9 @@ bindMagic progId (BufferData bdata) = liftIO $ do
82
83
(fromIntegral $ totalSize * dataSize)
83
84
offset
84
85
return (offset `plusPtr` fromIntegral (size * fromIntegral dataSize))
85
- ) nullPtr (map (fmap fromIntegral ) extracted)
86
+ ) nullPtr ((<$>) (fmap fromIntegral ) extracted)
86
87
where extracted = extract (Proxy :: Proxy (BufferData desc a ))
87
- totalSize = fromIntegral $ sum . map snd $ extracted
88
+ totalSize = fromIntegral $ sum . (<$>) snd $ extracted
88
89
dataSize = sizeOf proxy
89
90
proxy = undefined :: a
90
91
@@ -108,7 +109,7 @@ constructSectors WAD.Level{..}
108
109
(insert sectors res linedef, res)
109
110
) (emptySectors, result) levelLineDefs
110
111
in result
111
- where emptySectors = map (\ WAD. Sector {.. } -> Sector {
112
+ where emptySectors = (<$>) (\ WAD. Sector {.. } -> Sector {
112
113
sectorWalls = []
113
114
, sectorCeiling = fromIntegral sectorCeilingHeight / scale
114
115
, sectorFloor = fromIntegral sectorFloorHeight / scale
@@ -164,10 +165,10 @@ constructSectors WAD.Level{..}
164
165
-- these are evil
165
166
constructSubSectors :: WAD. Level -> [Subsector ]
166
167
constructSubSectors WAD. Level {.. }
167
- = map (Subsector . subsectorPoints) levelSSectors
168
+ = (<$>) (Subsector . subsectorPoints) levelSSectors
168
169
where subsectorPoints :: WAD. SSector -> [Vertex2D ]
169
170
subsectorPoints WAD. SSector {.. }
170
- = map (\ WAD. Seg {.. } ->
171
+ = (<$>) (\ WAD. Seg {.. } ->
171
172
vertexToVect $ levelVertices !! fromIntegral segStartVertex)
172
173
$ take (fromIntegral ssectorSegCount)
173
174
. drop (fromIntegral ssectorSegStart)
@@ -217,7 +218,7 @@ main = do
217
218
]
218
219
textToVert'
219
220
= M. fromList
220
- $ map (\ xs@ ((tex, _) : _) -> (tex, concatMap snd xs))
221
+ $ (<$>) (\ xs@ ((tex, _) : _) -> (tex, concatMap snd xs))
221
222
$ groupBy (\ (t1, _) (t2, _) -> t1 == t2)
222
223
$ sortOn fst vertexBufferData'
223
224
textToVert
@@ -227,7 +228,7 @@ main = do
227
228
sideDefCount = length dat
228
229
elementBufferData
229
230
= concat $ take sideDefCount $
230
- iterate (map (+ 4 )) ([0 ,1 ,2 ] ++ [2 ,1 ,3 ])
231
+ iterate ((<$>) (+ 4 )) ([0 ,1 ,2 ] ++ [2 ,1 ,3 ])
231
232
232
233
elementBufferId <- withNewPtr (glGenBuffers 1 )
233
234
glBindBuffer GL_ELEMENT_ARRAY_BUFFER elementBufferId
@@ -293,7 +294,7 @@ main = do
293
294
-- -- !ys = traceShowId $ triangulation ts
294
295
-- -- !asd = error $ show $ map wallPoints (chainWalls sectorWalls)
295
296
-- 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
297
298
in concatMap (\ (V2 x y) ->
298
299
[x, sectorFloor, y]
299
300
) ts ++
@@ -302,8 +303,8 @@ main = do
302
303
) ts
303
304
) sectors
304
305
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)
307
308
v2ToVector2 (V2 a b) = Vector2 (realToFrac a) (realToFrac b)
308
309
wallPoints Wall {.. } = [wallStart, wallEnd]
309
310
findItem f [] = error " findItem: item not found"
@@ -367,6 +368,7 @@ main = do
367
368
, rdVao = floorVertexArrayId
368
369
}
369
370
sprites <- createLevelThings wad progId (WAD. levelThings level)
371
+ let palette' = loadPalettes wad
370
372
initState <- GameState <$> return progId
371
373
<*> return wad
372
374
<*> return sideDefCount
@@ -379,8 +381,107 @@ main = do
379
381
<*> newIORef levelEnemies
380
382
<*> pure (loadPalettes wad)
381
383
<*> fillSkyTextureData wad
384
+ <*> pistolWeapon wad palette'
385
+ <*> newIORef 0
386
+ <*> newIORef 0
382
387
mainLoop (\ w -> runGame (loop w) initState)
383
388
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
+
384
485
getTextureId :: WAD. Wad -> WAD. LumpName -> IO GLuint
385
486
getTextureId wad name = do
386
487
(tW, tH, txt) <- loadTexture wad name
@@ -398,6 +499,8 @@ getTextureId wad name = do
398
499
loop :: Window -> Game ()
399
500
loop w = do
400
501
-- TODO: this is not very nice...
502
+ ticks' <- asks ticks
503
+ io $ modifyIORef' ticks' (+ 1 )
401
504
rot' <- get rot
402
505
(V3 px pz py) <- get player
403
506
let ax = axisAngle (V3 0 1 0 ) rot'
@@ -486,6 +589,17 @@ updateView w initV modelM = do
486
589
bindRenderData (spriteRenderData sprite)
487
590
glDrawElements GL_TRIANGLES 6 GL_UNSIGNED_INT nullPtr
488
591
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
+
489
603
-- this is a huge mess
490
604
--
491
605
@@ -497,9 +611,20 @@ multAndProject m v =
497
611
let (V4 x y z _) = m !* (extendToV4 v)
498
612
in V3 x y z
499
613
614
+ applyShot :: Game ()
615
+ applyShot = return ()
500
616
501
617
keyEvents :: Window -> V3 GLfloat -> Game ()
502
618
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
+
503
628
keyW <- io $ getKey w Key'W
504
629
when (keyW == KeyState'Pressed ) $ do
505
630
let moveM = mkTransformationMat identity move
0 commit comments