SFML sprites from diffrent classes

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.

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
1
2
3
4
5
6
7
8
9
10
11
Floor::Floor()
{
	Game game;

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

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

	game.LoadSpriteIntoStack(floor, pointerToFloorTexture);
}


Game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
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)
1
2
3
4
5
6
7
8
9
10
11
12
	while (window.isOpen())
	{
		// Handle Events
		game.HandleEvents(window);

		window.clear();

		// Draw Sprites
		game.DrawStackOfSprites(window);

		window.display();
	}


Game.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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;
};
Last edited on
Well.... Did you give the sprite a position and a size?
In your Floor constructor, game is a variable that is local to the constructor and stops existing when the constructor returns.
As far as I am aware I don't need to give it a size and position for it to show up

So where could I put my variable instead?
http://www.sfml-dev.org/documentation/2.0/classsf_1_1Sprite.php#a92559fbca895a96758abf5eabab96984


When doing this, you want these things to exist until you get rid of them.
In my project, I use a vector of pointers to my Item class and push those in as I dynamically create them. I've got the pointer stored now, so I won't lose it once the function returns.
I'll delete them from the vector when I no longer need them.
Here's an example of how I do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
    Holder *holder = new Holder; //This holds all my items

    MyItem *item = new MyItem(blah blah);
    ...
    holder->addNewItem(item);
    ....
}

void Holder::addNewItem(MyItem *element)
{
	m_elements->push_back(element);
	m_numberOfElements++;
	return;
}


You just have to be careful with allocations. Delete the allocated things inside the vector before the vector goes out of scope or you'll get a memory leak.
Sorry for late reply, been in school all day, thanks for helping :) but I still don't understand mate. How is my code not working when I am adding my sprite pointer to a stack just like you did?

1
2
3
4
5
6
7
8
9
10
11
Floor::Floor()
{
	Game game;

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

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

	game.LoadSpriteIntoStack(floor, pointerToFloorTexture); // I don't understand how this isn't working
}


1
2
3
4
5
void Game::LoadSpriteIntoStack(sf::Sprite sprite, sf::Texture *texture)
{
	sprite.setTexture(*texture);
	stackOfSprites.push_back(sprite);
}
Last edited on
Again, game is a local variable that ceases to exist when the Floor constructor returns.

It probably has the same name as a variable you are using elsewhere... but it is not the same variable.
Last edited on
Like cire said, it is a local variable. It goes out of scope once the Floor constructor returns. Put a print statement in the descructor and you should see it print when the Floor constructor returns.

http://www.learncpp.com/cpp-tutorial/4-1a-local-variables-and-local-scope/
I think I get it, so me creating my game object inside the constructor is a bad idea. Instead I create a pointer to the game object.

I changed my code so It creates a pointer to the game object instead of Game game; it says Game* game = new Game; but it still doesn't work. Am i missing the point or something here?
Why create a new 'game' inside of there? The game you created on the heap will still be there, but the pointer will go out of scope and will be destroyed, but the memory on the heap won't be de-allocated. You just created a memory leak (which I'm actually battling with myself right now).

It sounds like you need to create these items and add them to a 'game', but you only want one instance of 'game', right? You want them to all by pushed into the same vector?
Yes that is exactly what I want it to do mate :) So how would I do that?
Rather than having Floor create a new game inside of its constructor, how about you create a 'game' object in main that has a method that will take a pointer to Floor object and push it back into it's own vector.
Your object know about 'game', when it sounds like 'game' should know about your objects.
This method you speak about and pushing it back to it's own vector I don't quite understand. How would I do it, normally to do something like that I would pass the pointer through parameters. However, it is a constructor so how would I do that?
Problem Solved, thanks for the help guys
Topic archived. No new replies allowed.