SFML RenderTarget& Problem

I cannot figure out how to draw my object using my own Object class using SFML.

Here is my class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Object.h
#ifndef OBJECT_H
#define OBJECT_H

#include "SFML/Graphics.hpp"


class Object
{
    public:
        Object();
        virtual ~Object();
        virtual void Create(char* ImageFileName);
        virtual void Update();
        virtual void Display();

    protected:
    private:
//        Vector2d position;
        bool visible;
        char* ImageFileName;
        sf::Sprite sprite;
        sf::Image image;
};

#endif // OBJECT_H 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Object.cpp
#include "Object.h"
#include "SFML/Graphics.hpp"

Object::Object()
{
    //ctor
}

Object::~Object()
{
    //dtor
}

void Object::Create(char* ImageFileName)
{
    visible = true;//may be moved/changed
    image.LoadFromFile(ImageFileName);//load an image file for object
    sprite.SetImage(image);//set the object sprites image
}

void Object::Update()
{
    //set location etc...
}

void Object::Display()
{
    sf::Sprite::Draw();
}


Here is the error:
E:\CodeBlocks-Programs\SFML Tutorial 02\Object.cpp||In member function 'virtual void Object::Display()':|
E:\CodeBlocks-Programs\SFML Tutorial 02\Object.cpp|27|error: no matching function for call to 'sf::Sprite::Draw()'|
E:\CodeBlocks-Programs\SFML Tutorial 02\Object.cpp|27|note: candidate is:|
e:\programs\codeblocks-ep\mingw\bin\..\lib\gcc\mingw32\4.6.2\..\..\..\..\include\SFML\Graphics\Drawable.hpp|333|note: void sf::Drawable::Draw(sf::RenderTarget&) const|
e:\programs\codeblocks-ep\mingw\bin\..\lib\gcc\mingw32\4.6.2\..\..\..\..\include\SFML\Graphics\Drawable.hpp|333|note:   candidate expects 1 argument, 0 provided|
||=== Build finished: 4 errors, 0 warnings (0 minutes, 2 seconds) ===|

I just want to do this:
1
2
3
Object fred;
fred.Create("fredimage.png");
fred.Draw();
But, you can't do that, because Objects don't have a Draw method.

You pasted the error message in. Did you bother to read it?
Yes. I have no idea what to do with the RenderTarget or what it is. I looked it up but can't make heads or tails of it. Should I just app.draw(fred)?

Edit: Wait...don't sf::Sprites have Draw() methods? Never mind. That would be sf::RenderWindow()

What would I do with Object::Display() then? A lot of tutorials have similair methods.
Last edited on
I suspect you would change

1
2
3
4
void Object::Display()
{
    app.Draw(sprite);
}


But, I'm not familiar with the library. Perhaps a forum specific to it might be more appropriate.

http://www.sfml-dev.org/forum/
Last edited on
Thanks for the response. I have never failed to get a good answer about any topic even remotely related to C++ here. In response to your response:
RenderWindow app(blah blah blah); Creates a window named app. I think it is an object of a seperate class. I want to be able to reuse the Object class. Wouldn't doing it your way be like saying:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class first
{
void say(string Blah)
{
//Blah
}
};
class second
{
void draw(Sprite& Sprite)
{
thefirst.Draw(ItsSprite);
}
};

int main()
{
first thefirst;

return 0;
}

??
Last edited on
Like I said, pure speculation on my part. But since you have a sf::Sprite member sprite in the Object class, I think you're going to have hard time keeping it decoupled from the gui interface.
Can you please explain what you mean by decoupled from the gui? I thought I wanted the graphics coupled?
Topic archived. No new replies allowed.