0
votes

I recently started working with sfml and I cannot solve this problem. I have two classes which should work and display my sprite but nothing shows on the screen. I have tried a few things but none of them have worked so far, that's why I've decided to ask here :/

Thanks for any of your help, tips will also be appreciated ;)

Main.cpp:

#include <SFML\Graphics.hpp>
#include "Player.hpp"

sf::RenderWindow frame;
sf::Texture player_texture;
Player player(player_texture, 100, 100);
bool quit;

bool handle_events() {
    sf::Event event;
    if (frame.pollEvent(event)) {
        if (event.type == sf::Event::Closed) {
            return true;
        }
    }
    return false;
}

void update() {

}

void render() {
    frame.clear(sf::Color(127, 142, 123));
    player.draw_player(frame);
    frame.display();
}

void run() {
    while (quit != true) {
        quit = handle_events();
        update();
        render();
    }
}

int main() {
    player_texture.loadFromFile("player.png");
    frame.create(sf::VideoMode(800, 600), "frame");

    run();

    return 0;
}

Player.cpp:

#include "Player.hpp"

Player::Player(sf::Texture & player_texture, int pos_x, int pos_y) {
    player_sprite.setTexture(player_texture);
    player_sprite.setPosition(pos_x, pos_y);
    player_sprite.scale(4, 4);
}

void Player::draw_player(sf::RenderWindow & frame) {
    frame.draw(player_sprite);
}

Player.hpp:

#ifndef Player_hpp
#define Player_hpp

#include <SFML\Graphics.hpp>
#include <iostream>

class Player
{
private:
    sf::Sprite player_sprite;
public:
    Player::Player(sf::Texture & player_texture, int pos_x, int pos_y);
    void Player::draw_player(sf::RenderWindow & frame);
};

#endif
3
Where is a Player created? Is in its constructor where you give value to its texture, and i don't see where it is called.alseether

3 Answers

1
votes

Your player sprite is initialized with an empty texture. Then you load another picture into the texture and the player sprite does not know this. All the calculations the sprite made on the texture (for example size) are now invalid.

Make sure you load the texture before you pass it to the sprite. And don't change it afterwards.

0
votes

nvoigt is completely right.

You set the the texture of the sprite first (in the player constructor) and load it afterwards (in the main function):

...
player_sprite.setTexture(player_texture);
...
player_texture.loadFromFile("player.png");
...

You either have to reload the texture again with .setTexture inside the main function after the loading. Or you have to complete restructure you code.

By the way, this if (frame.pollEvent(event)) is not a good idea. There could have been multiple events triggered in on frame, for example mouse movement and window close after that. With the if you would only handle the first event in this frame, which is the mouse movement, even if you were looking for the second event. So you should do it with a while (while (frame.pollEvent(event))) to make sure that all the events are being handled.

0
votes

Are you sure the texture is loaded correctly? Check the return value of sf::Texture::loadFromFile to test.

More over, why your class Player does not extends from sf::Sprite? I think inheritance would be more appropriated that composition in this case.

Also, in your function handle_events, you can directly call the method RenderWindow::close when the user wants to close the window. Then, in your function run, call RenderWindow::isOpen to check if your app can continue. It's would be less dirty than this ugly not initialised quit variable.