diff --git a/renderer-tutorial.cabal b/renderer-tutorial.cabal index a962a33..bfe157e 100644 --- a/renderer-tutorial.cabal +++ b/renderer-tutorial.cabal @@ -26,8 +26,10 @@ executable renderer-tutorial , bytestring , monad-loops , random - other-modules: BufferClass + other-modules: BindableClass + , BufferClass , VertexBuffer + , VertexArray , IndexBuffer hs-source-dirs: src default-language: Haskell2010 diff --git a/src/BindableClass.hs b/src/BindableClass.hs new file mode 100644 index 0000000..6121e00 --- /dev/null +++ b/src/BindableClass.hs @@ -0,0 +1,10 @@ +module BindableClass where + +-- | typeclass for bindabl eobjects like buffers, vertex arrays or shaders +class Bindable a where + + -- bind the object in context + bind :: a -> IO () + + -- release object from context + unbind :: a -> IO () diff --git a/src/BufferClass.hs b/src/BufferClass.hs index a87234f..a76aff1 100644 --- a/src/BufferClass.hs +++ b/src/BufferClass.hs @@ -5,10 +5,14 @@ import qualified Graphics.Rendering.OpenGL as GL import SDL (($=), get) +-- internal imports + +import BindableClass + -- this aims to be a typeclass for all used buffer objects throughout the -- tutorial. -- think of it as some kind of interface in the object oriented context -class Buffer a where +class (Bindable a) => Buffer a where type ObjName a :: * @@ -20,11 +24,3 @@ class Buffer a where -- bind the buffer object and fill it with data initialize :: a -> IO () - - -- bind the buffer - bind :: a -> IO () - -- bind buf = GL.bindBuffer (target buf) $= Just (glId buf) - - -- unbind the buffer - unbind :: a -> IO () - -- unbind buf = GL.bindBuffer (target buf) $= Nothing diff --git a/src/IndexBuffer.hs b/src/IndexBuffer.hs index c1b19b8..8d1d7ce 100644 --- a/src/IndexBuffer.hs +++ b/src/IndexBuffer.hs @@ -9,6 +9,7 @@ import Foreign -- internal imports +import BindableClass import BufferClass -- layout of the IndexBuffer data object @@ -40,6 +41,8 @@ instance Buffer (IndexBuffer a) where -- release the buffer using the default implementation of the typeclass unbind buf +instance Bindable (IndexBuffer a) where + -- bind the buffer bind buf = GL.bindBuffer (target buf) $= Just (glId buf) diff --git a/src/Main.hs b/src/Main.hs index 884fb12..55f8fe0 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -31,6 +31,7 @@ import System.Random (randomRIO) -- internal imports +import BindableClass import BufferClass import VertexArray import VertexBuffer diff --git a/src/VertexArray.hs b/src/VertexArray.hs index 5822141..59bd4d6 100644 --- a/src/VertexArray.hs +++ b/src/VertexArray.hs @@ -11,6 +11,7 @@ import Control.Concurrent.MVar -- internal imports +import BindableClass import BufferClass import VertexBuffer @@ -29,6 +30,9 @@ instance Buffer VertexArray where initialize va = return () + +instance Bindable VertexArray where + bind va = GL.bindVertexArrayObject $= Just (vArrId va) unbind va = GL.bindVertexArrayObject $= Nothing diff --git a/src/VertexBuffer.hs b/src/VertexBuffer.hs index 941fa3d..c4c48ff 100644 --- a/src/VertexBuffer.hs +++ b/src/VertexBuffer.hs @@ -9,6 +9,7 @@ import Foreign -- internal imports +import BindableClass import BufferClass -- layout of the VertexBuffer data object @@ -39,6 +40,9 @@ instance Buffer (VertexBuffer a) where -- release the buffer using the default implementation of the typeclass unbind buf + +instance Bindable (VertexBuffer a) where + -- bind the buffer bind buf = GL.bindBuffer (target buf) $= Just (glId buf)