Checking for OpenGL Errors

Every so often I come by a bug that is annoying to debug. While I would like to write out lots of extra code that tells me exactly what went wrong with a function, it is just quicker for me to get the error type and look it up in the documentation.

For that reason, I made these helper functions. One clears the errors in OpenGL, the other outputs the error type as a string to the console and then asserts.

If the macro is used, this check can be turned off in release, and in debug the static bool within the function can be used to disable the check at runtime.

#ifdef DEBUG
#	define GL_CHECK_FOR_ERRORS()	GL::CheckForErrors()
#else
#	define GL_CHECK_FOR_ERRORS()
#endif
 
namespace GL
{
	//
	//
	bool CheckForErrors()
	{
		static bool checkForErrors = true;
 
		//
		if( !checkForErrors )
		{
			return false;
		}
 
		//
		const char * errorString = NULL;
		bool retVal = false;
 
		switch( glGetError() )
		{
		case GL_NO_ERROR:
			retVal = true;
			break;
 
		case GL_INVALID_ENUM:
			errorString = "GL_INVALID_ENUM";
			break;
 
		case GL_INVALID_VALUE:
			errorString = "GL_INVALID_VALUE";
			break;
 
		case GL_INVALID_OPERATION:
			errorString = "GL_INVALID_OPERATION";
			break;
 
		case GL_INVALID_FRAMEBUFFER_OPERATION:
			errorString = "GL_INVALID_FRAMEBUFFER_OPERATION";
			break;
 
// OpenGLES Specific Errors
#ifdef ATHENA_OPENGLES
		case GL_STACK_OVERFLOW:
			errorString = "GL_STACK_OVERFLOW";
			break;
 
		case GL_STACK_UNDERFLOW:
			errorString = "GL_STACK_UNDERFLOW";
			break;
#endif
 
		case GL_OUT_OF_MEMORY:
			errorString = "GL_OUT_OF_MEMORY";
			break;
 
		default:
			errorString = "UNKNOWN";
			break;
		}
 
		//
		if( !retVal )
		{
			printf( "%s\n", errorString );
			assert( retVal );
		}
 
		//
		return retVal;
	}
 
	//
	//
	void ClearErrors()
	{
		while( glGetError() != GL_NO_ERROR );
	}
}