Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Instances, that use the same sf::image  (Read 2697 times)

0 Members and 1 Guest are viewing this topic.

Luinechor

  • Guest
Instances, that use the same sf::image
« 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, 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:


How do I have to use this class?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Instances, that use the same sf::image
« Reply #1 on: July 22, 2009, 01:16:38 am »
You use sf::Sprite to share images, and not multiple sf::Image instances, do you?

Quote from: "Luinechor"
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)?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Luinechor

  • Guest
Re: Instances, that use the same sf::image
« Reply #2 on: July 22, 2009, 01:26:29 am »
Quote from: "Nexus"
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).

Quote
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:
   
Code: [Select]
static bool Init(const std::string &ImageFile)
{
return imageShot.LoadFromFile(ImageFile);
}


Thanks for the help!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Instances, that use the same sf::image
« Reply #3 on: July 22, 2009, 01:31:29 am »
Quote from: "Luinechor"
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... ;)

Quote from: "Luinechor"
imageShot is just declared
So that's the problem. Static class variables have to be defined outside the class.
Code: [Select]
class cShot
{
    static sf::Image imageShot;
};

sf::Image cShot::imageShot; // default constructor OR
sf::Image cShot::imageShot(200, 300); // other constructor
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Luinechor

  • Guest
Instances, that use the same sf::image
« Reply #4 on: July 22, 2009, 02:00:49 pm »
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:
Code: [Select]
#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
Code: [Select]
   static bool Init(const std::string& ImageFile)
    {
        return imageShot.LoadFromFile(ImageFile);
    }


exists. Also shown in the tutorial.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Instances, that use the same sf::image
« Reply #5 on: July 22, 2009, 02:03:12 pm »
The following line
Code: [Select]
sf::Image cShot::imageShot;
must be in one (and only one) .cpp, not in the header.
Laurent Gomila - SFML developer

Luinechor

  • Guest
Instances, that use the same sf::image
« Reply #6 on: July 22, 2009, 02:10:29 pm »
Hell, yeah. Of course there are several symbols if it's included in several files.

Thanks!