make checkGlError generally available
[gltest.git] / glutil.cpp
diff --git a/glutil.cpp b/glutil.cpp
new file mode 100644 (file)
index 0000000..0da85d5
--- /dev/null
@@ -0,0 +1,48 @@
+/* gltest - small OpenGL tearing test program
+ * Copyright (C) 2012-2013 Ralf Jung <post@ralfj.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "glutil.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+static const char *glErrorToString(GLenum e)
+{
+#define CASE(name) case name: return #name
+       switch (e) {
+               CASE(GL_NO_ERROR);
+               CASE(GL_INVALID_ENUM);
+               CASE(GL_INVALID_VALUE);
+               CASE(GL_INVALID_OPERATION);
+#ifndef CON_GLES2
+               CASE(GL_STACK_OVERFLOW);
+               CASE(GL_STACK_UNDERFLOW);
+#endif
+               CASE(GL_OUT_OF_MEMORY);
+               default: return "<unknown>";
+       }
+#undef CASE
+}
+
+void checkGlError(const char *what)
+{
+       GLenum e = glGetError();
+       if (e == GL_NO_ERROR) return;
+       fprintf(stderr, "GL error %d (%s): %s\n", e, glErrorToString(e), what);
+       exit(1);
+}