SFML community forums
Help => Graphics => Topic started by: bucknasty189 on April 07, 2011, 06:06:18 pm
-
I keep getting runtime errors around the line,
m_Game->m_Window.Draw(*m_BackGround);
Please help, I'm utterly confused.
m_BackGround is a Sprite*
namespace DE
{
SplashState::SplashState() :
cGameState()
{
}
SplashState::~SplashState()
{
Cleanup();
}
void SplashState::Init(cGameEngine* game)
{
m_Game = game;
sf::Sprite bg;
sf::Image* image = gImageManager.getResource("../data/images/background.jpg");
//sf::Sprite bg(image);
if( image != NULL)
{
bg.SetImage( *image );
}
m_BackGround = &bg;
m_BackGround->SetPosition(m_Game->m_Window.GetWidth()/2,m_Game->m_Window.GetHeight()/2);
m_StartButton = new cp::cpButton(&m_Game->m_Window, &m_Game->m_GUIContainer, "Start", 100, 100, 60, 30);
}
void SplashState::Cleanup()
{
delete m_StartButton;
delete m_Game;
m_StartButton = NULL;
m_Game = NULL;
}
void SplashState::Pause()
{
}
void SplashState::Resume()
{
}
void SplashState::HandleEvents()
{
}
void SplashState::Update()
{
}
void SplashState::Draw()
{
m_Game->m_Window.Draw(*m_BackGround);
m_StartButton->Draw();
}
} //namespace DE
I have my states in an array of cGameState pointers after being told the vector moves around in memory and may screw the address of my image up.
This is how I call the state in my main loop
m_sStack[CURRENT_STATE]->Draw();
-
Hello,
make sure, that you're calling SplashState::Init() before drawing the scene for the first time(The ctor doesn't).
But I wonder, why you create your sprite in such a weird way.
I would prefer:
m_Background = new sf::Sprite( *image );
Make also sure that the image exists all the time. SetImage expects a reference to an sf::Image. So if getResource is only loading the image and don't save a copy of it, your application will crash probably ;).
-
Yeah, I guess that was pointless. Thanks for the help. I guess I'll have to find a way to save that image. My first thought is to use the Asset Manager from the example game engine in the community wiki.
Any other suggestions?
-
Yeah, do what Fred is suggesting, currently you create a sf::Sprite in the Init() and then it goes out of scope at the end of Init() and the memory is freed, and your m_Background pointer now points to freed memory.
-
Why do you use Init() and Cleanup()? Aren't the constructor and destructor the better choice, as they ensure proper initialization and destruction?
By the way, not everything needs to be a pointer. Is there a reason why m_BackGround is a sf::Sprite* and not a sf::Sprite? The same applies to other variables...
-
Thanks for the suggestions. I will get rid of those init and cleanup funcs. And change my members from pointers.