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

SDL Class (with surface) object assignment

Started by kickstep Mar 14, 2009 at 9:08 AM 2 replies 850+ views
Original Post
kickstep
kickstep
I have a class which creates a surface from bitmap in the constructor (and frees that surface with the destructor). now of course since the surface is freed by the destructor i cannot assign an object of the class to another. how would i go about that? my best attempt is this: #include "SDL.h" #include "Maps.h" #include TileSet::TileSet(int w, int h, int tcount, char * bmp) : width(w), height(h), tilecount(tcount), bitmap(SDL_LoadBMP(bmp)) { } TileSet::~TileSet() {SDL_FreeSurface(bitmap);} TileSet & TileSet::operator=(const TileSet & rs) { if (this == &rs) return *this; //std::strcpy(label, rs.label); //rating = rs.rating; //SDL_BlitSurface(rs.bitmap, NULL, bitmap, NULL); bitmap = SDL_ConvertSurface(rs.bitmap, rs.bitmap->format, rs.bitmap->flags); width = rs.width; height = rs.height; tilecount = rs.tilecount; return *this; }
Mybowlcut
Mybowlcut
Have a look at this thread for some ideas on how to encapsulate surfaces so they are easily copied.

sheep19
sheep19
This is how I do it:

Surface &Surface::operator =(SDL_Surface *s){	// free the current surface	free();		// if the source surface is not null, we need to free it first	if(s)		surface = SDL_DisplayFormat(s);	else		surface = 0;	return *this;}
rip-off
rip-off
Here is a recent discussion on the design of a SDL_Surface wrapper which may solve your problem.

Topic Locked

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

Sign in to reply to this topic.