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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - winsett

Pages: [1]
1
Audio / Re: [SOLVED] No sound is played but in debug mode is played
« on: April 23, 2021, 09:12:18 am »
Actually the fewer items in the vector the more often it rearranges memory.
In visual studio pushing 10 items into a vector will rearrange the entire vector's memory 6 times (it rearranges all items after pushes 2, 3, 4, 5, 7, 10). GCC has a slightly different pattern, but same idea.
You can call reserve() on the vector to give it a bigger capacity before it rearranges again, but that's just delaying potential trouble.
Or use a vector of sound buffer pointers instead of sound buffers and allocate them explicitly, then reallocating the vector is harmless since the buffers themselves are fixed on the heap.
Next step is to add more sounds to the game and I'll see if there will be issues with the vector... If there are any I'll take into account your advice.  :)

2
Audio / Re: No sound is played but in debug mode is played
« on: April 22, 2021, 07:25:45 pm »
OK, I've found finally the bug. ;D It was in how I get each element from the vector in the for each loop in the SoundsPlayer::paly() method:

                for (auto sound : m_sounds)
                {
                        if (sound.getStatus() != sf::SoundSource::Status::Playing)
                        {
                                sound.setBuffer(buffer);
                                sound.play();
                                soundPlayed = true;
                                break;
                        }
                }
where I have forgotten to get the elements by reference:
for (auto& sound : m_sounds)
but not
for (auto sound : m_sounds).

It is a dumb mistake I've made it in the past too and I shall finally take notes about  ;D ;D


One thing with using std::vector is that any push_back can cause a reallocation of all elements and change in memory address, which for sf::SoundBuffers will break the connection to the sf::Sound and for sf::Sound the playback will be stopped.

My personally recommendation is to use a std::map or std::unordered_map for the sound buffers (see also the resource holder implementation) and then use a std::deque for the sf::Sound. That way you can push_back new sounds to play and pop_front sounds that finished playing (assuming they're all of about the same short play time).
Yes, your point makes sense but for now the sounds in my game will not be a big number and I think the vector will not rearrange addresses. But I will take it into account in the future if a problem arises.

3
Audio / Re: No sound is played but in debug mode is played
« on: April 21, 2021, 04:04:51 pm »
Hi @eXpl0it3r and thanks for your answer!

Either your posted code doesn't match what you've implemented...
Yes, the real code is a little bit complicated than what I presented here and that's why simplified it a bit in order to present the issue in a more clear way but I think logically it presents what I wrote on my real code.

or this line shouldn't compile.

soundsPly->play(resMan->getSoundBuffers().soundBuffers[0]);

getSoundBuffers() returns a vector, so you can't be accessing a property called soundBuffers.
ResourcesManager::getSoundBuffers() returns a vector to the private member in the same class:
std::vector<sf::SoundBuffer> soundBuffers
so I don't see why it shouldn't compile. But anyway that is not the issue here...  :)

Hard to say now, what your actual code is, but if getSoundBuffers really does return a std::vector<sf::SoundBuffer>, you're always return a copy of the the whole vector, which then only exists temporarily and will be deleted after the call to play() is finished.
So the temporary sound buffer copy gets destroyed, before it's actually played.

To fix this, you'd need to return a reference to the sound buffer.
OK, changed the declaration to
std::vector<sf::SoundBuffer>& getSoundBuffers(); and now the getter returns a reference to the vector of buffers but still no sound...

Additionally, I highly recommend to not misuse the singleton pattern for glorified globals. Instead use dependency injection, meaning to pass the resource manager and sound play via constructor to as few classes as possible.
OK, I'll try to refactor this but it will take some time as the Resource manager is used on many places... It's highly possible I to have some question to your instruction and will ask you later...

Thanks again for your directions!

4
Audio / [SOLVED] No sound is played but in debug mode is played
« on: April 20, 2021, 07:28:11 pm »
Hello!

I'm developing a small game and get to the point audio to be introduced to the game. But when I run the game and when sound should be played the game play no sound. The strange thing is when I'm in debug a sound is played when I go stepping through the code.  :o Nothing as error is shown to the console.

Here is a simplified scheme how I organized the audio system so far:
// singelton class for holding of all of the resources
class ResourcesManager
{
public:
    static ResourcesManager* getInstance(); // return single instance
    std::vector<sf::SoundBuffer> getSoundBuffers();
    ...
private:
    ResourcesManager() {}
    ...
    std::vector<sf::SoundBuffer> soundBuffers;
}

