Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: (Solved) SFML Game Dev Book - Ch1 - Sprite issue  (Read 2141 times)

0 Members and 1 Guest are viewing this topic.

Cameron

  • Newbie
  • *
  • Posts: 3
    • View Profile
(Solved) SFML Game Dev Book - Ch1 - Sprite issue
« on: December 08, 2014, 12:49:42 pm »
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.
« Last Edit: December 09, 2014, 03:34:48 am by Cameron »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: SFML Game Dev Book - Ch1 - Sprite issue
« Reply #1 on: December 08, 2014, 01:22:39 pm »
The updated and correct code to the book can be found here. Don't use a different source.

Make sure your GPU driver is uptodate. Go your vendors website and download it from there.

Build the latest SFML version, to ensure it's not an already fixed issue.

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 highly doubt that the book is instructing you to use sf::Texture in a global scope. Can you provide to code to that claim?

What do out2.png and out3.png contain? Do they even get created?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Cameron

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: SFML Game Dev Book - Ch1 - Sprite issue
« Reply #2 on: December 09, 2014, 03:13:17 am »
Quote
Make sure your GPU driver is uptodate. Go your vendors website and download it from there.

I try to keep it updated. There's a new release so I'll get that just in case.

Quote
Build the latest SFML version, to ensure it's not an already fixed issue.

I built it only a few weeks ago. Perhaps I downloaded the wrong one. I'll re-download and try again.

Quote
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 highly doubt that the book is instructing you to use sf::Texture in a global scope. Can you provide to code to that claim?

https://github.com/SFML/SFML-Game-Development-Book/blob/master/01_Intro/Include/Book/Game.hpp

Quote
What do out2.png and out3.png contain? Do they even get created?

They don't get created.


I'll post again once I've rebuilt SFML. I'm presuming this is the best place to get the latest release. https://github.com/SFML/SFML . I think the one I'm running is the 2.1 stable release.


Cameron

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: SFML Game Dev Book - Ch1 - Sprite issue
« Reply #3 on: December 09, 2014, 03:34:30 am »
The SFML 2.2 build on github solved the issue. Thanks for the suggestion :).

 

anything