Advertisement Jump to content
Sign in to follow this  

Loading images in SDL help.

This topic is 4480 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

Im using SDL in Devc++ and writing my first game and am having a problem with image loading. Heres my code: Object class .h
class object //simple object template { public: char * path;//store file path to objects image int velocityx,velocityy; // current velocity SDL_Rect coords; // current coordinates int lastx,lasty;//last coordinates SDL_Surface *mysurface;//object surface void update(SDL_Surface *screen,bool,int);//used to update objects info. object(); };
Image class.h
class LoadImg//Loads a image as a surface { public: int load(char *,SDL_Surface *,int, int,bool,int,SDL_Rect*); LoadImg(); private: SDL_Surface* loadedImageT; };
Image class .cpp
int LoadImg:: load(char * fpath,SDL_Surface * mysurface,int x, int y,bool alpha,int transparency,SDL_Rect* coords) { if(mysurface ==NULL) { loadedImageT = IMG_Load(fpath); if(loadedImageT == NULL) return 0; mysurface = SDL_DisplayFormat(loadedImageT); if(mysurface ==NULL) return 0; SDL_FreeSurface(loadedImageT); Uint32 colorkey = SDL_MapRGB( mysurface->format, 255, 255, 255); if(SDL_SetColorKey( mysurface, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey ) == -1) return 0; if(alpha == true) SDL_SetAlpha(mysurface, SDL_SRCALPHA|SDL_RLEACCEL, transparency); } return 1; } LoadImg:: LoadImg() { loadedImageT = NULL; }
and the Update method:
void object::update(SDL_Surface *screen,bool alpha,int transparency) { coords.x+=velocityx; coords.y+=velocityy; LoadImg image; if(!image.load(path,mysurface,coords.x,coords.y,alpha,transparency,&coords)) MessageBox(0,"Error loading image","Error",0); if(SDL_BlitSurface(mysurface, NULL, screen, &coords)!=0) MessageBox(0,"Error blitting image","Error",0); }
The problem is the images fail to load with the error "Error blitting image" which is caused by SDL_BlitSurface ontop of that if i remove the error code theres a huge memory leek. However if i put the SDL_BlitSurface in: int LoadImg:: load The images are loaded but with a memory leek again. I just want int LoadImg::load() to load an image as a surface which i can blit in update(), any idea why its failing ?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!