Ok I'll admit, this is my mess up. I even know whats wrong, I just don't know how to fix it. ^^;
So I have a little class *actually two classes* that I made to handle sprite/texture stuff to lower down the amount of re used code in other classes. What they do is very easy to figure out. If you want a sprite, you use get sprite, send it the path, it uses texture to retrieve the texture and such. But I'm trying to 1, figure out why its not working, and 2, trying to make a decision as to how to return the variables.
Option a, would be to use references *what I'm trying currently* but then that makes me wonder if it would conflict with other instances of the same class. *if two things tried to get a sprite at once*
Option b, Or if it would be better to just make a new copy of the variable every time. *would use more memory and such*.
As for the code here it is:
#include "stdafx.h"
#include "SpriteUtilities.h"
sf::Sprite& Sprite::getSprite(std::string path)
{
Texture texture;
sf::Sprite s;
s.setTexture(texture.getTexture(path));
return s;
}
sf::Texture& Texture::getTexture(std::string path)
{
sf::Texture t;
log.info("Attempting to load texture at: " + path);
if(!t.loadFromFile(path))
{
log.error("Unable to load texture from path: " + path);
log.error("Texture errors detected, graphic failure may occur!");
return t;
}
return t;
}
The problem is that since the variable is declared in a outside class/function, the moment it returns the reference the memory allocation is cleared and it returns a blank sprite. I just don't really remember how to fix it. >.>'
So any help, and recommendations as to which way would be best to go, is greatly appreciated!
I did indeed try pointers but they didn't just say error, they screamed it, all over my build log.
So I decided to try references. ^^;;
Huge thanks in advance and as always, if you wish to know more just ask!