From 9fc75b677213215b47c112f415c4c7719097c5c4 Mon Sep 17 00:00:00 2001 From: Lawrence Sebald Date: Sun, 3 Jan 2016 22:23:32 -0500 Subject: [PATCH] Add glGetError() and gluErrorString(). --- gl-error.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++- include/gl.h | 5 ++++- include/glu.h | 3 +++ 3 files changed, 53 insertions(+), 2 deletions(-) mode change 100755 => 100644 gl-error.c diff --git a/gl-error.c b/gl-error.c old mode 100755 new mode 100644 index d047653..ba487ec --- a/gl-error.c +++ b/gl-error.c @@ -2,6 +2,7 @@ libgl/gl-error.c Copyright (C) 2014 Josh Pearson + Copyright (C) 2016 Lawrence Sebald KOS Open GL State Machine Error Code Implementation. */ @@ -17,12 +18,27 @@ #define KOS_GL_INVALID_VALUE (1<<3) static GLsizei KOS_GL_ERROR_CODE; +static GLenum gl_last_error = GL_NO_ERROR; static char KOS_GL_ERROR_FUNCTION[64] = { '\0' }; +/* Quoth the GL Spec: + When an error occurs, the error flag is set to the appropriate error code + value. No other errors are recorded until glGetError is called, the error + code is returned, and the flag is reset to GL_NO_ERROR. + + So, we only record an error here if the error code is currently unset. + Nothing in the spec requires recording multiple error flags, although it is + allowed by the spec. We take the easy way out for now. */ +static void set_err_flag(GLenum error) { + if(gl_last_error == GL_NO_ERROR) + gl_last_error = error; +} + void _glKosThrowError(GLenum error, char *function) { sprintf(KOS_GL_ERROR_FUNCTION, "%s\n", function); + set_err_flag(error); switch(error) { case GL_INVALID_ENUM: @@ -69,4 +85,33 @@ void _glKosPrintError() { printf("KOS GL ERROR: GL_INVALID_VALUE\n"); _glKosResetError(); -} \ No newline at end of file +} + +GLenum glGetError(void) { + GLenum rv = gl_last_error; + + gl_last_error = GL_NO_ERROR; + return rv; +} + +const GLubyte *gluErrorString(GLenum error) { + switch(error) { + case GL_NO_ERROR: + return (GLubyte *)"no error"; + + case GL_INVALID_ENUM: + return (GLubyte *)"invalid enumerant"; + + case GL_INVALID_OPERATION: + return (GLubyte *)"invalid operation"; + + case GL_INVALID_VALUE: + return (GLubyte *)"invalid value"; + + case GL_OUT_OF_MEMORY: + return (GLubyte *)"out of memory"; + + default: + return (GLubyte *)"unknown error"; + } +} diff --git a/include/gl.h b/include/gl.h index c57476e..9808254 100755 --- a/include/gl.h +++ b/include/gl.h @@ -2,7 +2,7 @@ libgl/gl.h Copyright (C) 2013-2014 Josh "PH3NOM" Pearson - Copyright (C) 2014 Lawrence Sebald + Copyright (C) 2014, 2016 Lawrence Sebald Some functionality adapted from the original KOS libgl: Copyright (C) 2001 Dan Potter @@ -659,6 +659,9 @@ GLAPI void APIENTRY glFramebufferTexture2D(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level); GLAPI GLenum APIENTRY glCheckFramebufferStatus(GLenum target); +/* Error handling */ +GLAPI GLenum APIENTRY glGetError(void); + /* Non Operational Stubs for portability */ GLAPI void APIENTRY glAlphaFunc(GLenum func, GLclampf ref); GLAPI void APIENTRY glLineWidth(GLfloat width); diff --git a/include/glu.h b/include/glu.h index dcb89f2..6397441 100755 --- a/include/glu.h +++ b/include/glu.h @@ -2,6 +2,7 @@ libgl/glu.h Copyright (C) 2013-2014 Josh "PH3NOM" Pearson + Copyright (C) 2016 Lawrence Sebald Some functionality adapted from the original KOS libgl: Copyright (C) 2001 Dan Potter @@ -42,6 +43,8 @@ GLAPI void APIENTRY glhLookAtf2(GLfloat *eyePosition3D, GLfloat *center3D, GLfloat *upVector3D); +GLAPI const GLubyte* APIENTRY gluErrorString(GLenum error); + __END_DECLS #endif /* !__GL_GLU_H */