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

Poll

Do I have my Object structure set up somewhat correctly?

Yes
0 (0%)
No
0 (0%)
Learn to program before asking silly questions
1 (100%)

Total Members Voted: 1

Voting closed: February 24, 2012, 02:33:45 pm

Author Topic: sf::RenderTarget& Question  (Read 5664 times)

0 Members and 1 Guest are viewing this topic.

ZeroApoc

  • Newbie
  • *
  • Posts: 6
    • View Profile
sf::RenderTarget& Question
« on: February 19, 2012, 02:33:45 pm »
I cannot figure out how to draw my object using my own Object class using SFML.

Here is my class:

Code: [Select]
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


Code: [Select]

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:

Code: [Select]
Object fred;
fred.Create("fredimage.png");
fred.Draw();

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
sf::RenderTarget& Question
« Reply #1 on: February 19, 2012, 02:59:38 pm »
Quote
fred.Draw();
The function Draw doesn't exist inside your Object class. Did you mean Display?

I have a different design for making an object, maybe u can do something similar (the code is for SFML2, so you will have to rewrite it a little bit).
Code: [Select]
struct Picture : public sf::Drawable, sf::Transformable
{
    bool Load(const std::string filename);
    void Draw(sf::RenderTarget& target, sf::RenderStates states) const;

    sf::Texture m_Texture;
    sf::Sprite m_Sprite;
};

bool Picture::Load(const std::string filename)
{
    if (m_Texture.LoadFromFile(filename))
    {
        m_Sprite.SetTexture(m_Texture);
        return true;
    }
    else
         return false;
}

void Picture::Draw(sf::RenderTarget& target, sf::RenderStates states) const
{
        states.Transform.Translate(GetPosition());
        states.Transform.Scale(GetScale());

        target.Draw(m_Sprite, states);
}


If you are going to use this code for SFML 1.6 then the second parameter of the Draw function will be different, forget about sf::Transformable and only use sf::Drawable and finally use sf::Image instead of sf::Texture.

The picture class is used like this:
Code: [Select]
Picture pic;
pic.Load("image.png");
pic.SetPosition(10, 20);
pic.SetScale(2, 2);
window.Draw(pic);
TGUI: C++ SFML GUI

ZeroApoc

  • Newbie
  • *
  • Posts: 6
    • View Profile
sf::RenderStates states
« Reply #2 on: February 19, 2012, 07:50:16 pm »
Thanks for your response.

How will sf::RenderStates states be different?

I know nothing about states...
states.Transform.Translate(GetPosition());
states.Transform.Scale(GetScale());
How does the above two lines work? Is it a SFML thing or just 2. or your own bag of tricks?

sf::Edit()   (<- :P)
{

Does this look like what you were talking about?

Code: [Select]
struct Picture : public sf::Drawable
{
    bool Load(const std::string filename);
    void Draw(sf::RenderTarget& target, /**/sf::RenderStates states/**/) const;

    sf::Image Image_m;
    sf::Sprite Sprite_m;
};

bool Picture::Load(const std::string filename)
{
    if (Image_m.LoadFromFile(filename))
    {
        Sprite_m.SetTexture(m_Texture);
        return true;
    }
    else
         return false;
}

void Picture::Draw(sf::RenderTarget& target, /**/sf::RenderStates states/**/) const
{
        states.Transform.Translate(GetPosition());
        states.Transform.Scale(GetScale());

        target.Draw(m_Sprite, states);
}

}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::RenderTarget& Question
« Reply #3 on: February 19, 2012, 08:07:04 pm »
Code: [Select]
states.Transform.Translate(GetPosition());
states.Transform.Scale(GetScale());

Why don't you do like every other transformable class?
Code: [Select]
states.Transform *= GetTransform();
Laurent Gomila - SFML developer

ZeroApoc

  • Newbie
  • *
  • Posts: 6
    • View Profile
Why don't you do like every other transformable class?
« Reply #4 on: February 19, 2012, 08:21:52 pm »
Quote
Why don't you do like every other transformable class?

