1
Graphics / Re: A noobie needs help with drawing a bullet sprite
« on: April 17, 2013, 12:30:15 pm »
I finally managed to draw the bullet sprite
The error was in the Projectile.cpp where I tried to make an instance of the Projectile class:
The sprite drew itself allright after I changed that line to this:
The problem was not slicing as I thought but that I had made an extra local variable "bullet" that shadowed the one I had declared.
The "rule of three" led me to do some research into the copy and assingment constructors.
However I didn't include them in order to keep this example as minimal as possible.
Thank you all for your help!
Below are the three sourcefiles.
Sprite.cpp:
Projectile.h
Projectile.cpp:
The error was in the Projectile.cpp where I tried to make an instance of the Projectile class:
sf::Sprite bullet(bulletImage);
The sprite drew itself allright after I changed that line to this:
bullet = sf::Sprite(*bulletImage);
The problem was not slicing as I thought but that I had made an extra local variable "bullet" that shadowed the one I had declared.
The "rule of three" led me to do some research into the copy and assingment constructors.
However I didn't include them in order to keep this example as minimal as possible.
Thank you all for your help!
Below are the three sourcefiles.
Sprite.cpp:
#include <SFML/Graphics.hpp>
#include "projectile.h"
int main()
{
sf::Image bulletImage;
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
// Load an image for the bullet sprite
if (!bulletImage.LoadFromFile("images/bullet.png"))
return EXIT_FAILURE;
// Create the sprite for the bullet with a given position
Projectile bulletInst = Projectile(&bulletImage, 250.f, 150.f);
while (App.IsOpened())
{
sf::Event Event;
while (App.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
App.Close();
}
App.Clear();
App.Draw(bulletInst.getBullet());
App.Display();
}
return EXIT_SUCCESS;
}
#include "projectile.h"
int main()
{
sf::Image bulletImage;
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
// Load an image for the bullet sprite
if (!bulletImage.LoadFromFile("images/bullet.png"))
return EXIT_FAILURE;
// Create the sprite for the bullet with a given position
Projectile bulletInst = Projectile(&bulletImage, 250.f, 150.f);
while (App.IsOpened())
{
sf::Event Event;
while (App.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
App.Close();
}
App.Clear();
App.Draw(bulletInst.getBullet());
App.Display();
}
return EXIT_SUCCESS;
}
Projectile.h
#ifndef PROJECTILE_H
#define PROJECTILE_H
#include <SFML/Graphics.hpp>
class Projectile
{
public:
Projectile();
Projectile(sf::Image const *, float, float);
sf::Sprite getBullet() const;
virtual ~Projectile();
protected:
private:
sf::Sprite bullet;
float x_;
float y_;
};
#endif // PROJECTILE_H
#define PROJECTILE_H
#include <SFML/Graphics.hpp>
class Projectile
{
public:
Projectile();
Projectile(sf::Image const *, float, float);
sf::Sprite getBullet() const;
virtual ~Projectile();
protected:
private:
sf::Sprite bullet;
float x_;
float y_;
};
#endif // PROJECTILE_H
Projectile.cpp:
#include "projectile.h"
#include <SFML/Graphics.hpp>
#include <iostream>
using std::cout;
using std::endl;
Projectile::Projectile()
{
cout << "The bullet is here" << endl;
}
Projectile::Projectile(sf::Image const * bulletImage, float x, float y) : x_(x), y_(y)
{
bullet = sf::Sprite(*bulletImage);
bullet.SetPosition(x_, y_);
cout << "The bullet is here with a position (x, y): (" << x_ << ", " << y_ << ")"<< endl;
}
sf::Sprite Projectile::getBullet() const
{
return bullet;
}
Projectile::~Projectile()
{
cout << "The bullet was deleted in a position (x, y): (" << x_ << ", " << y_ << ")"<< endl;
}
#include <SFML/Graphics.hpp>
#include <iostream>
using std::cout;
using std::endl;
Projectile::Projectile()
{
cout << "The bullet is here" << endl;
}
Projectile::Projectile(sf::Image const * bulletImage, float x, float y) : x_(x), y_(y)
{
bullet = sf::Sprite(*bulletImage);
bullet.SetPosition(x_, y_);
cout << "The bullet is here with a position (x, y): (" << x_ << ", " << y_ << ")"<< endl;
}
sf::Sprite Projectile::getBullet() const
{
return bullet;
}
Projectile::~Projectile()
{
cout << "The bullet was deleted in a position (x, y): (" << x_ << ", " << y_ << ")"<< endl;
}