iDevGames Forums
SDL Parachute Deployed when accessing surface->pixels - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: SDL Parachute Deployed when accessing surface->pixels (/thread-5933.html)



SDL Parachute Deployed when accessing surface->pixels - BobbyWatson - Jan 22, 2005 09:39 PM

It's been a while since I've actually touched any SDL/OpenGL stuff, so I'm sorry if my problem is a very simple one (and I'm guessing that it is).

Here it is : I'm currently trying to load a texture (from a PNG file, though I've tried with a GIF and a BMP, with the same results), but every time I try to access my variable SDL_Surface* textureSurface->pixels, I get the following message in the Run Log :

Fatal signal: Bus Error (SDL Parachute Deployed)

Please note that I'm also using PHYSFS for loading files from inside a zip archive (which, according to the log, seems to be working fine).

1. I open the file with a PHYSFS function in "read" mode.
2. I read the whole file in memory.
3. I create an SDL_RWops using the copy of the file currently in RAM.
4. I check to make sure that I have a valid SDL_RWops, and then I use IMG_Load_RW to create and SDL_Surface from the stored memory. I check with IMG_GetError, and there is none.
5. I lock the texture surface.
6. Using SDL_Surface->format->BitsPerPixel, I determine the pixel format I have to pass to gluBuild2DMipmaps.
7. I can get the following information :

width = 512, height = 128, bitsPerPixel = 32

from the SDL_Surface without any problem.
8. The problem happens when I try to read something from SDL_Surface->pixels, I always get the Fatal signal Bus Error.

If it helps, here's the code :

Code:
if ((textureSurface = IMG_Load_RW(rw, 1)) != NULL)
{
        SDL_LockSurface(textureSurface);
                    
    //Il faut maintenant determiner le format de pixel de l'image
    switch(textureSurface->format->BitsPerPixel)
    {
        case 32 :    pixelFormat = GL_RGBA;
                imageType = GL_UNSIGNED_BYTE;
                internalPixelFormat = GL_RGBA8;
                break;
           case 24 :    pixelFormat = GL_RGB;
                imageType = GL_UNSIGNED_BYTE;
                internalPixelFormat = GL_RGB8;
                break;
        case 16 :    pixelFormat = GL_RGBA;
                imageType = GL_UNSIGNED_SHORT_5_5_5_1;
                internalPixelFormat = GL_RGB5_A1;
                break;
        default :    pixelFormat = GL_LUMINANCE;
                imageType = GL_UNSIGNED_BYTE;
                internalPixelFormat = GL_LUMINANCE8;
                break;
    }
                    
printf("width = %i, height = %i, bitsPerPixel = %i\n", textureSurface->w, textureSurface->h, textureSurface->format->BitsPerPixel);
                    
glGenTextures(1, &textureID);
                    
glBindTexture(GL_TEXTURE_2D, textureID);
                    
if (gluBuild2DMipmaps(GL_TEXTURE_2D, internalPixelFormat, textureSurface->w, textureSurface->h, pixelFormat, imageType, textureSurface->pixels))
    isValid = false;
                    
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
                    
SDL_UnlockSurface(textureSurface);
                    
SDL_FreeSurface(textureSurface);

}

Does anyone have an idea of where the problem might be? I'm out of ideas...


SDL Parachute Deployed when accessing surface->pixels - fisk - Jan 23, 2005 03:34 AM

I'm not sure, but it wouldn't be too strange if you cannot acces the data you just deallocated, now would it?
Try to comment out SDL_FreeSurface(textureSurface);


SDL Parachute Deployed when accessing surface->pixels - BobbyWatson - Jan 23, 2005 06:08 AM

The code doesn't get to SDL_FreeSurface, it crashes at gluBuild2DMipmaps, 3 lines before freeing the surface.


SDL Parachute Deployed when accessing surface->pixels - arekkusu - Jan 23, 2005 09:44 AM

Try peeking at textureSurface->pixels using a known image to make sure SDL has loaded the bits you expect.

i.e.
Code:
unsigned int *pix = (unsigned int *)textureSurface->pixels;
printf("first few pixels of image: %08x %08x %08x %08x", pix[0], pix[1], pix[2], pix[3]);

Maybe it has discarded the alpha channel or something.


SDL Parachute Deployed when accessing surface->pixels - BobbyWatson - Jan 23, 2005 10:10 AM

It gives me the following :

width = 512, height = 128, bitsPerPixel = 32
Fatal signal: Bus Error (SDL Parachute Deployed)
first few pixels of image: 00000000 00000000 00000000 00000000

Instead of 0, 1, 2 and 3 as pixels, I tried some random values (since most of the picture is transparent) between 0 and 65535 (since the picture is 512 by 128 pixels) and I got this :

width = 512, height = 128, bitsPerPixel = 32
Fatal signal: Bus Error (SDL Parachute Deployed)
first few pixels of image: 00000000 fdfbfbff 00000000 1514144d
Executable “Hector's Quest” has exited with status 246.


SDL Parachute Deployed when accessing surface->pixels - arekkusu - Jan 23, 2005 10:21 AM

Try it with a known image. Make a bitmap where the top left (bottom left, whichever) pixels are red, green, transparent, etc so you can compare against known values.


SDL Parachute Deployed when accessing surface->pixels - BobbyWatson - Jan 23, 2005 10:55 AM

I created a BMP image with a red pixel at 0, a green at 1 and a blue at 2, with the rest all black, and here's what I got :

width = 32, height = 32, bitsPerPixel = 24
Fatal signal: Bus Error (SDL Parachute Deployed)
first few pixels of image: 0000ff00 ff00ff00 00000000 00000000
Executable “Hector's Quest” has exited with status 246.

I'm not even sure if this is okay. Does SDL keep the same pixel order (BMP's are BGR and not RGB, if memory serves right), but it looks okay to me (considering that the pixel format is 3-byte and not 4-byte, no alpha channel). First pixel : 0000ff, second 00ff00, third ff0000, fourth 000000...

I also tried using SDL_LoadBMP instead of SDL_image, and the results are the same.


SDL Parachute Deployed when accessing surface->pixels - BobbyWatson - Jan 23, 2005 11:07 AM

Okay, stop thinking about it, I just found the source of the problem. It didn't come from SDL, but rather from the fact that I was calling glGenTextures before having an OpenGL context.

I'm really sorry about wasting everyone's time...


SDL Parachute Deployed when accessing surface->pixels - MattDiamond - Jan 23, 2005 06:30 PM

It wasn't a waste of time. The discussion helped you, and it may help others in the future.