Simplify gl-error.c

This commit is contained in:
Luke Benstead 2017-07-22 21:00:33 +01:00
parent 6a4a6a157b
commit 79b8e9af3c
5 changed files with 47 additions and 70 deletions

View File

@ -173,9 +173,8 @@ void _glKosInitFrameBuffers();
/* Error Codes */
void _glKosThrowError(GLenum error, char *functionName);
void _glKosResetError();
void _glKosPrintError();
GLsizei _glKosGetError();
GLubyte _glKosHasError();
GLuint _glKosTextureWidth(GLuint index);
GLuint _glKosTextureHeight(GLuint index);

View File

@ -102,7 +102,7 @@ GLAPI void APIENTRY glVertexPointer(GLint size, GLenum type,
if(stride < 0)
_glKosThrowError(GL_INVALID_VALUE, "glVertexPointer");
if(_glKosGetError()) {
if(_glKosHasError()) {
_glKosPrintError();
return;
}
@ -121,7 +121,7 @@ GLAPI void APIENTRY glNormalPointer(GLenum type, GLsizei stride, const GLvoid *p
if(stride < 0)
_glKosThrowError(GL_INVALID_VALUE, "glNormalPointer");
if(_glKosGetError()) {
if(_glKosHasError()) {
_glKosPrintError();
return;
}
@ -142,7 +142,7 @@ GLAPI void APIENTRY glTexCoordPointer(GLint size, GLenum type,
if(stride < 0)
_glKosThrowError(GL_INVALID_VALUE, "glTexCoordPointer");
if(_glKosGetError()) {
if(_glKosHasError()) {
_glKosPrintError();
return;
}
@ -754,7 +754,7 @@ static GLuint _glKosArraysVerifyParameter(GLenum mode, GLsizei count, GLenum typ
else if(type > count)
_glKosThrowError(GL_INVALID_VALUE, "glDrawArrays");
if(_glKosGetError()) {
if(_glKosHasError()) {
_glKosPrintError();
return 0;
}
@ -1298,7 +1298,7 @@ void APIENTRY glClientActiveTextureARB(GLenum texture) {
if(texture < GL_TEXTURE0_ARB || texture > GL_TEXTURE0_ARB + _glKosMaxTextureUnits())
_glKosThrowError(GL_INVALID_ENUM, "glClientActiveTextureARB");
if(_glKosGetError()) {
if(_glKosHasError()) {
_glKosPrintError();
return;

View File

@ -3,6 +3,7 @@
libgl/gl-error.c
Copyright (C) 2014 Josh Pearson
Copyright (C) 2016 Lawrence Sebald
* Copyright (C) 2017 Luke Benstead
KOS Open GL State Machine Error Code Implementation.
*/
@ -12,15 +13,8 @@
#include <stdio.h>
#define KOS_GL_INVALID_ENUM (1<<0)
#define KOS_GL_OUT_OF_MEMORY (1<<1)
#define KOS_GL_INVALID_OPERATION (1<<2)
#define KOS_GL_INVALID_VALUE (1<<3)
static GLsizei KOS_GL_ERROR_CODE = 0;
static GLenum gl_last_error = GL_NO_ERROR;
static char KOS_GL_ERROR_FUNCTION[64] = { '\0' };
static GLenum last_error = GL_NO_ERROR;
static char error_function[64] = { '\0' };
/* Quoth the GL Spec:
When an error occurs, the error flag is set to the appropriate error code
@ -30,67 +24,51 @@ static char KOS_GL_ERROR_FUNCTION[64] = { '\0' };
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:
KOS_GL_ERROR_CODE |= KOS_GL_INVALID_ENUM;
break;
case GL_OUT_OF_MEMORY:
KOS_GL_ERROR_CODE |= KOS_GL_OUT_OF_MEMORY;
break;
case GL_INVALID_OPERATION:
KOS_GL_ERROR_CODE |= KOS_GL_INVALID_OPERATION;
break;
case GL_INVALID_VALUE:
KOS_GL_ERROR_CODE |= KOS_GL_INVALID_VALUE;
break;
if(last_error == GL_NO_ERROR) {
last_error = error;
sprintf(error_function, "%s\n", function);
}
}
GLsizei _glKosGetError() {
return KOS_GL_ERROR_CODE;
GLubyte _glKosHasError() {
return (last_error != GL_NO_ERROR) ? GL_TRUE : GL_FALSE;
}
void _glKosResetError() {
KOS_GL_ERROR_CODE = 0;
sprintf(KOS_GL_ERROR_FUNCTION, "\n");
static void _glKosResetError() {
last_error = GL_NO_ERROR;
sprintf(error_function, "\n");
}
void _glKosPrintError() {
printf("\nKOS GL ERROR THROWN BY FUNCTION: %s\n", KOS_GL_ERROR_FUNCTION);
if(!_glKosHasError()) {
return;
}
if(KOS_GL_ERROR_CODE & KOS_GL_INVALID_ENUM)
printf("\nKOS GL ERROR THROWN BY FUNCTION: %s\n", error_function);
switch(last_error) {
case GL_INVALID_ENUM:
printf("KOS GL ERROR: GL_INVALID_ENUM\n");
if(KOS_GL_ERROR_CODE & KOS_GL_OUT_OF_MEMORY)
break;
case GL_OUT_OF_MEMORY:
printf("KOS GL ERROR: GL_OUT_OF_MEMORY\n");
if(KOS_GL_ERROR_CODE & KOS_GL_INVALID_OPERATION)
break;
case GL_INVALID_OPERATION:
printf("KOS GL ERROR: GL_INVALID_OPERATION\n");
if(KOS_GL_ERROR_CODE & KOS_GL_INVALID_VALUE)
break;
case GL_INVALID_VALUE:
printf("KOS GL ERROR: GL_INVALID_VALUE\n");
_glKosResetError();
break;
default:
break;
}
}
GLenum glGetError(void) {
GLenum rv = gl_last_error;
gl_last_error = GL_NO_ERROR;
GLenum rv = last_error;
_glKosResetError();
return rv;
}

View File

@ -151,7 +151,7 @@ GLAPI void APIENTRY glFramebufferTexture2D(GLenum target, GLenum attachment,
if(!FRAMEBUF_OBJECT)
_glKosThrowError(GL_INVALID_OPERATION, "glFramebufferTexture2D");
if(_glKosGetError()) {
if(_glKosHasError()) {
_glKosPrintError();
return;
}

View File

@ -208,7 +208,7 @@ void APIENTRY glCompressedTexImage2D(GLenum target,
if(GL_KOS_TEXTURE_UNIT[GL_KOS_ACTIVE_TEXTURE] == NULL)
_glKosThrowError(GL_INVALID_OPERATION, "glCompressedTexImage2D");
if(_glKosGetError()) {
if(_glKosHasError()) {
_glKosPrintError();
return;
}
@ -254,7 +254,7 @@ void APIENTRY glTexImage2D(GLenum target, GLint level, GLint internalFormat,
if(GL_KOS_TEXTURE_UNIT[GL_KOS_ACTIVE_TEXTURE] == NULL)
_glKosThrowError(GL_INVALID_OPERATION, "glTexImage2D");
if(_glKosGetError()) {
if(_glKosHasError()) {
_glKosPrintError();
return;
}
@ -398,7 +398,7 @@ void APIENTRY glTexEnvi(GLenum target, GLenum pname, GLint param) {
if(pname != GL_TEXTURE_ENV_MODE)
_glKosThrowError(GL_INVALID_ENUM, "glTexEnvi");
if(_glKosGetError()) {
if(_glKosHasError()) {
_glKosPrintError();
return;
}
@ -415,7 +415,7 @@ void APIENTRY glActiveTextureARB(GLenum texture) {
if(texture < GL_TEXTURE0_ARB || texture > GL_TEXTURE0_ARB + GL_KOS_MAX_TEXTURE_UNITS)
_glKosThrowError(GL_INVALID_ENUM, "glActiveTextureARB");
if(_glKosGetError()) {
if(_glKosHasError()) {
_glKosPrintError();
return;