When I try to call SDL_DisplayFormatAlpha, I get this unhandled exception: "Unhandled exception at 0x68125981 (SDL.dll) in herorpg.exe: 0xC0000005: Access violation reading location 0x0000013C."
SDL_Surface* load_image ( char* filename ) {
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = IMG_Load ( filename );
if ( loadedImage != NULL ) {
optimizedImage = SDL_DisplayFormatAlpha ( loadedImage );
SDL_FreeSurface ( loadedImage );
}
return optimizedImage;
}
bool init ( SDL_Surface*& screen ) {
//Initialize SDL
if ( SDL_Init ( SDL_INIT_EVERYTHING ) == -1 ) return false;
screen = SDL_SetVideoMode ( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_FULLSCREEN );
if ( screen == NULL ) return false;
//Set the caption
SDL_WM_SetCaption ( "Hero RPG", NULL );
return true;
}
int main ( int argc, char** argv ) {
init ( screen );
load_image ( "graphics\\background\\overworld" );
return 0;
}
If I comment out
//if ( loadedImage != NULL ) {
// optimizedImage = SDL_DisplayFormatAlpha ( loadedImage );
// SDL_FreeSurface ( loadedImage );
//}
and change
loadedImage = IMG_Load ( filename );
to
optimizedImage = IMG_Load ( filename );
the image loads fine. So I know there is no problem with the file name.
This is the exact function I used in the last program I designed, and it worked fine last time. I think the answer to this will be outside of the box. Why would I be receiving this unhandled exception?
In the documentation it has this line:
Of course, the video surface must be initialised using SDL_SetVideoMode before this function is called, or it will segfault.
Seeing I don't see a call to SDL_SetVideoMode
in your posted code, I can assume this is probably the cause.
User contributions licensed under CC BY-SA 3.0