Problem with Sprite class and SDL

Hi

I wanna have a root class (Sprite) which I want shall hold a name and an image (SDL_Surface). But when i am creating an object of Sprite, say spaceship, the program underlines either the first string class or the sdl_surface in the constructor in main.cpp

My code:
1
2
3
4
5
6
7
8
9
10
//Main.cpp:
//Initialize the sprites
Sprite s1("spaceship", "spaceship.gif", spaceship.gif);

//Sprite.cpp
Sprite::Sprite(std::string name, std::string filename, SDL_Surface* spriteImage)
		:name(name), name_image(filename), si(spriteImage) {si=IMG_Load(name_image.c_str());}

//Sprite.h
Sprite(std::string name, std::string name_image, SDL_Surface* spriteImage);
closed account (N85iE3v7)
Hey,

There might be a problem at :

 
Sprite s1("spaceship", "spaceship.gif", spaceship.gif);


The compiler will interpret spaceship.gif as if you were passing a structure or class spaceship with a member named "gif" like:

1
2
3
4
5
class CSpaceship
{
    // ...
    Sometype gif;
};



Seeing from the constructor you posted, it seems you need to pass an instance of the surface instead, something like:

1
2
// assuming that the SDL_Surface constructor takes as string as parameter
Sprite s1("spaceship", "spaceship.gif", new SDL_Surface ( "spaceship.gif" ));


Take a look at this very silly example:

1
2
3
4
5
6
7
8
9
10
11
12
13
class t{
public:
	t(string& t){};
};

class z{
public:
	z(t* ptr_t){};
};

// then later one you instantiate z
string a("");
z zInstance(new t(a));
Last edited on
Well okey, but how do I make the constructor so it takes a name, and the filename of the image source example: spaceship, "C/Images/spaceship.gif"

The best thing i wanted to do from the start was an Image class which holds a SDL_Surface pointer which will have a automatic reference countering and cleaning of the images.


closed account (N85iE3v7)
Just like you did just pass the path to the constructor:

 
Sprite s1("spaceship", "spaceship.gif", new SDL_Surface ( "c:\\Images\\spaceship.gif" ));


I am not sure but I think that this SDL_Surface class simply creates the source for you:

1
2
3
4
5
6
// here the image is loaded and placed somewhere in the class SDL_Surface
 SDL_Surface* YourSurface =   new SDL_Surface( "c:\\Images\\spaceship.gif" ));
 Sprite s1("spaceship", "spaceship.gif", YourSurface )
 // then inside of the class implementation of Sprite you may access 
 spriteImage->Your_Methods_Here();

Last edited on
well if i put new SDL:Surface ("filename"); it says:

Error: no suitable constructor exists to convert from "const char[14]" to "SDL_Surface"

If I just put another SDL_Surface which I have decleared before the Sprite s1(...); it works, but then the whole idea with sprites is pretty useless ^^
Last edited on
closed account (N85iE3v7)
Ok,

so please post here the constructor for SDL_Surface or most likely....

I actually gave a brief look at internet, I know zero of SDL by the way, but it seems SDL_Surface is a struct . I am not sure what you are doing after passing it to the constructor of your class. In this case you declare :

 
SDL_Surface* YourSurface;


and pass it to the constructor of sprite and let this class fill this structure.

Last edited on
yeah its a struct, since its made with C, and not C++.

It is compatible with c+ tho :P, but okey

This is my code:
This is in main.cpp.
Sprite s1("spaceship", "spaceship-sprite.gif", spriteObject);
then I declare (above):

SDL_Surface* spriteObject = IMG_Load( "spaceship-sprite.gif" );


and it does not complain nor does it work.

My code in Sprite class:

Sprite(std::string name, std::string name_image, SDL_Surface* spriteImage);
and header file:
1
2
3
4
Sprite::Sprite(std::string name, std::string filename, SDL_Surface* spriteImage)
		:name(name), name_image(filename), si(spriteImage) {
			si=IMG_Load(name_image.c_str());
	}


It works without the constructor of Sprite (Sprite s1(...))

I am not doing anything else with the Sprite object yet.
Last edited on
closed account (N85iE3v7)
When you try

 
SDL_Surface* spriteObject = IMG_Load( "spaceship-sprite.gif" );


Is spriteObject different from NULL ?
yeah, don't know if it becomes NULL after i send it into the sprite object.
closed account (N85iE3v7)
Are you making two calls to IMG_LOAD ? One in your main and the other at your constructor ? if so, try just calling it once, please change your Sprite class constructor to:

1
2
3
4
5
6
7
Sprite::Sprite(std::string name, std::string filename, SDL_Surface* spriteImage)
		:name(name), name_image(filename)
{
			// si=IMG_Load(name_image.c_str());
                            si = spriteImage;// you may left as you were implementing
                                                       // by assigning here you can debug more easily
}


Also at your main put a breakpoint at the place where you get the spriteObject reference and then inspect what that memory location has, i.e, inspect the struct. It might have some useful data rather than memory garbage.

Next, do the same inside the constructor.
But I still need to have an SDL_Surface object in the main.

I want the sprite object to hold everything about it- even the image.
closed account (N85iE3v7)
You still have, what you pass to Sprite is a pointer to the SDL_Surface, so that inside it you have a reference to this object. You are not creating anything new expect if you call IMG_Load.
Topic archived. No new replies allowed.