fixed crash when glDebugCallbackARB is available but glDebugCallback isn't
[laserbrain_demo] / src / opengl.c
index 796144c..206f21c 100644 (file)
@@ -1,3 +1,4 @@
+#include <stdlib.h>
 #include "opengl.h"
 #include "logger.h"
 
@@ -20,13 +21,40 @@ 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 GLAPIENTRY gldebug_logger(GLenum src, GLenum type, GLuint id, GLenum severity,
                GLsizei len, const char *msg, const void *cls)