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

Author Topic: [SOLVED] SFML 2: Function return "sf::Sprite" Value  (Read 4313 times)

0 Members and 1 Guest are viewing this topic.

Unistyle

  • Newbie
  • *
  • Posts: 3
    • View Profile
[SOLVED] SFML 2: Function return "sf::Sprite" Value
« on: July 25, 2012, 07:00:07 pm »
Hello,

I'm new by using SFML and already searched alot about how to use it.
I stucked by trying to make a function in a class.
Actually it works, ... but it seems to have problems with the return value.

I don't really program long time with c++, so dont slap me when some things could be
very wrong :D
The function "create()" in the class "doSprite" should shorten some things in the "main.cpp"
when i wanna make a sprite. The class isnt finished, but it is ready to test if it works.

Here the code:

doSprite.hpp
#include <SFML/System.hpp>

class doSprite
{
    private:
    public:

        //Member Variablen
        sf::String directory;

        //Position
        int xpos;
        int ypos;

        //Farbe
        int c_red;
        int c_green;
        int c_blue;
        int c_trans;

        //Funktionen
        int create();

        //Konstruktor
        doSprite();

        //Destruktor
        ~doSprite();
};
 

doSprite.cpp
#include "doSprite.hpp"

int doSprite::create(sf::String directory, int xpos, int ypos, int c_red, int c_green, int c_blue, int c_trans)
{
    sf::Texture texturName;
    if(!tx_player.loadFromFile(directory))
        return 1;

    sf::Sprite sprSprite;
    spr_player.setTexture(sprSprite);
    spr_player.setPosition(xpos,ypos);
    spr_player.setColor(sf::Color(c_red,c_green,c_blue,c_trans));

    return sprSprite;
}

//Konstruktor
doSprite::doSprite()
{
    //Member Variablen
    directory = "Sprites/Player/2.png";

    //Position
    xpos = 0;
    ypos = 0;

    //Farbe
    c_red = 255;
    c_green = 255;
    c_blue = 255;
    c_trans = 255;
}

//Destruktor
doSprite::~doSprite()
{

}
 

main.cpp
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include "doSprite.hpp"

int main()
{
    sf::VideoMode VMode = sf::VideoMode::getDesktopMode();
    sf::RenderWindow window(VMode, "SFML works!",sf::Style::Default);

    /* Test Function in class instead of this piece of code :/
    sf::Texture tx_player;
    if(!tx_player.loadFromFile("Sprites/Player/2.png"))
        return 1;

    sf::Sprite spr_player;
    spr_player.setTexture(tx_player);
    spr_player.setPosition(100,100);
    spr_player.setColor(sf::Color(255,255,255,255));
    */

    doSprite dS;
    sf::Sprite spr_player = dS.create("Sprites/Player/2.png", 200, 200, 255, 255, 255, 200);

    // Das Programm läuft solange das Fenster offen ist
    while (window.isOpen())
    {
        // Prüft alle Events, bis alle abgearbeitet sind
        sf::Event event;
        while (window.pollEvent(event))
        {
            // Mit dem "close requested" Event wird das Fenster geschlossen
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear(sf::Color(0,255,255));
        window.draw(spr_player);
        window.display();
    }

    return 0;
}
 

Build Log:
...\main.cpp:20: error: 'doSprite' was not declared in this scope
...\main.cpp:35: error: 'spr_player' was not declared in this scope
 

So the problem is, i dont really know how to return "sprSprite" from the "doSprite.cpp" to "main.cpp".
I hope you can help me :)

Edit: Added Build log under code.



~Unistyle~
« Last Edit: August 05, 2012, 05:49:32 pm by Unistyle »

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: SFML 2: Function return "sf::Sprite" Value
« Reply #1 on: July 25, 2012, 08:56:57 pm »
Quote
I don't really program long time with c++, so dont slap me when some things could be
very wrong
Ok, so I don't slap you but the code is a mess.  :P

First, in RGB, «G» doesn't stand for «Gelb» but for «Green»; i.e. not yellow.  ;)

Next, on the programming level : don't take it too badly but there is a lot of errors. I can't go through all of them now.

I just pick one error that is not really related to Object Oriented Programming :

void doSprite::create(sf::String directory, int xpos, ypos, c_red, c_yellow, c_blue, c_trans)
{
    /* snip */
    return sprSprite;
}


In fact, there is two huge errors in these two lines of code :
  • ypos, c_red, ... all need a type. like this for example ...create(sf::String directory, int xpos, int ypos, int c_red, int c_yellow, int c_blue, int c_trans)...
  • your function's return type is void but you try to return something.

You should probably start some good books (e.g. Accelerated C++) or online tutorials to get some strong basis before going on.
SFML / OS X developer

Acrobat

  • Full Member
  • ***
  • Posts: 153
    • View Profile
Re: SFML 2: Function return "sf::Sprite" Value
« Reply #2 on: July 25, 2012, 09:27:17 pm »
You forgot to include your header in main, also you have errors there.

Unistyle

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: SFML 2: Function return "sf::Sprite" Value
« Reply #3 on: July 25, 2012, 10:59:39 pm »
Thank you for your answer [=
And I'm really sorry for these mistakes ...
I corrected the code in the first post as much as i could.

Quote
You should probably start some good books (e.g. Accelerated C++) or online tutorials to get some strong basis before going on.

I watched Videos to learn how to use OOP in a console. I already learned C++ many times in the past (console over and over again after some breaks :D) This time i directly tried to use it with SFML. Maybe i watch a whole Totorial series the next days. In any case i buy a book :p

Quote
First, in RGB, «G» doesn't stand for «Gelb» but for «Green»; i.e. not yellow.  ;)
I searched for it 1000 times in my life, but this time i didnt searched and write it wrong.
I feel bad^^

Quote
ypos, c_red, ... all need a type
I don't believe i forgot that ... i have to make more breaks and look more often where mistakes are before I open a new Thread.

Quote
your function's return type is void but you try to return something.
Thats the point where i dont know what type i should use. I know how to return ONE type.
But sf::Sprite has not just one type implemented or? I read that you can just return one type,
so is it possible to make my function working? Or do i have to work it another way?

Quote
You forgot to include your header in main, also you have errors there.
Oh i forgot to include it  :-[ Thank you!


~Unistyle~

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: SFML 2: Function return "sf::Sprite" Value
« Reply #4 on: July 29, 2012, 03:15:16 pm »
I'm in vacation right now so I answer in a very concise manner :

Quote
But sf::Sprite has not just one type implemented or? I read that you can just return one type,
so is it possible to make my function working? Or do i have to work it another way?
You definitely have some issue with classes/objects. sf::Sprite is a class and any class is ONE type. So you can return an object of type sf::Sprite like you would do with a std::string or an int.

SFML / OS X developer

Unistyle

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: SFML 2: Function return "sf::Sprite" Value
« Reply #5 on: August 05, 2012, 05:47:47 pm »
Quote
You definitely have some issue with classes/objects. sf::Sprite is a class and any class is ONE type. So you can return an object of type sf::Sprite like you would do with a std::string or an int.

Yes, thanks for your answer :)
I just started too fast after my break with C++.
I began to learn all basics again, and now i dont have problems anymore with it :)

 

anything