Sign in to follow this  

Need help with making a class for a Surface

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

I need some input regarding a class I made for my game I'm trying to make in C++ with SDL.
The idea for the class is that it contains a SDL_Surface * that is created with its constructor and it should update the refcount of the surface for the user.

My header file:
[code]

#ifndef IMAGE_H
#define IMAGE_H

#include "SDL.h"

#include <string>

class Image
{
public:
Image();
Image(bool b, std::string filename);
~Image();
Image(const Image& other);
const Image& operator = (const Image& other);
SDL_Surface * getSurface();
void kill();

private:
SDL_Surface * picture;
SDL_Surface * opPicture;
};

#endif
[/code]


The .cpp:
[code]

#include "Image.h"
#include "SDL.h"
#include <string>
#include <iostream>

using namespace std;

Image::Image()
{
picture = NULL;
opPicture = NULL;
}

Image::Image(bool b, string filename)
{
picture = SDL_LoadBMP(filename.c_str());

if(picture != NULL)
{
opPicture = SDL_DisplayFormat(picture);
SDL_FreeSurface(picture);
cout << "Picture is working" << endl;

if(b == true)
{
Uint32 transparent = *(Uint32*) opPicture -> pixels;
SDL_SetColorKey(opPicture, SDL_SRCCOLORKEY | SDL_RLEACCEL, transparent);
cout << "Transparent is working" << endl;
}
}
}

Image::Image(const Image & other)
{
other.opPicture -> refcount++;
}

const Image& Image::operator =(const Image& other)
{
if(this != &other)
{
SDL_FreeSurface(opPicture);
opPicture = other.opPicture;

other.opPicture -> refcount++;
}

return * this;
}

Image::~Image()
{
SDL_FreeSurface(opPicture);
}

void Image::kill()
{
delete this;
}

SDL_Surface * Image::getSurface()
{
return opPicture;
}
[/code]


As far as I can see it should work properly but when I try to make another Image object that uses the same surface it does not work. The surface in the other image is NULL.
I'm sure that there is a stupid reason why it does not work but I can not see it at the moment and maybe you can give me some input.

Share this post


Link to post
Share on other sites
In the copy constructor you only update the reference count but you never update the opPicture pointer.[code]opPicture = other.opPicture;[/code]

The picture pointer don't need to be a member of Image because it is only used in one of the constructors.

Share this post


Link to post
Share on other sites
[quote name='Wooh' timestamp='1311172816' post='4837979']
In the copy constructor you only update the reference count but you never update the opPicture pointer.[code]opPicture = other.opPicture;[/code]

The picture pointer don't need to be a member of Image because it is only used in one of the constructors.
[/quote]

Heh, I missed that...

After adding [code] opPicture = other.opPicture[/code] in the copy constructor it works like a charm. Thanks!

Share this post


Link to post
Share on other sites

This topic is 2340 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.

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