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

Author Topic: Text and Audio aren't working  (Read 4502 times)

0 Members and 1 Guest are viewing this topic.

Nitetesi

  • Newbie
  • *
  • Posts: 9
    • View Profile
Text and Audio aren't working
« 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

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Text and Audio aren't working
« Reply #1 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.

Nitetesi

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Text and Audio aren't working
« Reply #2 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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Text and Audio aren't working
« Reply #3 on: November 20, 2021, 10:01:00 am »
For the sound you have to check that loading the audio file was successful.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/