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 (unsignedint i = 0; i < stackOfSprites.size(); i++)
{
window.draw(stackOfSprites[i]);
}
}
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.
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
}
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.
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?
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?