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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - ThunderAce

Pages: [1]
1
General / Re: SFML Texture in diffrent class not drawing
« 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 :)

2
General / Re: SFML Texture in diffrent class not drawing
« 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

3
General / Re: SFML Texture in diffrent class not drawing
« 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 ;)

4
General / Re: SFML Texture in diffrent class not drawing
« 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

5
General / 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;
};
 

Pages: [1]