canvas/app/Types/Sprite.hs

53 lines
1.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
import SDL (get, ($=))
import qualified Graphics.Rendering.OpenGL as GL
2020-02-07 13:57:09 +00:00
import qualified Graphics.GLUtil as GLU
2020-02-07 04:50:19 +00:00
import Codec.Picture
import Linear
-- internal imports
import Classes.Renderable
data SpriteRenderObjects = SpriteRenderObjects
{ roVAO :: GL.VertexArrayObject
, roVBO :: GL.BufferObject
}
2020-02-07 13:57:09 +00:00
data SpriteShaderObjects = SpriteShaderObjects
{ soProgram :: GLU.ShaderProgram
2020-02-07 04:50:19 +00:00
}
data Sprite = Sprite
{ spritePosition :: V2 Int
2020-02-07 13:57:09 +00:00
, spriteImage :: Image PixelRGBA8
2020-02-07 04:50:19 +00:00
}
instance Renderable Sprite where
type VertexObjects Sprite = SpriteRenderObjects
type ShaderObjects Sprite = SpriteShaderObjects
2020-02-07 13:57:09 +00:00
init (SpriteRenderObjects vao vbo) (SpriteShaderObjects prog) _ = do
GL.currentProgram $= Just (GLU.program prog)
2020-02-07 04:50:19 +00:00
GL.bindVertexArrayObject $= Just vao
draw
(SpriteRenderObjects vao vbo)
2020-02-07 13:57:09 +00:00
(SpriteShaderObjects prog)
2020-02-07 04:50:19 +00:00
(Sprite pos@(V2 px py) img) = do
let model = mkTransformation
2020-02-07 13:57:09 +00:00
(Quaternion 1 (V3 0 0 0) :: Quaternion Float)
2020-02-07 04:50:19 +00:00
(fmap fromIntegral $ V3 px py 0 :: V3 Float)
2020-02-07 13:57:09 +00:00
GLU.setUniform prog "model" model
2020-02-07 04:50:19 +00:00
GL.drawArrays GL.Triangles 0 6
2020-02-07 13:57:09 +00:00
clean _ = do
GL.currentProgram $= (Nothing :: Maybe GL.Program)
GL.bindVertexArrayObject $= (Nothing :: Maybe GL.VertexArrayObject)