SFML community forums

Help => Window => Topic started by: Clockwork on July 18, 2013, 02:00:56 am

Title: Render Function in the Player Class
Post by: Clockwork on July 18, 2013, 02:00:56 am
Hello everybody,

So I had a working program before but it only consisted of a header file and a cpp file.  It basically created a player sprite that moved using the arrow keys.

But now I'm splitting things up into different classes and I've encountered a problem.  In my game loop I can call:
window.draw(playerImage);
but I can't do that in the player class (at least I don't know how to).

What's giving me trouble is that I can't access the window variable from another class. I've tried adding this in my setup class and calling it from the player class, but it doesn't work:
//Setup class:
void CSetup::Render(sf::Texture texture, sf::Sprite sprite, std::string path, float x, float y)
{
        if (!texture.loadFromFile(path))
                std::cout << "Could not find " << path << std::endl;

        sprite.setTexture(texture);
        sprite.setPosition(x, y);

        window.draw(sprite);
}

//Player class
void CPlayer::RenderPlayer(void)
{
        setup -> Render(pTexture, pImage, "player.png", 384, 284);
}

When I try to do this, the screen just appears white and I can't exit the window except by closing the debug console.

How can I draw a Sprite from the player class?

Thanks for your time!
Title: Re: Render Function in the Player Class
Post by: The Hatchet on July 18, 2013, 02:11:49 am
You can either pass the window via pointer/reference to the player class to use to draw OR you can make a getSprite() function that returns a reference/pointer of the player's sprite to whatever is calling it so your main game loop can still do the render.
Title: Re: Render Function in the Player Class
Post by: Clockwork on July 18, 2013, 05:50:57 am
Hmm, I tried this:
Player.cpp :
sf::Sprite* CPlayer::GetPlayer()
{
        if (!pTexture.loadFromFile("player.png"))
                std::cout << "Could not load player image" << std::endl;

        pImage.setTexture(pTexture);
        pImage.setPosition(384, 284);
        pImage.setTextureRect(sf::IntRect(source.x * 32, source.y * 32, 32, 32));

        return &pImage;
}

Setup.cpp:
window.draw(player -> GetPlayer());

But there's an error (red line underneath the . ) that says: "No instance of overloaded function 'sf::RenderWindow draw' matches the argument list argument types are: (sf::Sprite*) object type is sf::RenderWindow."

How can I fix this?
Title: Re: Render Function in the Player Class
Post by: zsbzsb on July 18, 2013, 01:22:09 pm
But there's an error (red line underneath the . ) that says: "No instance of overloaded function 'sf::RenderWindow draw' matches the argument list argument types are: (sf::Sprite*) object type is sf::RenderWindow."

It seems you are missing a basic fundamental understanding of how C++ pointers/references work. I highly suggest you pickup a good C++ book and learn C++ before trying to learn SFML. While it is possibly to learn C++ from online tutorials it is not a good idea since many leave out important information and/or teach questionable coding habits.

Now to your problem you need to deference your pointer (hint *) before passing your sprite as a reference to SFML. Another thing that is wrong with your code is that you shouldn't be loading the texture in your drawing code. You should load the texture once in your constructor and then keep reusing it.
Title: Re: Render Function in the Player Class
Post by: Clockwork on July 19, 2013, 10:14:55 pm
Actually, I made this  post on stackoverflow (gamedev) that has to do with this.  I followed another tutorial, but it's not working.  Could you guys look at it please?  I'm not getting much luck there, but I don't really want to make another post here, because it's pretty similar.

Link: http://gamedev.stackexchange.com/questions/59404/sfml-renderwindow-unhandled-exception

Thanks!
Title: Re: Render Function in the Player Class
Post by: Nexus on July 19, 2013, 10:21:31 pm
Don't use SFML objects as global variables, you provoke problems with OpenGL context initialization/destruction. Global variables are generally a bad idea, and with a clean design there is almost never a need for them.

And please forget third-party tutorials, especially video tutorials. A lot of them teach questionable techniques and bad code style. Stick to the official tutorials and the API documentation, and ask if things are unclear.