I've just copied the code in the SFML Game Development book up to page 25 where I've simply got a sprite that I can move around the screen with a fighter jet texture. The issue is that the texture loses context somehow, and I'm left just with a white box. Same symptom as a lot of cases on the internet, but this is just copying from the book. I even downloaded the chapter 1 source which has fonts as well and I had an issue with the font as well.
It loads the texture just fine, and stores it in a global variable as was instructed in the book. I never actually get to see it displayed but I know it stores because I tested writing it back out to another file.
I put the rendering process in the constructor as a test (to eliminate context return at the end of the constructor) and tried writing the texture to file after each part....
Game::Game()
: mWindow(sf::VideoMode(640,480), "SFML APPLICATION")
, mTexture()
, mPlayer()
, mIsMovingUp(false)
, mIsMovingDown(false)
, mIsMovingRight(false)
, mIsMovingLeft(false)
{
if(!mTexture.loadFromFile("Media/Textures/Eagle.png"))
{
std::cout<<"Error loading texture";
}
//mTexture.copyToImage().saveToFile("out.png");
mPlayer.setTexture(mTexture);
//mPlayer.getTexture()->copyToImage().saveToFile("out.png");
mPlayer.setPosition(100.f,100.f);
mWindow.clear();
mPlayer.getTexture()->copyToImage().saveToFile("out.png");
mWindow.draw(mPlayer);
mPlayer.getTexture()->copyToImage().saveToFile("out2.png");
mWindow.display();
mPlayer.getTexture()->copyToImage().saveToFile("out3.png");
}
Here out.png is written but not the others, so I figure the texture is lost in the draw method somehow. Putting a breakpoint on the draw method I can see in the debugging info that m_texture is set to 1 at that point. Stepping to the next line m_texture is 1115684864.
The texture is just stored as a global as per the following Game.h file...
class Game
{
public:
Game();
void run();
private:
void processEvents();
void update(sf::Time deltaTime);
void render();
void handlePlayerInput(sf::Keyboard::Key key, bool isPressed);
private:
static const float PlayerSpeed;
static const sf::Time TimePerFrame;
sf::RenderWindow mWindow;
sf::Texture mTexture;
sf::Sprite mPlayer;
bool mIsMovingUp;
bool mIsMovingDown;
bool mIsMovingRight;
bool mIsMovingLeft;
};
I have no idea what the problem could be. I'm running mingw through codeblocks. I can only think that it could be a compiler/environment issue.
Any help would be great.
Thanks.