SFML community forums
Help => Graphics => Topic started by: Luinechor on July 22, 2009, 01:07:02 am
-
Hey,
I did use the search-function in this forum, but I could'nt find an answer.
Well, I need like 500 instances from a class with an sf::image, so they have to use the same image. I wrote the code as shown in
this tutorial (http://www.sfml-dev.org/tutorials/1.5/graphics-sprite.php), but I don't know how to really use it.
The class is called cShot and if I call cShot::Init (shown in the tutorial) I get one linking error:
(http://www.quicksnapper.com/files/6936/7352222114A66493059206.png)
How do I have to use this class?
-
You use sf::Sprite to share images, and not multiple sf::Image instances, do you?
The class is called cShot and if I call cShot::Init (shown in the tutorial) I get one linking error:
What is imageShot? Did you link SFML? Did you define imageShot (not only declare)?
-
You use sf::Sprite to share images, and not multiple sf::Image instances, do you?
I don't really understand this part.
It's just the static sf::Image, in the cShot-Class there's sf::Sprite Sprite, to store the position and things like this for every instance (because every missile has it's own position).
What is imageShot? Did you link SFML? Did you define imageShot (not only declare)?
imageShot ist the static sf::Image.
I used the SFML-Graphics Application Template for Xcode (Mac OS X 10.5.7) so I don't have to link it by myself.
imageShot is just declared, it's also only used by:
static bool Init(const std::string &ImageFile)
{
return imageShot.LoadFromFile(ImageFile);
}
Thanks for the help!
-
I don't really understand this part.
I wasn't sure, somehow it sounded like you would use images in the way sprites are designed. Sorry... ;)
imageShot is just declared
So that's the problem. Static class variables have to be defined outside the class.
class cShot
{
static sf::Image imageShot;
};
sf::Image cShot::imageShot; // default constructor OR
sf::Image cShot::imageShot(200, 300); // other constructor
-
Thanks, that worked.
The problem now is, that the compiler says:
ld: duplicate symbol cShot::imageShot in /Users/.../shot.o and /Users/.../main.o
The shot.h looks like:
#include <SFML/Graphics.hpp>
class cShot {
public:
void Draw();
void Update(sf::Event *Event);
int Initialize(sf::RenderWindow *apWindow);
int gWidth;
int gHeight;
int Speed;
cShot()
{
spriteShot.SetImage(imageShot); // every sprite uses the same unique image
}
static bool Init(const std::string& ImageFile)
{
return imageShot.LoadFromFile(ImageFile);
}
private:
sf::RenderWindow *pWindow;
sf::Sprite spriteShot;
static sf::Image imageShot;
};
sf::Image cShot::imageShot;
In shot.cpp 'imageShot' isn't used anytime. In main.cpp I'm just including the shot.h. I also didn't create an instance of cShot.
Uh, and I can't load an Image for 'imageShot' from a file (with the constructor), that's why the
static bool Init(const std::string& ImageFile)
{
return imageShot.LoadFromFile(ImageFile);
}
exists. Also shown in the tutorial.
-
The following line
sf::Image cShot::imageShot;
must be in one (and only one) .cpp, not in the header.
-
Hell, yeah. Of course there are several symbols if it's included in several files.
Thanks!