fixed crash when glDebugCallbackARB is available but glDebugCallback isn't
[laserbrain_demo] / src / opengl.c
index 9fe7ea3..206f21c 100644 (file)
@@ -1,8 +1,11 @@
+#include <stdlib.h>
 #include "opengl.h"
 #include "logger.h"
 
-static void gldebug_logger(unsigned int src, unsigned int type, unsigned int id,
-               unsigned int severity, int len, const char *msg, const void *cls);
+
+static void GLAPIENTRY gldebug_logger(GLenum src, GLenum type, GLuint id, GLenum severity,
+               GLsizei len, const char *msg, const void *cls);
+
 static const char *gldebug_srcstr(unsigned int src);
 static const char *gldebug_typestr(unsigned int type);
 static const char *gldebug_sevstr(unsigned int sev);
@@ -18,16 +21,43 @@ int init_opengl(void)
 #ifndef NDEBUG
        if(glcaps.debug) {
                info_log("Installing OpenGL debug callback\n");
-               glDebugMessageCallback(gldebug_logger, 0);
+               glDebugMessageCallbackARB(gldebug_logger, 0);
        }
 #endif
 
        return 0;
 }
 
+void dump_gl_texture(unsigned int tex, const char *fname)
+{
+       FILE *fp;
+       int i, width, height;
+       unsigned char *pixels;
+
+       glBindTexture(GL_TEXTURE_2D, tex);
+       glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
+       glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
+
+       if(!(pixels = malloc(width * height * 3))) {
+               return;
+       }
+       glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
+
+       if(!(fp = fopen(fname, "wb"))) {
+               free(pixels);
+               return;
+       }
+       fprintf(fp, "P6\n%d %d\n255\n", width, height);
+       for(i=0; i<width * height * 3; i++) {
+               fputc(pixels[i], fp);
+       }
+       fclose(fp);
+       free(pixels);
+}
+
 
-static void gldebug_logger(unsigned int src, unsigned int type, unsigned int id,
-               unsigned int severity, int len, const char *msg, const void *cls)
+static void GLAPIENTRY gldebug_logger(GLenum src, GLenum type, GLuint id, GLenum severity,
+               GLsizei len, const char *msg, const void *cls)
 {
        static const char *fmt = "[GLDEBUG] (%s) %s: %s\n";
        switch(type) {