diff options
Diffstat (limited to 'Spear/GLSL/Error.hs')
-rw-r--r-- | Spear/GLSL/Error.hs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/Spear/GLSL/Error.hs b/Spear/GLSL/Error.hs new file mode 100644 index 0000000..7865996 --- /dev/null +++ b/Spear/GLSL/Error.hs | |||
@@ -0,0 +1,45 @@ | |||
1 | module Spear.GLSL.Error | ||
2 | ( | ||
3 | getGLError | ||
4 | , printGLError | ||
5 | , assertGL | ||
6 | ) | ||
7 | where | ||
8 | |||
9 | |||
10 | import Spear.Setup | ||
11 | |||
12 | import Graphics.Rendering.OpenGL.Raw.Core31 | ||
13 | import System.IO (hPutStrLn, stderr) | ||
14 | |||
15 | |||
16 | -- | Get the last OpenGL error. | ||
17 | getGLError :: IO (Maybe String) | ||
18 | getGLError = fmap translate glGetError | ||
19 | where | ||
20 | translate err | ||
21 | | err == gl_NO_ERROR = Nothing | ||
22 | | err == gl_INVALID_ENUM = Just "Invalid enum" | ||
23 | | err == gl_INVALID_VALUE = Just "Invalid value" | ||
24 | | err == gl_INVALID_OPERATION = Just "Invalid operation" | ||
25 | | err == gl_OUT_OF_MEMORY = Just "Out of memory" | ||
26 | | otherwise = Just "Unknown error" | ||
27 | |||
28 | |||
29 | -- | Print the last OpenGL error. | ||
30 | printGLError :: IO () | ||
31 | printGLError = getGLError >>= \err -> case err of | ||
32 | Nothing -> return () | ||
33 | Just str -> hPutStrLn stderr str | ||
34 | |||
35 | |||
36 | -- | Run the given 'Setup' action and check for OpenGL errors. | ||
37 | -- If an OpenGL error is produced, an exception is thrown | ||
38 | -- containing the given string and the OpenGL error. | ||
39 | assertGL :: Setup a -> String -> Setup a | ||
40 | assertGL action err = do | ||
41 | result <- action | ||
42 | status <- setupIO getGLError | ||
43 | case status of | ||
44 | Just str -> setupError $ "OpenGL error raised: " ++ err ++ "; " ++ str | ||
45 | Nothing -> return result | ||