Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Loading images in SDL help.

Started by RobN Oct 15, 2006 at 10:51 AM 0 replies 800+ views
Original Post
RobN
RobN
Im using SDL in Devc++ and writing my first game and am having a problem with image loading. Heres my code: Object class .h
Quote:
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
Quote:
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
Quote:
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:
Quote:
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 ?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.