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 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)
|
|
|
|
(SpriteShaderObjects prog)
|
|
|
|
texture
|
|
|
|
_ = do
|
|
|
|
GL.currentProgram $= Just (GLU.program prog)
|
|
|
|
GL.bindVertexArrayObject $= Just vao
|
|
|
|
GL.activeTexture $= GL.TextureUnit 0
|
|
|
|
GL.textureBinding GL.Texture2D $= Just texture
|
|
|
|
GL.clearColor $= GL.Color4 1 1 1 1
|
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-02-07 13:57:09 +00:00
|
|
|
(SpriteShaderObjects 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-02-07 23:15:53 +00:00
|
|
|
GLU.setUniform prog "pm" (projection !*! 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)
|
2020-02-07 23:15:53 +00:00
|
|
|
GL.texture GL.Texture2D $= GL.Disabled
|