Hello everyone, I've a really weird problem thats driving me crazy, I just cant see whats wrong at all.
Basically, I have a texture loader class, which keeps a reference count of how many sprites are using each texture. When a sprite is created, it passes a filename to this class. If that file has already been loaded, then the texture manager just passes back a pointer to the sprite, otherwise it loads the file, and passes back a pointer (when I say pointer, I mean SDL_Surface* by the way).
My problem is that somewhere between loading a file, and passing the pointer back to my sprite, the image gets corrupted, and wont draw correctly, or at all.
I'm going to post some code, hope it isnt too long, it should be easy to understand though (I hope).
Ok, firstly, this class holds the data for each texture that is stored in my resource manager class. The resource manager keeps a std::vector of these Texture objects (ResourceManager::textures).
// texture data object
class Texture
{
public:
int references; // reference count
string filename; // textures filename
SDL_Surface* image; // texture surface
// constructor
Texture()
{
references = 0;
image = NULL;
}
// destructor
~Texture()
{
if(image)
SDL_FreeSurface(image);
image = NULL;
}
};
Now for the resource manager class. This is the function that loads textures into memory. It will only load a texture if it hasn't already been loaded in, and will return the surface* to that texture.
// load a texture
SDL_Surface* ResourceManager::LoadTexture(string filename)
{
// first, check if file is already in the manager list
for(int i = 0; i < (int)textures.size(); i++)
{
// if already present, just increment references, and return the pointer
if(textures.filename == filename)
{
textures.references++;
return(textures.image);
}
}
// otherwise, load the file
Texture temp;
temp.filename = filename;
temp.image = SDL_LoadBMP(filename);
temp.references++;
if(!temp.image)
{
printf("--- error --- loading texture file %s\n", filename);
return(NULL);
}
// still have a new texture, push onto vector
textures.push_back(temp);
return(temp.image);
}
As far as I'm aware, this code should work. The problem is when I'm trying to use the returned pointer from LoadTexture. In other SDL programs I have simply done
SDL_Surface* ptr = someOtherPtr;
and after that I was able to blit ptr to the screen without any problems. This time however, something is going badly wrong.
In my sprite class, after the resource manager is initialised obvoiusly, I call LoadTexture, and store the resulting surface pointer into sprite::image.
image = rManager->LoadTexture(imageName);
after which I attempt to blit, however, I either get a program crash, a black rectangle where the image should be, or just a blank screen. If I load the file using SDL_LoadBMP directly in my blitting function, it works, so it is not a file problem or anything. Can anyone see why this might be returning an invalid surface? If I check the data inside the returned surface it is correct, the only thing that is different, if the program actually runs, is that the pixel data is NULL for some reason.
I know this has been a loooong post, and I'm sorry, but I really dont know what else to do, hopefully somebody can point out something. I'm tempted scrap this whole engine im so frustrated.