Original Post
If I am sending sf::Sprite objects between classes should I be using pointers?
Depends on what you want. It's safe to not use pointers, but you have to realize that the two classes will each have their own copy of the [font=courier new,courier,monospace]sf::Sprite[/font] if you don't use references/pointers. If you want them to share the same [font=courier new,courier,monospace]sf::Sprite[/font], you have to use references/pointers.
As a general rule of thumb, I pass structs and classes by const reference or non-const references, and only copy something when I actually want a duplicate.
Basic variables like int and float I pass by value, normally.
Could you show a real piece of code as an example, to clarify your question?
class Enemy
{
// snip
public:
sf::Sprite &getSprite(){return sprite_;}
private:
sf::Sprite sprite_;
};
class Enemy
{
public:
static bool LoadResources(); //Loads all the static textures. Called once at program startup.
enum class Appearance {Normal, Fast, Powerful, Dead};
public:
Enemy();
void SetAppearance(Appearance appearance)
{
if(appearance == Appearance::Fast)
{
this->sprite.setTexture(Enemy::fastTexture);
}
else if(appearance == Appearance::Powerful)
{
this->sprite.setTexture(Enemy::powerfulTexture);
}
else if(appearance == Appearance::Dead)
{
this->sprite.setTexture(Enemy::deadTexture);
}
else //Normal appearance.
{
//Randomly choose one of the two 'normal' enemy textures.
if( random choice 1 )
{
this->sprite.setTexture(Enemy::normalTexture1);
}
else
{
this->sprite.setTexture(Enemy::normalTexture2);
}
}
}
private:
sf::Sprite sprite; //Each enemy has it's own sprite.
static sf::Texture normalTexture1; //Normal enemy texture, shared between all 'Enemy' instances.
static sf::Texture normalTexture2; //An alternative texture for normal enemies.
static sf::Texture fastTexture; //Texture for 'fast' enemies.
static sf::Texture powerfulTexture; //Texture for 'powerful' enemies.
static sf::Texture deadTexture; //Texture for dead enemies.
};
As JTippets mentioned, just give each object its own sprite. You could share textures between similar types of objects, but you don't need to do all the hassle of a "graphics manager", especially when that "manager" doesn't actually manage the lifetime of your graphics.
But hey, if every Enemy has the same appearance, you could even share their texture statically:
class Enemy
{
public:
static bool LoadResources(); //Loads all the static textures. Called once at program startup.
enum class Appearance {Normal, Fast, Powerful, Dead};
public:
Enemy();
void SetAppearance(Appearance appearance)
{
if(appearance == Appearance::Fast)
{
this->sprite.setTexture(Enemy::fastTexture);
}
else if(appearance == Appearance::Powerful)
{
this->sprite.setTexture(Enemy::powerfulTexture);
}
else if(appearance == Appearance::Dead)
{
this->sprite.setTexture(Enemy::deadTexture);
}
else //Normal appearance.
{
//Randomly choose one of the two 'normal' enemy textures.
if( random choice 1 )
{
this->sprite.setTexture(Enemy::normalTexture1);
}
else
{
this->sprite.setTexture(Enemy::normalTexture2);
}
}
}
private:
sf::Sprite sprite; //Each enemy has it's own sprite.
static sf::Texture normalTexture1; //Normal enemy texture, shared between all 'Enemy' instances.
static sf::Texture normalTexture2; //An alternative texture for normal enemies.
static sf::Texture fastTexture; //Texture for 'fast' enemies.
static sf::Texture powerfulTexture; //Texture for 'powerful' enemies.
static sf::Texture deadTexture; //Texture for dead enemies.
};
Each object does have its own sprite. I load up all the textures in the graphics class. Then I create a sprite and send it over to another class. I only do this once per class. Anyway thanks for the advice everyone
player.setTexture(graphicsManager.getTexture("playerSprite.png"));
IF you have a Sprite object that is the same through out your program, you should use the same sprite, no need to have mutliple of the same image loaded. Creating a sprite manager class that manages them and has methods for getting pointers to sprites, and/or use shared_ptr/weak_ptr.
[quote name='EddieV223' timestamp='1355697194' post='5011379']
IF you have a Sprite object that is the same through out your program, you should use the same sprite, no need to have mutliple of the same image loaded. Creating a sprite manager class that manages them and has methods for getting pointers to sprites, and/or use shared_ptr/weak_ptr.
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more