16
Window / Unhandled exception on window creation
« on: March 01, 2012, 09:02:05 pm »Quote from: "Laurent"
QuoteSo the lesson I learned is to never use static SFML objects, always use pointers and dynamic objects!
Wrong. Never use pointers and dynamically allocated objects, unless you really need to.
The lesson you learnt is to never use global objects
I have some problems occurring only with static SFML objects, I don't have this when I change the sf::Texture and sf::Sprite to pointers instead
![Smiley :)](https://www.sfml-dev.org/forums/Smileys/default/smiley.gif)
The code below runs fine, but when I change the Texture and Sprite pointers to real objects, I get trouble!
GS_Menu.h
Code: [Select]
#include "IGameState.h"
#include <SFML\Graphics.hpp>
class GameView;
class GS_Menu : public IGameState
{
public:
bool Init(GameView* gameView, GameEngine* gameEngine, GameLogic* gameLogic);
void DrawBackground();
~GS_Menu();
static GS_Menu* Instance() {
return &m_menuState;
}
protected:
GS_Menu();
private:
static GS_Menu m_menuState;
sf::Texture* m_bg;
sf::Sprite* m_bgs;
bool m_init;
};
GS_Menu.cpp
Code: [Select]
#include "GS_Menu.h"
#include "..\..\GameEngine\GameEngine.h"
#include <SFML\Graphics.hpp>
GS_Menu GS_Menu::m_menuState;
GS_Menu::GS_Menu(){}
GS_Menu::~GS_Menu(){}
bool GS_Menu::Init(GameView* gameView, GameEngine* gameEngine, GameLogic* gameLogic)
{
GView = gameView;
GEngine = gameEngine;
GLogic = gameLogic;
m_bg = new sf::Texture();
m_bgs = new sf::Sprite();
//open BG file
if(!m_bg->LoadFromFile("img\\bg\\mainbg.PNG"))
{
GEngine->WriteLog("mainbg.PNG cannot be loaded");
return false;
}
GEngine->guiManager()->NewButton(1,50,50);
return true; //true on succesfull initialization
}
void GS_Menu::Draw()
{
DrawBackground();
}
void GS_Menu::DrawBackground()
{
m_bgs->SetTexture(*m_bg);
m_bgs->SetPosition(0,0);
GEngine->graphicsManager()->App()->Draw(*m_bgs);
}