I've been to figure out why this keeps breaking for the last 3 days. I'm using a std::map to store all textures in, hopefully, and then just have my link to the images from that map. This works fine on one image; I can load it, store it, and retrieve it to have an entity use it and finally, it gets drawn properly. The minute I add in another texture, however, my game crashes on trying to create an entity that refers to any/either of the textures - GDB has it crashing on a segfault inside the std::map header file, presumably when I'm trying to retrieve the texture.
Here's the code in question:
Game.cpp
Game::Game()
{
time(&gameTime);
timeInfo = localtime(&gameTime);
gameWindow = SDL_CreateWindow(globals::SCREENTITLE.c_str(),
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
globals::SCREENWIDTH, globals::SCREENHEIGHT, SDL_WINDOW_SHOWN);
gameRenderer = SDL_CreateRenderer(gameWindow, 0, SDL_RENDERER_ACCELERATED);
loadTexture("texture.png");
loadTexture("raindrop.png");
//gameVector.push_back(&(textureLibrary["raindrop.png"]));
Entity newEntity(textureLibrary["raindrop.png"]);
//Entity anotherEntity(&(textureLibrary["raindrop.png"]));
gameVec.push_back(&(newEntity));
//gameVec.push_back(&(anotherEntity));
running = true;
while(running)
{
handleInput(gameInput);
update();
draw();
SDL_Delay(16);
}
};
Game.cpp
void Game::loadTexture(std::string filename)
{
SDL_Texture* newTexture = NULL;
SDL_Surface* loadedSurface = IMG_Load(filename.c_str());
if(loadedSurface == NULL)
SDL_Quit();
/// Set the image invisibility color
SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format, 0xFF, 0, 0xFF));
newTexture = SDL_CreateTextureFromSurface(gameRenderer, loadedSurface);
SDL_FreeSurface(loadedSurface);
textureLibrary[filename.c_str()] = newTexture;
return;
};
And finally, my entity constructor - very basic.
Entity::Entity(SDL_Texture* itsTexture)
{
texture = itsTexture;
SDL_QueryTexture(itsTexture, NULL, NULL, &texRect->w, &texRect->h);
};
texture is the member of Entity, and is a simple SDL_Texture*.
Again; this system worked fine when I had only one image loaded - it wasnt until I had both ["raindrop.png"] and ["texture.png"] that it started segfaulting on the
Entity newEntity(textureLibrary["raindrop.png"]);
bit.
Any clues? Don't mind the terrible coding. This is me trying to get a game worked out in a 8wk challenge; and I'm sure it may be sloppy. :(