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

Author Topic: SFML book, chapter 2 white box issue  (Read 2239 times)

0 Members and 1 Guest are viewing this topic.

neobonde

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
SFML book, chapter 2 white box issue
« on: July 15, 2017, 11:54:01 pm »
Hey everyone

I have a problem trying to get the code from chapter 2 in the sfml game development book to work.
The problem is the whitebox issue, I understand what causes the whitebox (a loss of the texture) but i simply cant figure out why im losing my texture?

Here is my ResourceHolder
#pragma once
#include <map>
#include <memory>
#include <cassert>


template<typename Resource, typename Identifier>
class ResourceHolder
{
public:
        void load(Identifier id, const std::string& filename)
        {
                std::unique_ptr<Resource> resource(new Resource());
                if (!resource->loadFromFile(filename))
                        throw std::runtime_error("ResourceHolder::load - failed to load " + filename);
                auto inserted = mResourceMap.insert(std::make_pair(id, std::move(resource)));
                assert(inserted.second);
        }

        Resource get(Identifier id)
        {
                auto found = mResourceMap.find(id);
                assert(found != mResourceMap.end());

                return *found->second;
        }

        const Resource& get(Identifier id) const
        {
                auto found = mResourceMap.find(id);
                assert(found != mResourceMap.end());

                return *found->second;
        }

private:
        std::map<Identifier, std::unique_ptr<Resource>> mResourceMap;
};

 

And this is a snippet from Game.cpp, where i use the Resource holder:

Game::Game() : mTextures() ,mPlayer() ,mWindow(sf::VideoMode(640, 480), "SFML Application")
{
       
        try
        {
                mTextures.load(Textures::Airplane, "Eagle.png");
        }
        catch (std::runtime_error& e)
        {
                std::cout << "Exception: " << e.what() << std::endl;
        }

        mPlayer.setTexture(mTextures.get(Textures::Airplane));
        mPlayer.setPosition(200.f, 200.f);
}

for reference here is Game.hpp

#include <SFML/Graphics.hpp>
#include "ResourceHolder.hpp"

namespace Textures
{
        enum ID
        {
                Landscape,
                Airplane,
                Missile
        };
}

class Game
{
public:
        Game();
        void run();

private:
        void processEvents();
        void update(sf::Time deltaTime);
        void render();

        void handlePlayerInput(sf::Keyboard::Key key, bool isPressed);

public:
        sf::Time TimePerFrame = sf::seconds(1.f / 60.f);

private:
        ResourceHolder<sf::Texture, Textures::ID> mTextures;
        sf::Texture mTexture;
        sf::Sprite mPlayer;

        sf::RenderWindow mWindow;

        bool mIsMovingUp = false;
        bool mIsMovingDown = false;
        bool mIsMovingLeft = false;
        bool mIsMovingRight = false;

};
 

Hope one of you guys can help me out, because im really stuck :)
« Last Edit: July 15, 2017, 11:56:03 pm by neobonde »

HawkDeath

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: SFML book, chapter 2 white box issue
« Reply #1 on: July 16, 2017, 11:08:26 am »
Should help.
Code: [Select]
sf::Texture *mTexture = new sf::Texture();
mTexture ->loadFromFile("");

neobonde

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: SFML book, chapter 2 white box issue
« Reply #2 on: July 16, 2017, 11:53:02 am »
I guess that would work, but then i dont use the ResourceHolder? or am i missing something?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: SFML book, chapter 2 white box issue
« Reply #3 on: July 16, 2017, 12:25:34 pm »
Does it output the exception error?
It might be wise to return from the program if the loading fails.

You may also want to return a reference to the resource rather than a copy...
        Resource get(Identifier id)
 
« Last Edit: July 16, 2017, 12:30:06 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

HawkDeath

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: SFML book, chapter 2 white box issue
« Reply #4 on: July 16, 2017, 12:30:25 pm »
Now should work
Code: [Select]
template<typename Resource, typename Identifier>
class ResourceHolder
{
public:
void load(Identifier id, const std::string& filename)
{
std::unique_ptr<Resource> resource = std::make_unique<Resource>(Resource());
if (!resource->loadFromFile(filename))
throw std::runtime_error("ResourceHolder::load - failed to load " + filename);
auto inserted = mResourceMap.insert(std::make_pair(id, std::move( resource )));
assert(inserted.second);
}

Resource* get(Identifier id)
{
auto found = mResourceMap.find(id);
assert(found != mResourceMap.end());

return found->second.get();
}

const Resource* get(Identifier id) const
{
auto found = mResourceMap.find(id);
assert(found != mResourceMap.end());

return found->second;
}

private:
std::map<Identifier,std::unique_ptr<Resource>> mResourceMap;
};

sf::Texture *t = mTextures.get(Textures::ID::Airplane);
shape.setTexture(t);


neobonde

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: SFML book, chapter 2 white box issue
« Reply #5 on: July 16, 2017, 01:08:07 pm »
Thank you it helped :), but i dont know why the version from the book does not work? :) i have even just tried out their source code and it seams to be the same problem.

And no it does not output an exception error, and yes handling it would be ideal :)

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: SFML book, chapter 2 white box issue
« Reply #6 on: July 16, 2017, 01:15:05 pm »
The code from the book that is on its GitHub repository shows that it does actually return a reference:
ResourceHolder.hpp
ResourceHolder.inl

It's possible that it's a misprint in the book but you should always check their GitHub repository for up-to-date and corrected code :)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

neobonde

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: SFML book, chapter 2 white box issue
« Reply #7 on: July 16, 2017, 01:19:22 pm »
Oh i see now, it was just a missed & on my part, thanks.
There is no misprint in the book, i just didn't notice my mistake