Advertisement

SDL_DisplayFormat Access Violation

Started by June 28, 2011 04:27 PM
-1 comments, last by KazenoZ 13 years, 5 months ago
Hello,

I know this is a problem that had been discussed alot, but I just couldn't find any solution that worked for me here =\

This is my image loading function:

SDL_Surface* Map::IMGLoad(string file, int scale){

SDL_Surface* img = NULL;

SDL_Surface* optImg = NULL;

img = IMG_Load(file.c_str());

if(img != NULL){

//Optimizing the image?

optImg = SDL_DisplayFormat(img);

SDL_FreeSurface(img);

img = NULL;

//Setting the alpha color for the graphics.

SDL_SetColorKey(optImg, SDL_SRCCOLORKEY, SDL_MapRGB(optImg->

format, 255, 0, 255));

//Doubles the sprite size if neccesary

if(scale != 1){

optImg = zoomSurface(optImg, scale, scale, NULL);

SDL_SetColorKey(optImg, SDL_SRCCOLORKEY, SDL_MapRGB(optImg->

format, 255, 0, 255));

}

}

else{
cout <<"Error loading "<< file.c_str();

SDL_Quit();

}

return optImg;

}



Scale is a given variable determining wether the user wants to resize the sprite at runtime.
File is the file location of the image to load.

The error occurs on the line
optImg = SDL_DisplayFormat(img);
And is the typical access violation error tied to the SDL_DisplayFormat() usual problems.

Debugging determines that img is loaded from the file correctly, so it's not bad data.

I'm 100% positively sure that SDL is initialized and the screen video mode is set before this function is called, as the function that calls loading of sprites is positioned in the code after the initializers.


//Initiallizers

SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO);

SDL_GetError();

SDL_Surface* screen;

if(project.loadSeek("Settings", "Fullscreen", data) == "Yes")
screen = SDL_SetVideoMode(400*project.scale, 300*

project.scale, 0,

SDL_DOUBLEBUF|SDL_HWSURFACE|SDL_FULLSCREEN);

if(project.loadSeek("Settings", "Fullscreen", data) == "No")

screen = SDL_SetVideoMode(400*project.scale, 300*

project.scale, 0,

SDL_DOUBLEBUF|SDL_HWSURFACE);

//Initialize SDL_mixer

Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 );

//Sets the window name and icon

SDL_WM_SetCaption(project.Name.c_str(), 0);

if(project.loadSeek("Settings", "Icon", data) != "-1"){
SDL_Surface* icon = IMGLoadBG(project.icon);

if(icon){

SDL_WM_SetIcon(icon, NULL);

SDL_FreeSurface(icon);

}

else

std::cout << SDL_GetError();

}

//Loading the graphics.

ch.pic = IMGLoad(project.loadSeek("Settings", "Hero");

ch.frame = SDL_CreateRGBSurface(ch.pic->flags, ch.w, ch.h, ch.

pic->format->BitsPerPixel, ch.pic->format->Rmask, ch.pic->

format->Gmask, ch.pic->format->Bmask, ch.pic->format->Amask);

SDL_SetColorKey(ch.frame, SDL_SRCCOLORKEY, SDL_MapRGB(ch.frame->

format, 255, 0, 255));

SDL_Surface* background = IMGLoad(project.map->pPath,

project.scale);

//Loading events

list<vPoint> pList = pointLoad(data);

list<vText> strList = textLoad(data);

list<vSwitch> sList = switchLoad(data);

list<vNum> numList = numLoad(data);

list<vPic> picList = picLoad(data);

list<vSound> sndList = sndLoad(data);

list<Event> evList = eventLoad(data, atoi(project.loadSeek("Settings", "Scale", data).c_str()));

parList.clear();



Project is a variable that manages various aspects of the game played, none of which has to do with the sprite loading.
Project's loadSeek method, reads an external data file under the the given category and value, all the calls to it in here return the correct values.

The various load functions at the end use the seekLoad method to load given values to different variables that will later be used in the game.

Interestingly enough, despite there being some calls to IMGLoad that have been successul before the error, the error only comes up at the last command shown in this snippet inside of
list<Event> evList = eventLoad(data, atoi(project.loadSeek("Settings", "Scale", data).c_str()));

So it's definetly not the initializers, and the values are correct, I also tried changing to SDL_SWSURFACE, but had no luck there either..... Basicly, I'm stumped...

If there's any more information I could give that would help anyone help me out, just say so.

Thanks alot.

This topic is closed to new replies.

Advertisement