Okay, I am trying to create code that will let me load OpenGL textures using
SDL. However, following some tutorial code that I found, I get a segfault that I
cannot figure out. I am running Ubuntu Linux and have gotten my SDL and GL code
to work fine for everything else.
I am able to reproduce the error with a short program
/////////////////////////////////
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
int main(int argc, char *argv[])
{
GLuint texture;
SDL_Surface* surface;
SDL_Surface* screen;
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf("Unable to initialize SDL: %s\n", SDL_GetError());
return 1;
}
atexit(SDL_Quit);
int videoMode = 0;
videoMode |= SDL_OPENGL;
screen = SDL_SetVideoMode( 640, 480, 16, videoMode );
//Load Bitmap
if(!(surface = SDL_LoadBMP("image.bmp"))) {
return -1;
}
// Have OpenGL generate a texture object handle for us
glGenTextures( 1, &texture );
// Bind the texture object
glBindTexture( GL_TEXTURE_2D, texture );
// Set the texture's stretching properties
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexImage2D( GL_TEXTURE_2D, 0, 3, surface->w, surface->h, 0,
GL_BGR, GL_UNSIGNED_BYTE, surface->pixels );
//Free surface after using it
if(surface)
SDL_FreeSurface(surface);
}
///////////////////////////
Debugging with GDB doesn’t give me the specific line of the problem, but tracing
through, I have isolated the problem at glTexImage2D. It seems that some other
people may have had similar problems, but I can’t find a resolution anywhere.
In my actual program I originally tried loading the (pcx) image through
SDL_image, and then converted it into a BMP when that failed for the same
reason.
Any help at all would be welcome! If there is any other information that would
be useful, please let me know.
-Ben