Greetings.
So I'm a bit of a C++ noob and using SFML 2.1 to make a little game project to get better at programming.
I'm having problems when creating an inheritance hierarchy for the entities I plan on using for my game.
I made an Entity class in a separate header to use as a base class to derive other stuff like a player class and enemy class from.
What would the best way of having it hold a 'draw' function that the derived classes can use to draw themselves to the RenderWindow that I have in my main() be?
The tutorial on 'Creating a SFML-like entity' was confusing, and the example code gives me
'object of abstract class type "Entity" is not allowed;' errors.
I'm not going to be using vertex arrays. Just simple shapes (rectangles, circles, sprites) for now.
Entity.h
class MyEntity : public sf::Drawable, public sf::Transformable
{
public:
// Some stuff
private:
// pure virtual function
virtual void drawStuff(sf::RenderTarget& target, sf::RenderStates states) const = 0;
};
Player.h
#include "Entity.h"
class Player : public MyEntity
{
public:
// Player stuff
private:
void drawStuff(sf::RenderTarget& target, sf::RenderStates states) const
{
states.transform *= getTransform();
target.draw(playerIcon, states);
};
int playerXPosition;
int playerYPosition;
// Rectangle to represent player (placeholder)
sf::RectangleShape playerIcon;
};
Output
1>Game.cpp(22): error C2259: 'MyEntity' : cannot instantiate abstract class
1> due to following members:
1> 'void sf::Drawable::draw(sf::RenderTarget &,sf::RenderStates) const' : is abstract
1> C:\Users\User\Documents\Programming\SFML-2.1-windows-vc10-32bits\SFML-2.1\include\SFML/Graphics/Drawable.hpp(69) : see declaration of 'sf::Drawable::draw'
1> 'void MyEntity::drawStuff(sf::RenderTarget &,sf::RenderStates) const' : is abstract
1> c:\users\user\documents\programming\crappygame\crappygame\Entity.h(13) : see declaration of 'MyEntity::drawStuff'
1>Game.cpp(23): error C2259: 'Player' : cannot instantiate abstract class
1> due to following members:
1> 'void sf::Drawable::draw(sf::RenderTarget &,sf::RenderStates) const' : is abstract
1> C:\Users\User\Documents\Programming\SFML-2.1-windows-vc10-32bits\SFML-2.1\include\SFML/Graphics/Drawable.hpp(69) : see declaration of 'sf::Drawable::draw'
Why can't I instantiate an abstract class?
To my knowledge the pure virtual drawStuff function is what makes Entity abstract, but I've tried removing it and still get the errors.
Also, what is the
'sf::RenderStates states' parameter meant to do and how would
'states' be passed in as an argument when calling the function?? I've looked at the documentation but it didn't make any sense to me.
Also, I tried searching the forums but the search thing appears broken. I tried it a few times and it told me to inform an admin.
I know I've made a rat's ass of this whole thing but please don't hate me.
Windows 7, Visual C++ 2010, Release config.