SFML community forums

Help => Graphics => Topic started by: lorence30 on July 05, 2015, 08:37:59 pm

Title: program crashes for some reason
Post by: lorence30 on July 05, 2015, 08:37:59 pm
my program crashes when i do like this

sf::Text MainScreen::NEWGAME("NEWGAME",sf::Font(Resource<sf::Font>::getResource("resources//fonts//arial.ttf")),50);

void MainScreen::showMainScreen()
{
    WindowManager::getGameWindow().draw(NEWGAME);
}


but my program works fine when i do like this

void MainScreen::showMainScreen()
{
    WindowManager::getGameWindow().draw(sf::Text("NEWGAME",sf::Font(Resource<sf::Font>::getResource("resources//fonts//arial.ttf")),50));
}

I cant figure it out.
Title: AW: program crashes for some reason
Post by: eXpl0it3r on July 05, 2015, 08:54:30 pm
As long as you use the text the font must exist and as long as you use a font that loads from a stream, the stream must exist.

Provide a minimal and compilable example if you want more information about your problem.
Title: Re: program crashes for some reason
Post by: lorence30 on July 06, 2015, 05:21:33 pm
sorry for the late reply
this is my Resource class
#ifndef RESOURCE_H
#define RESOURCE_H

#include <SFML/Graphics.hpp>
#include <map>
#include <string>

template<typename T> class Resource
{
public:
    static const T& getResource(const std::string);
private:
    static std::map<std::string,T>resource_map;
    Resource();
};

template<typename T> std::map<std::string,T> Resource<T>::resource_map;

template<typename T> const T& Resource<T>::getResource(const std::string filename)
{
    auto& rResource = resource_map;

    auto iter = rResource.find(filename);

    if ( iter != rResource.end() )
        return iter->second;
    else
    {
        auto& _key_pair = rResource[filename];
        _key_pair.loadFromFile(filename);
        return _key_pair;
    }
}

#endif // RESOURCE_H
 

in any case if this is working:
void MainScreen::showMainScreen()
{
WindowManager::getGameWindow().draw(sf::Text("NEWGAME",sf::Font(Resource<sf::Font>::getResource("resources//fonts//arial.ttf")),50));
}
this must be working too
sf::Text MainScreen::NEWGAME("NEWGAME",sf::Font(Resource<sf::Font>::getResource("resources//fonts//arial.ttf")),50);

void MainScreen::showMainScreen()
{
    WindowManager::getGameWindow().draw(NEWGAME);
}
Title: Re: program crashes for some reason
Post by: eXpl0it3r on July 06, 2015, 06:38:34 pm
Well as I said, you create a temporary object and the font needs to exist the whole time you use the sf::Text and not just temporarily.

Take a look at Thor's resource holder (http://www.bromeon.ch/libraries/thor/v2.0/tutorial-resources.html) if you want to see a working resource manager.
Title: Re: program crashes for some reason
Post by: lorence30 on July 08, 2015, 05:55:38 am
sorry for the late reply @eXpl0it3r
i was so busy.

i change my code into this

sf::Font showMainScreen::font_class(Resource<sf::Font>::getResource("resources//fonts//arial.ttf");
sf::Text MainScreen::NEWGAME("NEWGAME",font_class,50);
void MainScreen::showMainScreen()
{
WindowManager::getGameWindow().draw(NEWGAME);
}

font_class and NEWGAME should be exist till the end of the program because theyre are static. but still crashing
Title: AW: program crashes for some reason
Post by: eXpl0it3r on July 08, 2015, 08:18:38 am
Don't use global/static resources.
When is it crashing btw?

Also you don't need to apologize every time you post something. :D
Title: Re: program crashes for some reason
Post by: lorence30 on July 09, 2015, 11:45:23 am
Quote
Don't use global/static resources.
why?

it crashes when i use fonts

Title: Re: program crashes for some reason
Post by: Nexus on July 11, 2015, 09:51:22 am
Because they lead to problems, especially with SFML resources.
There's a ton of threads about this topic, you could use the search function.