Hello there!
I'm trying to draw an entity using inheritance from sf::Drawable like in "Designing your own entities with vertex arrays" tutorial (
http://www.sfml-dev.org/tutorials/2.1/graphics-vertex-array.php).
But as the screen loads I can only see a white rectangle of my sprite, not the actual sprite.
If I load the same texture and assign it to the sprite from Game.cpp the sprite loads properly.
So why isn't the sprite from Player class working properly?
The entity I'm trying to draw:
#include "Player.h"
#include <SFML/Graphics.hpp>
#include <ostream>
const int STARTING_HEALTH = 100;
Player::Player()
{
x=y=20;
dx=dy=1;
health = STARTING_HEALTH;
sf::Texture texture;
texture.loadFromFile("resources/player.jpeg", sf::IntRect(30, 145, 82, 125));
m_sprite.setTexture(texture);
}
void Player::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
target.draw(m_sprite);
}
It's header:
Entity is inherited from sf::Drawable#ifndef PLAYER_H
#define PLAYER_H
#include <SFML/Graphics.hpp>
#include "Entity.h"
class Player : public Entity
{
public:
Player();
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
};
#endif // PLAYER_H
And the main game entry:
Game.cpp
#include <SFML/Graphics.hpp>
#include "Player.h"
int main()
{
sf::RenderWindow window(sf::VideoMode(1000, 800), "Game Window");
Player player;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(player);
window.display();
}
return 0;
}