SFML Game development problem

i have been following the SFML Game Development book and i am on page 40. when i try to run the program instead of the texture appearing it shows a white rectangle.

here is my code:

ResourceHolder.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#ifndef TEXTURES_H_INCLUDED
#define TEXTURES_H_INCLUDED

#include <map>
#include <string>
#include <SFML\Graphics.hpp>
#include <memory>

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

class TextureHolder
{
private:
    std::map<Textures::ID,
    std::unique_ptr<sf::Texture>> mTextureMap;

public:

    void load(Textures::ID id, const std::string& filename);

    sf::Texture& get(Textures::ID id);

    const sf::Texture& get(Textures::ID id) const;
};

#endif // TEXTURES_H_INCLUDED 


ResourceHolder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "TextureHolder.h"

inline void TextureHolder::load(Textures::ID id, const std::string& filename)
{
    std::unique_ptr<sf::Texture> texture(new sf::Texture());
    texture->loadFromFile(filename);

    mTextureMap.insert(std::make_pair(id, std::move(texture)));
}


inline sf::Texture& TextureHolder::get(Textures::ID id)
{
    auto found = mTextureMap.find(id);
    return *found->second;
}


inline const sf::Texture& TextureHolder::get(Textures::ID id) const
{
    auto found = mTextureMap.find(id);
    return *found->second;
}


Game.cpp (relevant part i include ResourceHolder.cpp)
1
2
3
TextureHolder textures;
        textures.load(Textures::ID::Airplane, "Media/Textures/Eagle.png");
        mPlayer.setTexture(textures.get(Textures::Airplane));


i have been trying to figure this out but i just can't see the problem.
anyone?
Do you have the images in the correct folders?
Hello,

When you are executing the line:

 
    texture->loadFromFile(filename);


You are not capturing error messages. You should find out what return values are and display appropriate messages when an error is encountered. It should like you never loaded your texture file, or perhaps the wrong one.

Largins (Larry)
Two Possibility(that comes mind):

Possibility #1:(Likely not it)

They're saying your image file may not be where you believe it to be.

Using IDE's you'll have to consider the idea that the executable is ran from inside the Debug folder or Release folder (people sometimes forget).

Ensure that your image file is there.

Otherwise, everyone is telling to you wrap your:
 
texture->loadFromFile(filename)

in an if statement - to ensure it properly loaded.

sf::Texture should return a bool if it failed to load from file.

http://sfml-dev.org/documentation/2.0/classsf_1_1Texture.php#a8e1b56eabfe33e2e0e1cb03712c7fcc7

http://sfml-dev.org/documentation/2.0/classsf_1_1Texture.php

---

Possibility #2:(This is probably it)

If that isn't it, you need to ensure that your texture object persists after leaving the function; persisting data would be, data from a higher scope, global, or "new" data.

SFML is responsible for all of its objects and will destroy them if you are to ever lose scope. It only binds to a pointer of Texture, so that may be the reason for having an empty image instead of your predicted image.
Last edited on
mPlayer is part of the game class so it is making a pointer to the texture it is using
Not entirely sure what's happening, but let me comment the flow here: (high possibility I am wrong)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
inline void TextureHolder::load(Textures::ID id, const std::string& filename)
{
    //-Initial calling of TextureHolder::load function-
        //id is a copied variable
        //filename is a referenced string

    std::unique_ptr<sf::Texture> texture(new sf::Texture());
    //-Smart pointer with sf::Texture datatype
        //declares a texture    //non-Persistent scope - a copy of whatever's in new sf::Texture - which has no image;
            //with new sf::Texture(); //Persistent scope

    texture->loadFromFile(filename);
    //-Attempting to load from file
        //http://www.sfml-dev.org/documentation/2.0/classsf_1_1Texture.php#a8e1b56eabfe33e2e0e1cb03712c7fcc7
            //creates a (new?) sf::image - non pointer, local object
            //texture (possibly points to or) is assigned the image

    mTextureMap.insert(std::make_pair(id, std::move(texture)));
    //-Makes a pair using:
        //id - a temporary variable but possibly good enough for plain data
        //RealValue of texture - floating duplicate of Texture and values
            //http://en.cppreference.com/w/cpp/utility/move
            //Duplicated the Texture objects data - stateless and persistent
                //Creates a new sf::Texture Object
                    //Object is at a new block of memory ( different variables and pointers) 
                    //pointers still pointing towards the same block of memory though)
                    //http://www.sfml-dev.org/documentation/2.0/classsf_1_1Texture.php#a8e1b56eabfe33e2e0e1cb03712c7fcc7

}
//-function ends
    //id is released - values in copied id destroyed
    //local Texture release - smart pointer, and has pointers - No longer being used
        //deletes new sf::Texture and the sf::Image created during loadfromfile?(guessing - hence why textures are scope reliant)
        //http://en.cppreference.com/w/cpp/memory/unique_ptr 


Just a bunch of assumption.

I don't completely understand the library and have not looked into the internal bindings yet, but some stuff you might want to consider, and possibly help you debug.

"std::unique_ptr is a smart pointer that retains sole ownership of an object through a pointer and destroys that object when the unique_ptr goes out of scope. No two unique_ptr instances can manage the same object"

Again, those were my speculations.
Hopefully, someone out there who has a strong understanding of the behavior of SFML and smart pointers can enlighten us.
Last edited on
Topic archived. No new replies allowed.