SDL_Exception in SDL_DisplayFormat()

Started by
8 comments, last by Mybowlcut 16 years, 7 months ago
Hey. I've got a function that loads an image using SDL. I'm getting the exception below at the commented line:
bool Draw_Engine::Load_Image(Image& image)
{
	//Free up the previous image if there was one
	if(image.Get_Surface() != NULL)
		Free_Image(image);

    SDL_Surface* loaded_image = NULL;
    SDL_Surface* optimized_image = NULL;
    
	loaded_image = IMG_Load(image.Get_File_Name().c_str());
    
    // If the image was loaded successfully, try to optimize it.
    if(loaded_image != NULL) {
        optimized_image = SDL_DisplayFormat(loaded_image); // THIS LINE
        SDL_FreeSurface(loaded_image);
		 //If the image was optimized just fine
		if(optimized_image != NULL && image.Is_Colour_Key()) {
			// Create the colour key
			colour_key = SDL_MapRGB(optimized_image->format, image.Get_Red(), image.Get_Blue(), image.Get_Green());
			// Sets the colour key to the image
			SDL_SetColorKey(optimized_image, SDL_RLEACCEL | SDL_SRCCOLORKEY, colour_key);
		} 
		// Set the image's surface to the newly optimized surface
		image.Set_Surface(optimized_image);
    }
	// Otherwise return false.
	else
		return false;

	return true;
}

Quote:Unhandled exception at 0x1002b195 in Ghost.exe: 0xC0000005: Access violation reading location 0x0000013c.
Just wondering what could possibly cause such an exception? loaded_image has these data values when the exception is thrown:

Advertisement
This guy seemed to end up having the same problem but never got it solved...

I had a somewhat similar problem that was solved by using SDL_SWSURFACE instead of SDL_HWSURFACE. It may be worth a try.
You are probably calling that code before you have set up your screen surface. Se if SDL_GetVideoSurface() returns NULL. I remember this happening to me.

SDL_DisplayFormat will obviously only work if there is a "display" which SDL can use to set the format... [smile]
Before I had even looked at your replies, the code started working... exactly what happened to the guy in the link that I posted. I'll try those things you guys suggested.

rip-off it did not return NULL. Which I guess is bad since I still don't know the cause...

Simian Man, do you know what the difference between storing the surface in video memory and system memory is? Performance wise, etc.? Just curious before I try it out. Unfortunately, once again, even if it did work I couldn't tell the difference at the moment because it miraculously started working... uhhh.

I've changed some things around and the error happened again... :\

Have you definitely called SDL_Init() first?
Good fight! Haha. I'd changed my initialisation functions around and it turns out that that's exactly the problem! Ahhh! How embarrassing! Thanks. :p

If SDL_Init wasn't called, how did you call SDL_SetVideoMode... O_o
I put both of those functions in the same function:

bool Draw_Engine::Initialise() {	if(SDL_Init(SDL_INIT_EVERYTHING) == -1)		return false;	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);	if(screen == NULL)		return false;	SDL_WM_SetCaption(SCREEN_CAPTION.c_str(), NULL);	//Initialize SDL_ttf    if(TTF_Init() == -1)        return false;	//Open the font    font = TTF_OpenFont(DEFAULT_FONT.c_str(), DEFAULT_FONT_SIZE);        //If there was an error in loading the font    if(font == NULL)        return false;	//The color of the font	font_colour.r = DEFAULT_FC_R;	font_colour.g = DEFAULT_FC_G;	font_colour.b = DEFAULT_FC_B;	// Initialisation was successful	return true;}


For the program that I'm making I keep a vector of all the images I need to use. When I add an image I also load it at the same time in Load_Image(). :)

This topic is closed to new replies.

Advertisement