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

Author Topic: sound /music loads successfully but no sound at all  (Read 15565 times)

0 Members and 1 Guest are viewing this topic.

alien8

  • Newbie
  • *
  • Posts: 16
  • 8 bits rocks!
    • View Profile
    • Email
Re: sound /music loads successfully but no sound at all
« Reply #15 on: August 13, 2015, 01:07:40 pm »
I apologize if this post appears to not consider your last response, Hapax, you're so kind but I must paste prewritten posts on the run...OK I know my next stop is the system (window maybe more) module.


The biggest error was to be mistakenly convinced that due to the presence of the 'if (!buffer.loadFromFile("..."))' the buffer of the sound wouldn't be not only damaged but also repeated or born from zero.
That is precisely the reason
"
if (sound.getStatus() != sf::Sound::Playing)
    sound.play();
"
that brillantly was suggested gets its purpose defeated

I don't know why I was using 2,2 tuto but the only difference to 2.3 is that it doensn't specify the list of supported formats

Having read the system module, etc. I tend to think that the position of the main loop is for facilitate, among other things, space for events (through the event loop itself) and that's the reason you must not integrate the bulk of the code into the main loop.
But then it arises the question of what about the supervisor ( ...is that the name for the loop in wich the course of the game takes place? ).

Anyways, I want to think that if the buffers are not destroyed, the sound variables are available, and not only that, if there is playing any music, it arrives to the end if no method interrupts it. Another question is if you include all this in a thread and that thread is affected by something.
Am I in the right track?.


..so many thanks, I hope this solves initial doubts of mine and can focus on the provided links by Hapax
Alien 8

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: sound /music loads successfully but no sound at all
« Reply #16 on: August 13, 2015, 09:56:27 pm »
The biggest error was to be mistakenly convinced that due to the presence of the 'if (!buffer.loadFromFile("..."))' the buffer of the sound wouldn't be not only damaged but also repeated or born from zero.
That is precisely the reason
"
if (sound.getStatus() != sf::Sound::Playing)
    sound.play();
"
that brillantly was suggested gets its purpose defeated
Not really.
Your biggest error (to use your own words) here is to not realize a very basic C++ fact; that objects created in a scope, on the stack, get destroyed when that scope (in this case, the game loop) ends.  This is very fundamental C++ knowledge and, had you had that knowledge, it would/should have made it obvious that the "sound" object would not live for more than a single frame. The if statement testing whether or not the sound is currently playing is completely irrelevant in that context.

Having read the system module, etc. I tend to think that the position of the main loop is for facilitate, among other things, space for events (through the event loop itself) and that's the reason you must not integrate the bulk of the code into the main loop.
But then it arises the question of what about the supervisor ( ...is that the name for the loop in wich the course of the game takes place? ).
The main "game" loop is what (in most games) drives everything. It is responsible for processing events, updating game state and rendering the current state of the world. It's what makes the game "tick".
The main game loop repeats over and over - every frame - and it's where you do all your processing. But, that doesn't mean that you need to create all objects within the loop. Long lived objects usually get created before the loop and persist for the entire lifetime of the application. Objects that need to be dynamically created (but need to live more than a single frame) are usually created on the heap and stored as std::unique_ptr or std::shared_ptr objects - commonly in STL containers like std::vector or similar.

Anyways, I want to think that if the buffers are not destroyed, the sound variables are available, and not only that, if there is playing any music, it arrives to the end if no method interrupts it.
You may think what you want. But that's just not good enough. You need to know how the lifetime rules for objects in C++ work.
As long as your sound object is alive and there's something to play in its buffer and play() has been called on it, it will play. But if you destroy the object (for example; by letting it go out of scope) or destroy the buffer it's playing from, then - surprise, surprise - it will stop playing.

Another question is if you include all this in a thread and that thread is affected by something.
Music already plays in its own thread. You don't need to worry about that...  Please, please don't even consider threads until you have a much firmer grasp of C++ (I'd say at least 5 more years of experience or so).

Am I in the right track?.
Personally, I'd say no.
You seem to be guessing randomly and making assumptions that you have no basis for.
You should attempt to get a firmer grasp of the C++ language before proceeding or you'll only get yourself into trouble that you'll have no idea how to dig yourself out of.
« Last Edit: August 13, 2015, 11:17:14 pm by Jesper Juhl »