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!
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?