canvas/app/Types/Sprite.hs

86 lines
2.3 KiB
Haskell
Raw Normal View History

2020-02-07 13:57:09 +00:00
{-# LANGUAGE TypeFamilies #-}
2020-02-07 04:50:19 +00:00
module Types.Sprite where
2020-05-13 03:42:32 +00:00
import Data.Maybe (maybe)
import qualified Data.Map.Strict as M
2020-02-07 04:50:19 +00:00
import SDL (get, ($=))
import qualified Graphics.Rendering.OpenGL as GL
2020-05-13 03:42:32 +00:00
import qualified Graphics.GL.Core33 as GLRaw
-- import qualified Graphics.GLUtil as GLU
2020-02-07 04:50:19 +00:00
import Codec.Picture
import Linear
2020-05-13 03:42:32 +00:00
import Foreign.Ptr
import Foreign.Marshal.Utils (with)
import Unsafe.Coerce (unsafeCoerce)
2020-02-07 04:50:19 +00:00
-- internal imports
import Classes.Renderable
2020-05-13 03:42:32 +00:00
import Types.CanvasShader
2020-02-07 04:50:19 +00:00
data SpriteRenderObjects = SpriteRenderObjects
{ roVAO :: GL.VertexArrayObject
, roVBO :: GL.BufferObject
}
2020-02-07 13:57:09 +00:00
data SpriteShaderObjects = SpriteShaderObjects
2020-05-13 03:42:32 +00:00
{ soProgram :: CanvasShader
2020-02-07 04:50:19 +00:00
}
data Sprite = Sprite
{ spritePosition :: V2 Int
2020-02-07 23:15:53 +00:00
, spriteSize :: V2 Int
2020-02-07 04:50:19 +00:00
}
instance Renderable Sprite where
type VertexObjects Sprite = SpriteRenderObjects
type ShaderObjects Sprite = SpriteShaderObjects
2020-02-07 23:15:53 +00:00
init
(SpriteRenderObjects vao vbo)
2020-05-13 03:42:32 +00:00
(SpriteShaderObjects (CanvasShader att uni prog))
2020-02-07 23:15:53 +00:00
texture
_ = do
2020-05-13 03:42:32 +00:00
GL.currentProgram $= Just prog
2020-02-07 23:15:53 +00:00
GL.bindVertexArrayObject $= Just vao
GL.activeTexture $= GL.TextureUnit 0
GL.textureBinding GL.Texture2D $= Just texture
2020-02-09 01:38:02 +00:00
-- let projection = ortho (-960) 960 (-540) 540 (-1) 1 :: M44 Float
-- GLU.setUniform prog "projection" (projection)
2020-02-07 04:50:19 +00:00
draw
(SpriteRenderObjects vao vbo)
2020-05-13 03:42:32 +00:00
(SpriteShaderObjects (CanvasShader att uni prog))
2020-02-07 23:15:53 +00:00
(Sprite pos@(V2 px py) size) = do
let (V2 sx sy) = fmap fromIntegral size
model = fmap (fmap fromIntegral) $ V4
(V4 sx 0 0 px)
(V4 0 sy 0 py)
(V4 0 0 1 0)
(V4 0 0 0 1)
:: M44 Float
2020-02-09 01:38:02 +00:00
projection = ortho (-960) 960 (-540) 540 (-1) 1
-- putStrLn $ show model
2020-05-13 03:42:32 +00:00
maybe
(const (putStrLn warn >> return ()))
(flip asUniform . fst)
(M.lookup "pm" uni)
(projection !*! model)
2020-02-07 04:50:19 +00:00
GL.drawArrays GL.Triangles 0 6
2020-05-13 03:42:32 +00:00
where
warn = "WARNING: uniform pm is not active"
v `asUniform` loc = with v $ GLRaw.glUniformMatrix4fv (unsafeCoerce loc) 1 1 . castPtr
2020-02-07 04:50:19 +00:00
2020-02-07 13:57:09 +00:00
clean _ = do
GL.currentProgram $= (Nothing :: Maybe GL.Program)
GL.bindVertexArrayObject $= (Nothing :: Maybe GL.VertexArrayObject)
2020-02-07 23:15:53 +00:00
GL.texture GL.Texture2D $= GL.Disabled