Hey everyone
So, I've come across a very weird type of behaviour that has left me stumped.
First of all, I have successfully managed to draw a rain cloud picture using this code:
main.cppint main()
{
RenderWindow window(VideoMode(800, 500), "Rain Cloud");
WorldEntity rainCloud = WorldEntity();
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
if (event.type == Event::Closed)
{
window.close();
}
}
window.clear();
window.draw(rainCloud.getSprite());
window.display();
}
return 0;
}
And here's my simple
WorldEntity class:
/*
WorldEntity is a collection of basic properties needed by all objects that needs to exist within the game world
*/
class WorldEntity
{
private:
Sprite m_sprite;
Vector2f m_position;
Texture mahTexture;
public:
WorldEntity::WorldEntity()
{
mahTexture.loadFromFile("RainCloud.png");
m_sprite = Sprite(mahTexture);
m_position = Vector2f(0, 0);
}
void setPosition(float x, float y);
void setPosition(Vector2f newPosition);
Sprite getSprite();
};
#endif
Now, this code works perfectly and the rain cloud sprite is drawn. However... if I do the following change to the WorldEntity class:
WorldEntity::WorldEntity(Texture test)
{
mahTexture.loadFromFile("RainCloud.png");
m_sprite = Sprite(mahTexture);
m_position = Vector2f(0, 0);
}
That is, I now pass a texture called "test" by value (Notice I do NOT use it for anything at all!). And then change the first part of the main.cpp source code to pass a texture instead:
Texture texture_rainCloud;
if (!texture_rainCloud.loadFromFile("TheActual.png"))
cout << "Dummy textureTexture failed to load" << endl;
WorldEntity rainCloud = WorldEntity(texture_rainCloud);
It does
not work anymore. That is, now I get a white rectangle drawn instead of the cloud! Notice that all I did was simply pass a texture into the WorldEntity class that I do not even use for anything at all. I changed literally nothing but the fact that a texture is now passed to the constructor, and I don't use that variable for anything.
What exactly is going on there!?
Anyone has any ideas?