SFML community forums

Help => General => Topic started by: eternityq on May 03, 2014, 07:56:24 pm

Title: Problem while trying to draw sprite from another class [CLOSED]
Post by: eternityq on May 03, 2014, 07:56:24 pm
Hello there!

I'm trying to draw an entity using inheritance from sf::Drawable like in "Designing your own entities with vertex arrays" tutorial (http://www.sfml-dev.org/tutorials/2.1/graphics-vertex-array.php (http://www.sfml-dev.org/tutorials/2.1/graphics-vertex-array.php)).

But as the screen loads I can only see a white rectangle of my sprite, not the actual sprite.
If I load the same texture and assign it to the sprite from Game.cpp the sprite loads properly.

So why isn't the sprite from Player class working properly?

The entity I'm trying to draw:
#include "Player.h"

#include <SFML/Graphics.hpp>
#include <ostream>

const int STARTING_HEALTH = 100;

Player::Player()
{
        x=y=20;
        dx=dy=1;
        health = STARTING_HEALTH;
        sf::Texture texture;
        texture.loadFromFile("resources/player.jpeg", sf::IntRect(30, 145, 82, 125));
        m_sprite.setTexture(texture);
}

void Player::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
        target.draw(m_sprite);
}

It's header:
Entity is inherited from sf::Drawable

#ifndef PLAYER_H
#define PLAYER_H

#include <SFML/Graphics.hpp>
#include "Entity.h"

class Player : public Entity
{
public:
        Player();
        virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
};

#endif // PLAYER_H
 

And the main game entry:
Game.cpp
#include <SFML/Graphics.hpp>
#include "Player.h"

int main()
{
    sf::RenderWindow window(sf::VideoMode(1000, 800), "Game Window");
       
        Player player;

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear();
                window.draw(player);
        window.display();
    }

    return 0;
}
Title: Re: Problem while trying to draw an object using inheritance from sf::Drawable
Post by: FRex on May 03, 2014, 08:06:52 pm
Try and remove the () from this line: Player player();
Title: Re: Problem while trying to draw an object using inheritance from sf::Drawable
Post by: eternityq on May 03, 2014, 08:18:25 pm
Try and remove the () from this line: Player player();
Hm, now it's error C2243: 'type cast' : conversion from 'Player *' to 'const sf::Drawable &' exists, but is inaccessible
Title: Re: Problem while trying to draw an object using inheritance from sf::Drawable
Post by: eternityq on May 03, 2014, 08:28:07 pm
Oh, well. Seems like I forgot to set inheritance from sf::Drawable as public. So that's the issue.
Title: Re: Problem while trying to draw object using inheritance from sf::Drawable [CLOSED]
Post by: Ixrec on May 03, 2014, 08:32:43 pm
By the way, if you want an explanation for the first error you were getting, look up the "Most Vexing Parse".
Title: Re: Problem while trying to draw object using inheritance from sf::Drawable [CLOSED]
Post by: eternityq on May 03, 2014, 08:36:07 pm
By the way, if you want an explanation for the first error you were getting, look up the "Most Vexing Parse".

Thanks! I'll do that!
Title: Re: Problem while trying to draw object using inheritance from sf::Drawable
Post by: eternityq on May 03, 2014, 08:58:39 pm
Damn. Well, now it works, but as I compile the program and start it, I can only see white rectangle on the gamescreen. And if I'm loading the same texture and assigning it to the sprite from Game.cpp and load this one it works.
So why doesn't the Player's sprite work properly?
Title: Re: Problem while trying to draw sprite from another class
Post by: Ixrec on May 03, 2014, 09:07:50 pm
Read the official tutorial on sprites and textures.  This is the "white square problem", which is what happens when your texture gets destroyed before you draw the sprite.
Title: Re: Problem while trying to draw sprite from another class
Post by: eternityq on May 03, 2014, 09:10:16 pm
Read the official tutorial on sprites and textures.  You're experiencing the "white square problem".

Ah. Okay. Thanks a lot again, didn't get to the end of the tutorial :(