Converting OpenGL texture to SDL surface

Hi everyone,

I’m trying to make an SDL surface out of an OpenGL texture. Here’s what
I have:

void pixel_data = malloc(widthheight4);
memset(pixel_data, 0, width

glBindTexture(GL_TEXTURE_2D, gl_texture);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixel_data);

This seems to be working fine and puts the texture into pixel_data.
What I can’t figure out is how to get the pixel data into an SDL
surface. I tried locking it and copying directly into the surface->
pixels, but this doesn’t work. I’m guessing that the pixel format
doesn’t match that of the surface, but I don’t know how to get the
pixels in the correct format from OpenGL. Here’s how I create the

SDL_Surface *surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width,
height, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);

Any help would be very appreciated.


Never mind, I figured it out. I totally forgot about
SDL_CreateRGBSurfaceFrom(), which works perfectly for this.