why this code not playing voice of cat i.e. when selected CAT from ListBox and p

Started by Nabeel Nazir, 09 September 2013, 08:22:32

Nabeel Nazir

hi , i hope you are fine texus ! I am little bit confuse about these two piece of codes ! Please explain these as good as you can , I shall be very thankful !


buffers.push_back(sf::SoundBuffer());
buffers.back().loadFromFile(name + ".wav");

sounds.push_back(sf::Sound());
sounds.back().setBuffer(buffers.back());

-------------------------

auto& items = listBox->getItems();
for (auto it = items.cbegin(); it != items.cend(); ++it)
{
ofile << it->toAnsiString() << std::endl;
}


texus

I just placed comments in your code to make it more clear. But if there is still something specifically that you don't understand then just let me know.

buffers.push_back(sf::SoundBuffer());  // Create a new SoundBuffer an put it in the back of the list
buffers.back().loadFromFile(name + ".wav");  // Initialize the just created SoundBuffer

sounds.push_back(sf::Sound());  // Create a new Sound object and put it in the back of the list
sounds.back().setBuffer(buffers.back());  // Tell the just created Sound object that it should use the just created SoundBuffer

-------------------------
auto& items = listBox->getItems();  // Get a list of all the items in the list box (auto is std::vector<sf::String> here)
for (auto it = items.cbegin(); it != items.cend(); ++it)  // Use an iterator to loop over the list of items (auto is a std::vector<sf::String>::const_iterator here)
{
    ofile << it->toAnsiString() << std::endl;  // Write the item in the list to the file ( '*it' is a sf::String, and to write to a file you need an std::string which is why you need the toAnsiString function )
}


Nabeel Nazir

kindly explain auto keyword and toAnsiString() function more because i never use them ever before !

texus

For auto, you probably find better explanations when googling for it.
You can find some info here for example: https://www.cprogramming.com/c++11/c++11-auto-decltype-return-value-after-function.html

For the toAnsiString you can look at its documentation.
When writing to a file (or in the terminal), you can only use std::string. The sf::String class doesn't provide the '<<' operator which would allow it to be written to the file. So you need to convert the sf::String to an std::string first (which is what the toAnsiString function does) before writing it to the file. The reason I use sf::String instead of std::string is because sf::String also supports unicode characters, but as long as you don't use them then you don't have to worry about it and just use toAnsiString to get the std::string.

Nabeel Nazir

what advance function actually do ?
Please reply ASAP !

if(callback.id==1)
{
auto it = sounds.begin();
std::advance(it, listBox->getSelectedItemIndex());
it->play();
}

texus

You have an iterator 'it' that points to the first element in the list.
The advance function will increment it a few times so that the iterator then points to the x'th element in the list.

If you want quick answers then I think you would better google for it. I'm sure you'll find enough explanation at cplusplus.com.