SFML community forums

Help => General => Topic started by: Nitetesi on November 19, 2021, 09:19:54 pm

Title: Text and Audio aren't working
Post by: Nitetesi on November 19, 2021, 09:19:54 pm
I am using Debug x64 and i am not mixing libraries (linked libs with -d suffixes)
No sound at all and Text is broken because of the font

void Window::setText()
{
        sf::Font font;
        if (!font.loadFromFile("edge.otf"))
        {
          std::cout << "ERROR" << std::endl;
        }
        this->text.setFont(font);
        this->text.setCharacterSize(24);
        this->text.setString("Hello World");
}
 

Exception thrown at 0x00007FFD771569DB (sfml-graphics-d-2.dll) in WindWar.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

Sound::Sound()
{
        buffer.loadFromFile("assets/harp.wav");
}

Sound::~Sound()
{
}

void Sound::play()
{
        sound.setBuffer(buffer);
        sound.play();
}
 

No error but no sound
Title: Re: Text and Audio aren't working
Post by: kojack on November 20, 2021, 01:18:16 am
For the text, the problem is your font is a local variable in Window::setText(). Once that function ends, the font is deconstructed. As the header for Text::setFont says:
Quote
The font argument refers to a font that must exist as long as the text uses it. Indeed, the text doesn't store its own copy of the font, but rather keeps a pointer to the one that you passed to this function. If the font is destroyed and the text tries to use it, the behavior is undefined.
So the Font should be stored somewhere more permanent.
Title: Re: Text and Audio aren't working
Post by: Nitetesi on November 20, 2021, 06:35:55 am
For the text, the problem is your font is a local variable in Window::setText(). Once that function ends, the font is deconstructed. As the header for Text::setFont says:
Quote
The font argument refers to a font that must exist as long as the text uses it. Indeed, the text doesn't store its own copy of the font, but rather keeps a pointer to the one that you passed to this function. If the font is destroyed and the text tries to use it, the behavior is undefined.
So the Font should be stored somewhere more permanent.
Oh that was the problem for the font, thanks
Title: Re: Text and Audio aren't working
Post by: eXpl0it3r on November 20, 2021, 10:01:00 am
For the sound you have to check that loading the audio file was successful.