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

Author Topic: Problem while trying to draw sprite from another class [CLOSED]  (Read 3411 times)

0 Members and 3 Guests are viewing this topic.

eternityq

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
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).

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;
}
« Last Edit: May 03, 2014, 09:10:26 pm by eternityq »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Try and remove the () from this line: Player player();
Back to C++ gamedev with SFML in May 2023

eternityq

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
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
« Last Edit: May 03, 2014, 08:22:14 pm by eternityq »

eternityq

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Oh, well. Seems like I forgot to set inheritance from sf::Drawable as public. So that's the issue.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
By the way, if you want an explanation for the first error you were getting, look up the "Most Vexing Parse".

eternityq

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
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!

eternityq

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
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?

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Problem while trying to draw sprite from another class
« Reply #7 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.
« Last Edit: May 03, 2014, 09:10:26 pm by Ixrec »

eternityq

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Problem while trying to draw sprite from another class
« Reply #8 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 :(