I have an issue with loading a buffer into a sound if the loading happens in a constructor.
If I have an empty object, that was initialized with the default(empty) constructor, and then I assign it to another object, the sound doesn't play, but if I just call the constructor which has the loading logic by default, it plays the sound fine.
Sorry if I am not being explicit enough, here is the code:
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
class Player
{
sf::SoundBuffer buf;
sf::Sound sound;
public:
Player() { }
Player(int a)
{
if(!buf.loadFromFile("Laser_Shoot2.wav"))
std::cerr << "error loading sound \n";
sound.setBuffer(buf);
}
void shoot()
{
sound.play();
}
};
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
#if true // doesn't play
Player player;
player = Player(3);
player.shoot();
#else // does play
Player player(3);
player.shoot();
#endif
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.display();
}
return 0;
}
I have used the precompiler if to test the bug, you can use it to see how it works, or doesn't. I made sure that the buffer is always in memory, which it is, I have checkes with gdb also, but it seems that the function setBuffer is not called for some reason if I copy the object over into an empty one. If I load the buffer again before playing the sound, it works just fine.
At the moment I have tried to find similar issues, but to no avail. I have tried to change the binaries of
the library, I used different audio drivers for the 2 audio outputs I have, doesn't work on either.
- OS: Windows 10 Pro 64-bit Ver. 20H2 OS build 19042.1466
- Compiler: g++ (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0
- Dynamic library: I have tried using 2.5.1 and main branch from github
- Audio Drivers: Realtek(R) Audio 6.0.8730.1
- Compiler flags: -g -O2 -std=c++17 -Wall
At the moment I am pretty sure this is not an intended feature of SFML, I have not found anything on the internet as I said, and neither on the forum or github, if I missed something and there was an issue raised, I am sorry, quite tired of debugging.
I have also made a test repo so that you would have all the files I have used:
https://github.com/Rocco2300/sfml-sound-issueIn the repo I have the 2.5.1 SFML version.