Sign in to follow this  

SDL_Exception in SDL_DisplayFormat()

This topic is 3736 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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(). :)

Share this post


Link to post
Share on other sites

This topic is 3736 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this