Here is the code I have. The code works and displays the image from the class just fine. I was just wondering if anyone could look it over and make sure I'm doing it properly.
Basically I want to make sure if I create 100 bullets from the Bullet class, that bullet.png is only really loaded once, and that all 100 sprites are using the same image.
Bullet.h
#ifndef BULLET_H_INCLUDED
#define BULLET_H_INCLUDED
#include <SFML/Graphics.hpp>
class Bullet
{
private:
static sf::Image Image;
sf::Sprite bulletSprite;
public:
Bullet();
~Bullet();
void loadImage();
void draw(sf::RenderWindow &App);
};
#endif // BULLET_H_INCLUDED
Bullet.cpp
#include "bullet.h"
Bullet::Bullet()
{
}
Bullet::~Bullet()
{
}
void Bullet::loadImage()
{
static sf::Image Image;
if (!Image.LoadFromFile("bullet.png"))
{
// My Error message...
}
bulletSprite.SetImage(Image);
bulletSprite.SetColor(sf::Color(0, 255, 255, 128));
bulletSprite.SetPosition(20.f, 20.f);
bulletSprite.SetScale(2.f, 2.f);
bulletSprite.SetCenter(0, 0);
}
void Bullet::draw(sf::RenderWindow &App)
{
App.Draw(bulletSprite);
}
Main.cpp
#include <SFML/Graphics.hpp>
#include "bullet.h"
int main()
{
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
while (App.IsOpened())
{
sf::Event Event;
while (App.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
App.Close();
}
Bullet theBullet;
theBullet.loadImage();
App.Clear();
theBullet.draw(App);
App.Display();
}
return EXIT_SUCCESS;
}