Sign in to follow this  

SDL Class (with surface) object assignment

Recommended Posts

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 <iostream> 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; }

Share this post

Link to post
Share on other sites
This is how I do it:

Surface &Surface::operator =(SDL_Surface *s)
// free the current surface

// if the source surface is not null, we need to free it first
surface = SDL_DisplayFormat(s);
surface = 0;

return *this;

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this