Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Overriding draw() in sf::Drawable. (Solved.)  (Read 7660 times)

0 Members and 1 Guest are viewing this topic.

Assassin0795

  • Newbie
  • *
  • Posts: 19
    • View Profile
Overriding draw() in sf::Drawable. (Solved.)
« 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);
}
« Last Edit: January 12, 2014, 12:53:10 am by Assassin0795 »

Daddi

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
    • http://foxdev.de/
    • Email
Re: Overriding draw() in sf::Drawable.
« Reply #1 on: January 11, 2014, 10:43:04 pm »
Can you show us the code where you try to initialize the object?

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Overriding draw() in sf::Drawable.
« Reply #2 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);
  ...
}

Daddi

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
    • http://foxdev.de/
    • Email
Re: Overriding draw() in sf::Drawable.
« Reply #3 on: January 11, 2014, 10:55:33 pm »
Didn't he just do that?

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Overriding draw() in sf::Drawable.
« Reply #4 on: January 11, 2014, 10:57:22 pm »
I'm an idiot. I misread his question.

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
Re: Overriding draw() in sf::Drawable.
« Reply #5 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.
TGUI: C++ SFML GUI

Assassin0795

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Overriding draw() in sf::Drawable.
« Reply #6 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. :)

 

anything