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

Author Topic: Sprites and loading images  (Read 2994 times)

0 Members and 1 Guest are viewing this topic.

sludge

  • Newbie
  • *
  • Posts: 24
    • View Profile
Sprites and loading images
« on: July 06, 2010, 07:52:07 pm »
In my program, I have a single image that will be displayed multiple times.  In the tutorials, I read that a good way to handle such a situation is to make a static image that every sprite accesses.  I took the code from the tutorial, and now I'm trying to use it, but I'm getting a few issues.

first let me post the code:
Code: [Select]


////////////VariableLabel.h
#include "headers.h"

#pragma once
class VariableLabel
{
public:
VariableLabel();
static bool Initialize(const std::string& ImageFile);

private:
    static sf::Image image;      // Each sprite object shares this single image
    sf::Sprite Sprite;           // But there is one sprite per variable label
double value;
};

////////////VariableLabel.cpp
#include "VariableLabel.h"

VariableLabel::VariableLabel()
{
Sprite.SetImage(image); // Every sprite uses the same image
}

bool VariableLabel::Initialize(const std::string& ImageFile)
{
return image.LoadFromFile(ImageFile);
}



My questions concerns the implementation of the constructor.  If I declare an instance of VariableLabel before calling the static function "static bool Initialize(const std::string& ImageFile)", when I try to draw the sprite that the VariableLabel contains, nothing is drawn.  I assume that this is because image is not initialized when the constructor of VariableLabel is called, so the sprite is never assigned an image.  

If I modify my code a bit and create an instance of VariableLabel after calling the initialize function, things work properly.  Is there anyway to modify the class so that the constructor is still able properly assign images to the sprites before calling the initialize function.  I only ask because I would like the call the initialize function inside a function of a class that contains VariableClass objects as its members.

Thank you.

-Nick

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Sprites and loading images
« Reply #1 on: July 06, 2010, 08:06:42 pm »
It should work even if the image has not been loaded yet. Your problem is probably that the subrect is not properly initialized, because the image has a null size when you call SetImage; you have to adjust it after the image is initialized (with the SetSubRect function).

What you can do is to call initialize() automatically from the constructor, when the first instance is created.
Laurent Gomila - SFML developer

sludge

  • Newbie
  • *
  • Posts: 24
    • View Profile
Sprites and loading images
« Reply #2 on: July 06, 2010, 09:09:17 pm »
I thought about doing that, but isn't it redundant to call this static initialize function every single time a new instance of VariableLabel is created?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Sprites and loading images
« Reply #3 on: July 06, 2010, 11:15:12 pm »
I didn't say "call it every time", I said "call it the first time" ;)

Basically, you need a static boolean to store the "initialization state": it is initialized to false and then set to true after the first object is constructed and initialize() has been called.
Laurent Gomila - SFML developer

e_barroga

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Sprites and loading images
« Reply #4 on: July 12, 2010, 02:21:43 am »
Why don't you just store the images in a stl map container?