2020-10-28 10:48:58 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
module Scenes.Test where
|
|
|
|
|
|
|
|
import Affection
|
|
|
|
|
2020-10-30 06:38:18 +00:00
|
|
|
import SDL (($=), get)
|
|
|
|
import qualified SDL
|
|
|
|
|
|
|
|
import qualified Graphics.Rendering.OpenGL as GL
|
|
|
|
|
2020-12-06 07:14:50 +00:00
|
|
|
import qualified Data.Vector.Storable as VS
|
|
|
|
|
2020-10-28 10:48:58 +00:00
|
|
|
import Control.Concurrent.STM
|
|
|
|
|
|
|
|
import Control.Monad (void)
|
|
|
|
|
2020-10-30 06:38:18 +00:00
|
|
|
import Linear
|
|
|
|
|
2020-10-28 10:48:58 +00:00
|
|
|
-- internal imports
|
|
|
|
|
|
|
|
import Types
|
2020-12-05 09:10:37 +00:00
|
|
|
import Classes
|
2020-12-06 07:14:50 +00:00
|
|
|
import Map
|
|
|
|
import Renderer as R
|
2020-10-28 10:48:58 +00:00
|
|
|
|
|
|
|
data Test = Test
|
|
|
|
{ testMap :: TMVar LevelMap
|
2020-12-06 07:14:50 +00:00
|
|
|
, testGraphics :: TMVar GLAssets
|
2020-10-28 10:48:58 +00:00
|
|
|
, testLoaded :: TVar Bool
|
|
|
|
}
|
|
|
|
|
2020-12-06 07:14:50 +00:00
|
|
|
data GLAssets = GLAssets
|
|
|
|
{ glVA :: VertexArray
|
|
|
|
, glVB :: VertexBuffer
|
|
|
|
, glIB :: IndexBuffer
|
|
|
|
, glSP :: Shader
|
|
|
|
}
|
|
|
|
|
2020-10-28 10:48:58 +00:00
|
|
|
instance Scene Test where
|
|
|
|
|
2020-12-06 07:14:50 +00:00
|
|
|
initScene =
|
|
|
|
Test
|
|
|
|
<$> newEmptyTMVarIO
|
|
|
|
<*> newEmptyTMVarIO
|
|
|
|
<*> newTVarIO False
|
2020-10-28 10:48:58 +00:00
|
|
|
|
|
|
|
loadScene level progress = do
|
|
|
|
atomically $ do
|
|
|
|
void $ takeTMVar progress
|
|
|
|
putTMVar progress (0, "Loading test level...")
|
|
|
|
loadedMap <- constructMap testLevelDesc 0
|
|
|
|
atomically $ putTMVar (testMap level) loadedMap
|
|
|
|
void $ atomically $ do
|
|
|
|
void $ takeTMVar progress
|
2020-10-30 06:38:18 +00:00
|
|
|
putTMVar progress (0.5, "Loaded test level map!")
|
2020-10-28 10:48:58 +00:00
|
|
|
void $ atomically $ swapTVar (testLoaded level) True
|
2020-10-30 06:38:18 +00:00
|
|
|
|
|
|
|
let projection = ortho 0 800 0 600 (-1) 1 :: M44 GL.GLfloat
|
|
|
|
view = mkTransformationMat (identity :: M33 GL.GLfloat) (V3 0 0 0)
|
|
|
|
model = mkTransformationMat (identity :: M33 GL.GLfloat) (V3 0 0 0)
|
|
|
|
|
|
|
|
vertexArray <- newVertexArray
|
|
|
|
|
2020-12-05 09:10:37 +00:00
|
|
|
bind vertexArray
|
2020-10-30 06:38:18 +00:00
|
|
|
|
2020-12-06 07:14:50 +00:00
|
|
|
vertexBuffer <- newVertexBuffer 1024
|
|
|
|
|
|
|
|
bind vertexBuffer
|
|
|
|
|
|
|
|
let vertices = VS.fromList (createQuad (V2 (400 - 16) (300 - 16)) 0)
|
|
|
|
|
|
|
|
write vertexBuffer 0 vertices
|
|
|
|
|
|
|
|
indexBuffer <- newIndexBuffer 1024
|
|
|
|
|
|
|
|
bind indexBuffer
|
|
|
|
|
|
|
|
let indices = VS.fromList [0, 1, 2, 2, 3, 0]
|
|
|
|
|
|
|
|
write indexBuffer 0 indices
|
|
|
|
|
|
|
|
addBuffer (undefined :: Vertex) vertexBuffer
|
|
|
|
|
|
|
|
shader <- newShader
|
|
|
|
[ ShaderSource GL.VertexShader "./res/shaders/vert.shader"
|
|
|
|
, ShaderSource GL.FragmentShader "./res/shaders/frag.shader"
|
|
|
|
]
|
|
|
|
|
|
|
|
bind shader
|
|
|
|
setUniform shader "u_mvp" (projection !*! view !*! model)
|
|
|
|
|
|
|
|
unbind vertexArray
|
|
|
|
unbind vertexBuffer
|
|
|
|
unbind indexBuffer
|
|
|
|
unbind shader
|
|
|
|
|
|
|
|
atomically $ do
|
|
|
|
putTMVar (testGraphics level)
|
|
|
|
(GLAssets vertexArray vertexBuffer indexBuffer shader)
|
|
|
|
writeTVar (testLoaded level) True
|
|
|
|
|
2020-10-30 06:38:18 +00:00
|
|
|
void $ atomically $ do
|
|
|
|
void $ takeTMVar progress
|
|
|
|
putTMVar progress (1, "Loaded graphics!")
|
|
|
|
|
2020-10-28 10:48:58 +00:00
|
|
|
|
|
|
|
isSceneLoaded = liftIO . atomically . readTVar . testLoaded
|
|
|
|
|
|
|
|
update _ _ = return ()
|
|
|
|
|
|
|
|
onEvents _ _ = return ()
|
|
|
|
|
2020-12-06 07:14:50 +00:00
|
|
|
render level = liftIO $ do
|
|
|
|
(GLAssets va vb ib sh) <- atomically (readTMVar $ testGraphics level)
|
|
|
|
bind vb
|
|
|
|
R.draw va ib sh
|
2020-10-28 10:48:58 +00:00
|
|
|
|
|
|
|
testLevelDesc :: LevelDescriptor
|
|
|
|
testLevelDesc = LevelDescriptor
|
|
|
|
[ (0, "res/maps/00_test/00_test.bmp")
|
|
|
|
]
|
|
|
|
0
|
|
|
|
"res/tiles/00_test/00_test.png"
|
|
|
|
(3, 3)
|
|
|
|
|
2020-12-06 07:14:50 +00:00
|
|
|
createQuad :: V2 GL.GLfloat -> Int -> [Vertex]
|
|
|
|
createQuad pos@(V2 x y) index =
|
|
|
|
[ newVertex (V3 x y 0) (V4 0 1 0 1) (V2 0 1) (fromIntegral index)
|
|
|
|
, newVertex (V3 (x + 32) y 0) (V4 0 1 0 1) (V2 0 1) (fromIntegral index)
|
|
|
|
, newVertex (V3 (x + 32) (y + 32) 0) (V4 0 1 0 1) (V2 0 1) (fromIntegral index)
|
|
|
|
, newVertex (V3 x (y + 32) 0) (V4 0 1 0 1) (V2 0 1) (fromIntegral index)
|
|
|
|
]
|