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

Author Topic: Problems setting up a static image in a class?  (Read 3095 times)

0 Members and 1 Guest are viewing this topic.

pabloist

  • Newbie
  • *
  • Posts: 26
    • View Profile
Problems setting up a static image in a class?
« on: October 08, 2010, 05:55:05 am »
Should this be working? I'm trying to follow the tutorial.
Code: [Select]

class Laser
{
    public:
        Laser()
        {
            image.CreateMaskFromColor(sf::Color(153, 217, 234));
            sprite.SetImage(image);
            speed = 1;
        }
       
        int speed;
        sf::Sprite sprite;
        static sf::Image image;

        void Init();
        bool isColliding(const sf::Sprite& Target);
};

Code: [Select]

void Laser::Init()
{
    image.LoadFromFile("C:/Users/Michael/Desktop/Various_Files/Animation_Resources/Items/RedLaser.png");
}


It gives me errors like "undefined reference to `Laser::image'". What should I do?

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Problems setting up a static image in a class?
« Reply #1 on: October 08, 2010, 09:48:55 am »
Read Some FAQ about static var in class.

Also, you may not want to call this Init method from the outside of the class (because what happens if it is called twice? Or never?) .

To do that keep a static Boolean to know if it was already init and call init in the Ctor.
SFML / OS X developer

pabloist

  • Newbie
  • *
  • Posts: 26
    • View Profile
Problems setting up a static image in a class?
« Reply #2 on: October 09, 2010, 12:13:26 am »
okay, thanks for the advice, I got it working.

epicasian

  • Newbie
  • *
  • Posts: 14
    • View Profile
Problems setting up a static image in a class?
« Reply #3 on: October 10, 2010, 05:16:17 am »
I have the exact same problem, what did you do to fix it?

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Problems setting up a static image in a class?
« Reply #4 on: October 10, 2010, 08:58:19 am »
Just as an advise : The most important thing in programming (and many other fields) is to be able to search by yourself.    :wink:

The search keys are here "C++ static variable inside a class".
Try the exercise.

A Solution :
Here :  http://www.learncpp.com/cpp-tutorial/811-static-member-variables/
SFML / OS X developer

epicasian

  • Newbie
  • *
  • Posts: 14
    • View Profile
Problems setting up a static image in a class?
« Reply #5 on: October 10, 2010, 05:41:22 pm »
I understand the way I have to initialize it, but I don't know how to in SFML. I tried this:

Stick.cpp
Code: [Select]

sf::Image Stick::StickImage = StickImage.Create(blah)


How would you do something like this in SFML? I've looked in the Docs, but haven't found anything yet.

Thanks in advance.

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Problems setting up a static image in a class?
« Reply #6 on: October 10, 2010, 06:30:23 pm »
okay, that's something else. You're mixing constructing an object and loading the image.

To create your static image, you have to use the constructor of sf::Image like this :
Code: [Select]
sf::Image Stick::StickImage = sf::Image(); // Or any other Ctor.

Then you have to load it. This process is in fact not related to the above construction. You can load the image in the Ctor of you class, but it wouldn't be smart to load your image many times. So you could have something like this :
Code: [Select]
class Titi {
  static sf::Image tata;
  static void LoadTataOnce();
public:
  Titi();
};
/*--*/
sf::Image Titi::tata; // Default Ctor

Titi::Titi() {
  LoadTataOnce();
}

void Titi::LoadTataOnce() {
  static bool is_loaded = false; // initialized once to false for all the program time life.
  if (!is_loaded) {
    tata.LoadFromFile(...) or tata.Create(...);
    is_loaded = true; // Updated this static var to load only once tata.
  }
}


As you can see it is not directly related to SFML but to a more general code structure : every time you have such static variables (that need some function call like create or load after the ctor) you can apply the above structure.

But sometimes it's not the best solution and ResourceManager are more useful. That's let you delegate responsibilities to some other classes/functions.

BTW, this may have some interest for you : http://geosoft.no/development/cppstyle.html/

Have fun!
SFML / OS X developer

 

anything