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

Author Topic: SFML Texture in diffrent class not drawing  (Read 6531 times)

0 Members and 2 Guests are viewing this topic.

ThunderAce

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
SFML Texture in diffrent class not drawing
« on: December 12, 2015, 08:43:23 pm »
I have a game class with a function to load my sprite into a stack and a function to draw the stack of sprites. I have set it up this way so when I have more sprites it is easier to program.

I also have a floor class that makes the sprite and the texture in the constructor, then loads it into my vector of sprites.

My problem is that my texture doesn't display on the screen, not even a white box. Is there an obvious mistake? Or am I just not understanding how textures work.

Floor.cpp
Floor::Floor()
{
        Game game;

        sf::Sprite floor;
        sf::Texture* pointerToFloorTexture = new (sf::Texture);

        pointerToFloorTexture->loadFromFile("floor.png");

        game.LoadSpriteIntoStack(floor, pointerToFloorTexture);
}
 

Game.cpp
void Game::LoadSpriteIntoStack(sf::Sprite sprite, sf::Texture *texture)
{
        sprite.setTexture(*texture);
        stackOfSprites.push_back(sprite);
}

void Game::DrawStackOfSprites(sf::RenderWindow& window)
{
        for (unsigned int i = 0; i < stackOfSprites.size(); i++)
        {
                window.draw(stackOfSprites[i]);
        }
}
 

main.cpp (just the loop)
        while (window.isOpen())
        {
                // Handle Events
                game.HandleEvents(window);

                window.clear();

                // Draw Sprites
                game.DrawStackOfSprites(window);

                window.display();
        }
 

Game.h
#pragma once

class Game
{
public:
        Game();
        // Handle Events
        void HandleEvents(sf::RenderWindow& window);

        void LoadSpriteIntoStack(sf::Sprite sprite, sf::Texture *texture);
        void DrawStackOfSprites(sf::RenderWindow& window);

private:
        std::vector<sf::Sprite> stackOfSprites;
};
 

noct

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • nctdev.pl
Re: SFML Texture in diffrent class not drawing
« Reply #1 on: December 12, 2015, 09:12:22 pm »
I think you have to pass a reference to Texture instead of pointer, or it won't be working correct.

ThunderAce

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: SFML Texture in diffrent class not drawing
« Reply #2 on: December 12, 2015, 09:24:02 pm »
Any chance you can give me an example or something? I don't quite understand how to do that

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: SFML Texture in diffrent class not drawing
« Reply #3 on: December 12, 2015, 10:05:07 pm »
There is no indication as to how the floor class is actually used. Where is the floor object created, for example?

You should be testing the result of "loadFromFile()" to see if there are any errors. It could be failing to find the file.

I think you have to pass a reference to Texture instead of pointer, or it won't be working correct.
sprite.setTexture(*texture);
*texture here is not a pointer. texture is a pointer and *texture is the deferenced pointer (the actual object i.e. the texture)

Comments:
There are better options than using raw pointers with "new". Using objects on the stack can work just fine without the need to use the heap. However, if you need to use the heap, consider reading this short article. That said, if you store your resources in an std::vector (or similar), they are automatically stored in the heap/dynamic memory.

You don't seem to need the sprite parameter in LoadSpriteIntoStack() as it's an empty sprite and it creates a new copy anyway. Just declare a new sprite in the function (or push/emplace one directly to the vector and set the texture afterwards).

If you're using C++11 or later (and why wouldn't you be doing so?), the loop to draw the sprite stack can be simplified:
for (auto& sprite : stackOfSprites)
    window.draw(sprite);
(although you can add braces around that one statement if you prefer)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

ThunderAce

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: SFML Texture in diffrent class not drawing
« Reply #4 on: December 12, 2015, 10:31:19 pm »
Okay I scrapped the floor class, as I don't understand how I was meant to use it. Now I have the same problem in the player class:

Player.cpp
void Player::CreatePlayer()
{
        Game game;

        sf::Sprite playerSprite;
        sf::Texture playerTexture;

        playerTexture.loadFromFile("images/player.png");

        game.LoadSpriteIntoStack(playerSprite, playerTexture);
}

main.cpp
player.CreatePlayer();

        while (window.isOpen())
        {
                // Handle Events
                game.HandleEvents(window);

                window.clear(sf::Color(sf::Color(0,0,0,0)));

                // Draw Sprites
                game.DrawStackOfSprites(window);

                window.display();

        }

Game.cpp
void Game::LoadSpriteIntoStack(sf::Sprite sprite, sf::Texture &texture)
{
        sprite.setTexture(texture);
        stackOfSprites.push_back(sprite);
}

void Game::DrawStackOfSprites(sf::RenderWindow& window)
{
        for (auto& sprite : stackOfSprites)
                window.draw(sprite);
}
 

How would I fix this?

EDIT: I tested the loadFromFile and it works perfectly ;)
« Last Edit: December 12, 2015, 10:34:00 pm by ThunderAce »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: SFML Texture in diffrent class not drawing
« Reply #5 on: December 12, 2015, 10:39:39 pm »
Your CreatePlayer() function is creating a new Game object and loading the sprites into that game object instead of the one scoped (presumably) to the main function.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

ThunderAce

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: SFML Texture in diffrent class not drawing
« Reply #6 on: December 12, 2015, 11:18:34 pm »
main.cpp
        sf::Texture playerTexture;

        game.LoadSpriteIntoStack(player.CreatePlayer(playerTexture));

        while (window.isOpen())
        {
                // Handle Events
                game.HandleEvents(window);

                window.clear(sf::Color(sf::Color(0,0,0,0)));

                // Draw Sprites
                game.DrawStackOfSprites(window);

                window.display();

        }

Player.cpp
sf::Sprite Player::CreatePlayer(sf::Texture texture)
{
        texture.loadFromFile("images/player.png");
        sf::Sprite playerSprite(texture);
        return playerSprite;
}

I've made a few changes, to get rid of that issue. It now draws a white box the size of the texture but not the actual texture

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: SFML Texture in diffrent class not drawing
« Reply #7 on: December 12, 2015, 11:41:53 pm »
sf::Sprite Player::CreatePlayer(sf::Texture texture)
Passing the texture by value creates a new local copy of it. The function then points the sprite at it and the texture is then destroyed and thus The White Square Problem.
passing the texture by reference should alleviate that problem:
sf::Sprite Player::CreatePlayer(sf::Texture& texture)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

ThunderAce

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: SFML Texture in diffrent class not drawing
« Reply #8 on: December 12, 2015, 11:50:55 pm »
Okay, I just figured that out just before i read your message. Was experimenting and I figured it out. Thanks for all your help :)

 

anything