{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{- |
 - Module      : Haskengl.Internal.IO.View
 - Description : view function for Haskengl
 - Copyright   : Andromeda 2025
 - License     : WTFPL
 - Maintainer  : Matrix @Andromeda:tchncs.de
 - Stability   : Experimental
 -}
module Haskengl.Internal.IO.View where

import Relude

import qualified Haskengl.Internal.Bindings.OpenGL as GL
import           Haskengl.Internal.Bindings.OpenGL       (($=))
import qualified Haskengl.Internal.Bindings.GLFW   as GLFW

import           Haskengl.Internal.Types

-- | default canvas color
backgroundColor :: GL.Color4 GL.GLfloat
backgroundColor = GL.Color4 1 0 1 1

-- | display the state to the screen
view :: GLFW.Window -> Model -> IO ()
view window (Model objects) = do
  GL.clearColor $= backgroundColor
  GL.clear [GL.ColorBuffer]
  displayObjects objects
  GLFW.swapBuffers window
  GLFW.pollEvents

-- | draw an array of objects with OpenGL
displayObjects :: DisplayableObjects -> IO (DisplayableObjects)
displayObjects [] = return []
displayObjects ((DisplayableObject vao numVertices _ primitiveMode):objects) = do
  GL.bindVertexArrayObject $= Just vao
  GL.drawArrays primitiveMode 0 numVertices
  displayObjects objects