Because I have no idea what a transformable class is. I am using SFML version 1.6.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::RenderTarget& Question
« Reply #5 on: February 19, 2012, 08:44:07 pm »
Quote
Because I have no idea what a transformable class is. I am using SFML version 1.6.

You didn't write this code, so I was obviously not talking to you :wink:
Laurent Gomila - SFML developer

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
sf::RenderTarget& Question
« Reply #6 on: February 19, 2012, 08:54:09 pm »
Quote from: "Laurent"
Code: [Select]
states.Transform.Translate(GetPosition());
states.Transform.Scale(GetScale());

Why don't you do like every other transformable class?
Code: [Select]
states.Transform *= GetTransform();

I didn't notice that, I don't look much in the sfml source code.
From now on I will do it like that.
TGUI: C++ SFML GUI

ZeroApoc

  • Newbie
  • *
  • Posts: 6
    • View Profile
obvious
« Reply #7 on: February 19, 2012, 08:55:20 pm »
So does that mean I am not getting an answer? I'll take an answer from anyone in the know.

What's the deal with this?:
states.Transform.Translate(GetPosition());
states.Transform.Scale(GetScale());

I have no idea what it is or what to do with it.

I am dying to learn something about SFML. You know...moving  sprite(check), firing a laser(check), time control(not done), Implementing SFML in my own class(Kind of check and kind of not done). SFML tutorials seem to be sparse. I was warned but at the ame time I was told SFML is C++ and not C. I would rather not waste my time mucking about with C.

Sorry about the post with dual personalities but that's what you get reading something wrote by someone with extreme ADHD.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::RenderTarget& Question
« Reply #8 on: February 19, 2012, 08:57:49 pm »
Quote
I didn't notice that, I don't look much in the sfml source code.

It is also explained in the doc of sf::Transformable.
Laurent Gomila - SFML developer

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
sf::RenderTarget& Question
« Reply #9 on: February 19, 2012, 09:01:31 pm »
Quote
void Object::Display()
{
    sf::Sprite::Draw();
}

I don't think this part is correct.

To draw you will need a window, so it should be something like this:
Quote
void Object::Display(sf::Window& window)
{
    window.Draw(sprite);
}
TGUI: C++ SFML GUI

ZeroApoc

  • Newbie
  • *
  • Posts: 6
    • View Profile
texus
« Reply #10 on: February 19, 2012, 09:15:51 pm »
Now that was what i was looking for. passing in the window like that should be simple to figure out. I would have after a couple days of tearing my hair out. Passing the window by reference was perfect.

Code: [Select]
Object fred;
fred.Create("imagefile.PNG");
fred.Display(window);


That was so simple...so simple I couldn't figure it out. Seriously...I was freakin' floundering over here. Thanks texus. I guess you could call me nebus. lol

ZeroApoc

  • Newbie
  • *
  • Posts: 6
    • View Profile
Oops
« Reply #11 on: February 19, 2012, 10:08:55 pm »
F:\CodeBlocks-Programs\SFML Tutorial 02\main.cpp||In function 'void GameLoop()':|
F:\CodeBlocks-Programs\SFML Tutorial 02\main.cpp|38|error: no matching function for call to 'sf::RenderWindow::Display(Object&)'|
F:\CodeBlocks-Programs\SFML Tutorial 02\main.cpp|38|note: candidate is:|
F:\Libraries\SFML-1.6-sdk-windows-mingw\SFML-1.6\include\SFML\Window\Window.hpp|249|note: void sf::Window::Display()|
F:\Libraries\SFML-1.6-sdk-windows-mingw\SFML-1.6\include\SFML\Window\Window.hpp|249|note:   candidate expects 0 arguments, 1 provided|
||=== Build finished: 4 errors, 0 warnings (0 minutes, 1 seconds) ===|

I tried it and this is what I got. Maybe window is not a class? What am I doing wrong? I wrote it verbatim.

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
sf::RenderTarget& Question
« Reply #12 on: February 19, 2012, 10:36:23 pm »
In your header you declared Display without parameters.
Quote
virtual void Display();


Of course this also has to change.
Code: [Select]
virtual void Display(sf::Window& window);
TGUI: C++ SFML GUI