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.cpp
int 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!? :D Anyone has any ideas?
Hey Binary1248!
Thank you so much for the reply :) I'm quite new to C++ but attempting to learn by simply throwing myself out in a project. Thank you, I did not realize that the syntax I used actually caused any copying! So what you are saying is that when you use syntax such as:
WorldEntity rainCloud = WorldEntity(...)
It will actually construct a worldEntity object and then copy it to the "rainCloud" variable due to the assignment operator? So if I instead use:
WorldEntity rainCloud(...)
Then I construct it "in place" for the rainCloud object? Which will not cause any copying.
Thank you for your time!
Yes -- however, compilers are allowed to optimize this copy away.
Still, there's no need to name the type twice in a declaration.
WorldEntity rainCloud; // default constructor
WorldEntity rainCloud(...); // parameter constructor
As a general advice, don't store resources (textures, sound buffers, fonts etc.) along with the game objects that use them. This will only cause trouble. Instead, think about storing resources in a centralized place.