// another singelton class for playing the sounds
class SoundsPlayer
{
public:
        static std::shared_ptr<SoundsPlayer> getInstance();
        void play(const sf::SoundBuffer& buffer)
        {
                bool soundPlayed = false;
                // get next available sound
                for (auto sound : m_sounds)
                {
                        if (sound.getStatus() != sf::SoundSource::Status::Playing)
                        {
                                sound.setBuffer(buffer);
                                sound.play();
                                soundPlayed = true;
                                break;
                        }
                }
                if (!soundPlayed) // no sounds available
                {
                        sf::Sound sound;
                        m_sounds.push_back(sound);
                        size_t last = m_sounds.size() - 1;
                        m_sounds[last].setBuffer(buffer);
                        m_sounds[last].play();
                }
        }
private:
        SoundsPlayer() = default;
        std::vector<sf::Sound> m_sounds;
};


class PlayingCharacter
{
    ...
    void playAttackingSound()
        {
                extern ResourcesManager *resMan;

                SoundBuffersHolder soundBuffersHolder;
                std::shared_ptr<SoundsPlayer> soundsPly = SoundsPlayer::getInstance();
                bool isErr;
                soundsPly->play(resMan->getSoundBuffers().soundBuffers[0]);
        }
}
 

Some explanations on the code above:
  • ResourcesManager is taking care of holding all of the resources and it is a singelton class. So far it works the way I organized it for the textures and other resources - I invoke
    extern ResourcesManager *resMan;
    and then I get what I need from it, i.e.
    sf::texture tex = resMan->getTexture() )
  • First I simply tried to play a sound directly in the PlayingCharacter::playAttackingSound() method but there was no sound produced and I decided that probably the sf::Sound variable goes out of scope and that's why I made this SoundsPlayer singelton
  • The PlayingCharacter::playAttackingSound() is invoked on more than a seconds (timer is taking care of it) and the sound is less than a second long so no overlapping happens
  • Of course, these classes are in different files each

What am I missing?  :-\

5
Window / Re: Errors when initializing a sf::RenderWindow variable
« on: December 15, 2016, 07:43:27 pm »
Please, always use Google before posting on any kind of forum. You'll avoid asking about one of the most asked question ever. :)
http://stackoverflow.com/questions/7004031/c-unresolved-externals

Unresolved externals error usually means that you have linked something incorrectly.  Please go through the tutorial and only when you get stuck come back with the question and a note where exactly you got stuck.
Thank you for your reply, sjaustirni and for your directions!
I usually google before ask questions - this saves me lots of time but this time I couldn't find anything about this kind of error - it appears I didn't make good enough search. Now while I know its in the linker I fixed the problem.

6
Window / Errors when initializing a sf::RenderWindow variable
« on: December 12, 2016, 02:46:41 pm »
Hi there, new to this forum and with a little experience with SFML programming.

I get these 3 errors when I try to compile a VS2015 c++ project.

Quote
Error   LNK2019   unresolved external symbol "__declspec(dllimport) public: __thiscall sf::String::String(char const *,class std::locale const &)" (__imp_??0String@sf@@QAE@PBDABVlocale@std@@@Z) referenced in function "protected: virtual int __thiscall CBatteryAlarmDlg::OnInitDialog(void)" (?OnInitDialog@CBatteryAlarmDlg@@MAEHXZ)   BatteryAlarm   E:\docs\programirane\BatteryAlarm\BatteryAlarm\BatteryAlarmDlg.obj   1   

Error   LNK2019   unresolved external symbol "__declspec(dllimport) public: __thiscall sf::String::~String(void)" (__imp_??1String@sf@@QAE@XZ) referenced in function "protected: virtual int __thiscall CBatteryAlarmDlg::OnInitDialog(void)" (?OnInitDialog@CBatteryAlarmDlg@@MAEHXZ)   BatteryAlarm   E:\docs\programirane\BatteryAlarm\BatteryAlarm\BatteryAlarmDlg.obj   1   

Error   LNK1120   2 unresolved externals   BatteryAlarm   E:\docs\programirane\BatteryAlarm\BatteryAlarm\Debug\BatteryAlarm.exe   1   

These errors appear only when I add this peace of code:
Quote
sf::RenderWindow testWindow(sf::VideoMode(800, 600), "test window");

Could you help me resolve what I've done wrong? Thanks! :)

Pages: [1]
anything