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

Author Topic: program crashes for some reason  (Read 2189 times)

0 Members and 1 Guest are viewing this topic.

lorence30

  • Full Member
  • ***
  • Posts: 124
    • View Profile
    • Email
program crashes for some reason
« 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: program crashes for some reason
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

lorence30

  • Full Member
  • ***
  • Posts: 124
    • View Profile
    • Email
Re: program crashes for some reason
« Reply #2 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);
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: program crashes for some reason
« Reply #3 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 if you want to see a working resource manager.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

lorence30

  • Full Member
  • ***
  • Posts: 124
    • View Profile
    • Email
Re: program crashes for some reason
« Reply #4 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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: program crashes for some reason
« Reply #5 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
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

lorence30

  • Full Member
  • ***
  • Posts: 124
    • View Profile
    • Email
Re: program crashes for some reason
« Reply #6 on: July 09, 2015, 11:45:23 am »
Quote
Don't use global/static resources.
why?

it crashes when i use fonts


Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: program crashes for some reason
« Reply #7 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: