SFML community forums

Help => Graphics => Topic started by: Assassin0795 on January 11, 2014, 10:28:35 pm

Title: Overriding draw() in sf::Drawable. (Solved.)
Post by: Assassin0795 on January 11, 2014, 10:28:35 pm
Hello,

So I picked up SFML a few days ago, and so far it's been going pretty well. However, while I was making an entity class and trying to override the draw() function from sf::Drawable, when I went to make an entity, it said that I couldn't instantiate an abstract class - that is, even though I had thought I overrode draw(), it told me I hadn't. I've been searching StackOverflow, these forums, and Google for awhile now and haven't found an answer, and was hoping that someone here could point out what I did wrong.

My code:

 // Entity.h
#ifndef ENTITY_H_
#define ENTITY_H_

#include "SFML\Graphics.hpp"

class Entity : public sf::Drawable, public sf::Transformable {

private:

        sf::VertexArray vertices;
        sf::Texture texture;
        sf::Sprite sprite;

public:

    // Miscellaneous functions

    virtual void draw(sf::RenderTarget &target, sf::RenderStates &states) const;

};

#endif

 // Entity.cpp
#include "Entity.h"

// Miscellaneous functions

void Entity::draw(sf::RenderTarget &target, sf::RenderStates &states) const {
        states.transform *= getTransform();
        states.texture = &texture;
        target.draw(vertices, states);
}
Title: Re: Overriding draw() in sf::Drawable.
Post by: Daddi on January 11, 2014, 10:43:04 pm
Can you show us the code where you try to initialize the object?
Title: Re: Overriding draw() in sf::Drawable.
Post by: Jesper Juhl on January 11, 2014, 10:51:25 pm
sf::Drawable::draw() is pure:
  virtual void draw(RenderTarget& target, RenderStates states) const = 0;
You must implement it when you inherit from sf::Drawable.

Simply do:

class Entity : public sf::Drawable
{
public:
  ... your public stuff.
private:
  void draw(sf::RenderTarget& target, sf::RenderStates states) const;
};

and then write an implementation for Entity::draw()... it will need to do something along the lines of this:

void Entity::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
  ...
  target.draw(<some_sprite_or_whatnot>, states);
  ...
}
Title: Re: Overriding draw() in sf::Drawable.
Post by: Daddi on January 11, 2014, 10:55:33 pm
Didn't he just do that?
Title: Re: Overriding draw() in sf::Drawable.
Post by: Jesper Juhl on January 11, 2014, 10:57:22 pm
I'm an idiot. I misread his question.
Title: Re: Overriding draw() in sf::Drawable.
Post by: texus on January 11, 2014, 11:50:27 pm
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const = 0;
virtual void draw(sf::RenderTarget &target, sf::RenderStates &states) const;

There is a small difference between these lines, the states parameter shouldn't be a reference.
So you aren't overriding the old method.
Title: Re: Overriding draw() in sf::Drawable.
Post by: Assassin0795 on January 12, 2014, 12:51:40 am
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const = 0;
virtual void draw(sf::RenderTarget &target, sf::RenderStates &states) const;

There is a small difference between these lines, the states parameter shouldn't be a reference.
So you aren't overriding the old method.

That was it; it works now. Thank you, everyone. :)