I am creating a custom class, called TexturedSprite, to ease the process of loading textures/ setting a sprite's texture/ drawing the sprite. It basically holds a Texture pointer and sprite and autmatically takes care of the relations between them. The problem is when I try to load a file into the texture.
Here are the definitions of my loadFromFile function and constructor that takes a string parameter:
bool TexturedSprite::loadFromFile(const std::string& filename, const sf::IntRect& area) { return m_texture->loadFromFile(filename, area); m_sprite.setTexture(*m_texture); }
TexturedSprite::TexturedSprite(const std::string& filename)
{
//Load the texture
if(!m_texture->loadFromFile(filename))
std::cout << "Failed to load file for textured sprite: " << filename << std::endl;
m_sprite.setTexture(*m_texture); //Set the sprite to the texture
}
I ran my program in gdb and the call stack says the problem is at sf::Texture::create, specifically line 125:
m_size.x = width;
Here is my call stack:
#0 68ED9585 sf::Texture::create(this=0xbaadf00dbaadf00d, width=950, height=600) (C:\SFML-2.1\src\SFML\Graphics\Texture.cpp:125)
#1 68ED9A2A sf::Texture::loadFromImage(this=0xbaadf00dbaadf00d, image=..., area=...) (C:\SFML-2.1\src\SFML\Graphics\Texture.cpp:192)
#2 68ED97F7 sf::Texture::loadFromFile(this=0xbaadf00dbaadf00d, filename=..., area=...) (C:\SFML-2.1\src\SFML\Graphics\Texture.cpp:160)
#3 00402349 whack::TexturedSprite::TexturedSprite(this=0x6f7708, filename=...) (C:\Users\Main\Documents\Programs\WhackGame\src\TexturedSprite.cpp:12)
#4 00401A73 whack::IntroState::IntroState(this=0x6f76f0, window=...) (C:\Users\Main\Documents\Programs\WhackGame\src\IntroState.cpp:12)
#5 004016DA whack::Game::Game(this=0x22fbf0) (C:\Users\Main\Documents\Programs\WhackGame\src\Game.cpp:8)
#6 00401512 main() (C:\Users\Main\Documents\Programs\WhackGame\main.cpp:5)
Here is the code I'm calling it from (it crashes whether I use the initializer list or loadFromFile function:
IntroState::IntroState(sf::RenderWindow& window) : m_window(&window), m_needs_change(false, dte::StateChangeType::Next) //,m_background("Resources/Images/intro_background.png"), m_logo("Resources/Images/logo.png")
{
if(!m_background.loadFromFile("Resources/Images/intro_background.png"))
std::cerr << "Unable to load background texture for intro state." << std::endl;
if(!m_logo.loadFromFile("Resources/Images/logo.png"))
std::cerr << "Unable to load background texture for intro state." << std::endl;
}
It works fine if I do the textures and sprites separately. I am using TDM gcc 4.8.1, Windows 7 64-bit, and CB 12.11. I have included my TexturedSprite files and IntroState files in case you need any more information.