module Graphics.GPipe.Internal.Debug where

import Control.Monad
import Data.Maybe
import Data.List (intercalate)
import qualified Debug.Trace as Trace
import Graphics.GL.Core45
import Graphics.GL.Types

traceIt :: Show a => String -> a -> a
traceIt :: String -> a -> a
traceIt String
t a
a = String -> a -> a
forall a. String -> a -> a
Trace.trace (String
t String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" = " String -> String -> String
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
a) a
a

traceList :: Show a => String -> [a] -> [a]
traceList :: String -> [a] -> [a]
traceList String
t [a]
as = String -> [a] -> [a]
forall a. String -> a -> a
Trace.trace (String
t String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" = [\n\t" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\n\t" ((a -> String) -> [a] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map a -> String
forall a. Show a => a -> String
show [a]
as) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n]") [a]
as

checkGlError :: String -> IO ()
checkGlError :: String -> IO ()
checkGlError String
title = do
    GLenum
e <- IO GLenum
forall (m :: * -> *). MonadIO m => m GLenum
glGetError
    Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (GLenum
e GLenum -> GLenum -> Bool
forall a. Eq a => a -> a -> Bool
/= GLenum
forall a. (Eq a, Num a) => a
GL_NO_ERROR) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
        String -> IO ()
forall a. HasCallStack => String -> a
error (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
"[" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
title String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"] GL error -> " String -> String -> String
forall a. [a] -> [a] -> [a]
++ GLenum -> String
getErrorMessage GLenum
e

getErrorMessage :: GLenum -> String
getErrorMessage :: GLenum -> String
getErrorMessage GLenum
errorCode = String -> Maybe String -> String
forall a. a -> Maybe a -> a
fromMaybe (String
"Unknown error code " String -> String -> String
forall a. [a] -> [a] -> [a]
++ GLenum -> String
forall a. Show a => a -> String
show GLenum
errorCode) (Maybe String -> String) -> Maybe String -> String
forall a b. (a -> b) -> a -> b
$ GLenum -> [(GLenum, String)] -> Maybe String
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup GLenum
errorCode
    [ (GLenum
forall a. (Eq a, Num a) => a
GL_NO_ERROR, String
"GL_NO_ERROR")
    , (GLenum
forall a. (Eq a, Num a) => a
GL_INVALID_ENUM, String
"GL_INVALID_ENUM")
    , (GLenum
forall a. (Eq a, Num a) => a
GL_INVALID_VALUE, String
"GL_INVALID_VALUE")
    , (GLenum
forall a. (Eq a, Num a) => a
GL_INVALID_OPERATION, String
"GL_INVALID_OPERATION")
    , (GLenum
forall a. (Eq a, Num a) => a
GL_STACK_OVERFLOW, String
"GL_STACK_OVERFLOW")
    , (GLenum
forall a. (Eq a, Num a) => a
GL_STACK_UNDERFLOW, String
"GL_STACK_UNDERFLOW")
    , (GLenum
forall a. (Eq a, Num a) => a
GL_OUT_OF_MEMORY, String
"GL_OUT_OF_MEMORY")
    , (GLenum
forall a. (Eq a, Num a) => a
GL_INVALID_FRAMEBUFFER_OPERATION, String
"GL_INVALID_FRAMEBUFFER_OPERATION")
    , (GLenum
forall a. (Eq a, Num a) => a
GL_CONTEXT_LOST, String
"GL_CONTEXT_LOST")
    ]