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

texus

Which part about the file handling isn't working exactly (the code looks good at first sight)? Does the text file get written correctly? What gets placed in the list box when trying to load from a file?

If you want to store the animals, even after the program has quit, then there is no other option than storing it in a text file on the hard disk.

Nabeel Nazir

I put some animal names in the text file and read them from file into listBox's additem function one by one ;and it is going well  ... now i want to write all items of listBox into the file as you suggest me and give me piece of code ... but it's not working well , it is giving some kind of compiler/syntax error !

std::ofstream ofile("animals.txt");
if (ifile.is_open())
{
    auto& items = listBox->getItems();
    for (auto& item : items)
    {
        ofile << item.toAnsiString() << std::endl;
    }
}

   

Nabeel Nazir


texus

I edited my post later because I realized that this was going to happen. VS2010 has only very limited c++11 support.

Quote from: texus on 15 September 2013, 11:55:10Edit: just realized that that for loop is only supported since VS2012. It will be like this for you:
for (auto it = items.cbegin(); it != items.cend(); ++it)
{
    ofile << it->toAnsiString() << std::endl;
}

Nabeel Nazir

hi, check please what i am doing wrong, it's not working well, means animal name will be added but when window get's closed and again i run the code there is no recently added animal name!
[I DON'T WANT TO ATTACH THIS PRINT SCREEN , I CLEARED THE ATTACHMENT SEVERAL TIME ; BUT DON'T KNOW WHY THIS WILL AGAIN ATTACH,PLEASE DON'T CONSIDER IT ]


#include <SFML/Audio.hpp>
#include <TGUI/TGUI.hpp>
#include <conio.h>
//#include <cstdio>
#include <fstream>
//void function(const tgui::Callback& callback)
//{
//    if (callback.id == 1)
// {
// //std::cout << "Hello world" << std::endl;
// }
//
//
//
//}

int main()
{

std::list<sf::SoundBuffer> buffers;
std::list<sf::Sound> sounds;

sf::SoundBuffer buffer;
sf::SoundBuffer buffer1;
sf::SoundBuffer buffer2;
sf::SoundBuffer buffer3;
sf::SoundBuffer buffer4;
sf::SoundBuffer buffer5;
sf::Sound sound;

//sound.setBuffer(buffer);
sound.setPitch(1.5f);
sound.setVolume(80);
    // Create the window
    sf::RenderWindow window(sf::VideoMode(800, 600), "Window");
    tgui::Gui gui(window);

std::string name;

tgui::ListBox::Ptr listBox(gui);

std::ifstream ifile;
ifile.open("animals.txt");
if(ifile.is_open())
{
while(!ifile.eof())
{
ifile>>name;
ifile.ignore(10,'\n');
listBox->addItem(name);

}

}
else
{
std::cout<<"ifile.txt does not open properly!"<<std::endl;
}



    // Load the font (you should check the return value to make sure that it is loaded)
    gui.setGlobalFont("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/fonts/DejaVuSans.ttf");



    tgui::Button::Ptr button(gui);
    button->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
button->setPosition(280, 250);
    button->setText("SPEAK");
    button->setSize(200, 40);



listBox->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
listBox->setSize(150, 120);
    listBox->setItemHeight(30);
    listBox->setPosition(300, 30);
//   listBox->addItem("COW");
//   listBox->addItem("CAT");
//   //listBox->addItem("DONKEY");
//listBox->addItem("LION");
//listBox->addItem("GOAT");
listBox->setCallbackId(3);

tgui ::Callback callback;

button->bindCallback(tgui::Button::LeftMouseClicked);
button->setCallbackId(1);

tgui::EditBox::Ptr editBox(gui);
    editBox->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
    editBox->setBorders(6, 4, 6, 4);
    editBox->setPosition(230, 350);
    editBox->setText("Add Animal Name Here!");
editBox->setSize(360, 40);

tgui::Button::Ptr button1(gui);
    button1->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
button1->setPosition(280, 450);
    button1->setText("Add Animal");
    button1->setSize(200, 40);

button1->bindCallback(tgui::Button::LeftMouseClicked);
button1->setCallbackId(2);

buffer.loadFromFile("Lion Roar.wav");
//sound.setBuffer(buffer);
buffer1.loadFromFile("Goat Bah.wav");
//sound.setBuffer(buffer1);
buffer2.loadFromFile("Cat Meow.wav");
buffer3.loadFromFile("Single Cow.wav");
//sound.setBuffer(buffer2);

//   //tgui::Button::Ptr button(gui);
//   button->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black .conf");
//button->setPosition(380, 350);
//   button->setText("Add");
//   button->setSize(200, 40);


    while (window.isOpen())
    {
/////////////////////////////////////////////////////

while (gui.pollCallback(callback))
{

if(callback.id==1)
{
if (listBox->getSelectedItem()=="CAT")
{

sound.setBuffer(buffer2);
sound.play();

}

else if (listBox->getSelectedItem()=="LION")
{

sound.setBuffer(buffer);
sound.play();

}
else if (listBox->getSelectedItem()=="GOAT")
{
sound.setBuffer(buffer1);
sound.play();
}
else if(listBox->getSelectedItem()=="COW")
{
sound.setBuffer(buffer3);
sound.play();

}

}
/*else
{
std::cout<<"id is not valid!!!!"<<std::endl;
}*/
else if(callback.id==2)
{
//editBox->getText();

//listBox->setSelectedItem(listBox->getItems().size()-1); // Select the last item in the list box

name=editBox->getText();
listBox->addItem(name);


/*buffers.push_back(sf::SoundBuffer());
buffers.back().loadFromFile(editBox->getText() + ".wav");

sounds.push_back(sf::Sound());
sounds.back().setBuffer(buffers.back());
sounds.back().setPitch(1.5f);
sounds.back().setVolume(80);

auto it = sounds.begin();
std::advance(it, listBox->getSelectedItemIndex());
it->play();*/


}


}

std::ofstream ofile("animals.txt");
if (ifile.is_open())
{
auto& items = listBox->getItems();
for (auto it = items.cbegin(); it != items.cend(); ++it)
{
ofile << it->toAnsiString() << std::endl;
}
}



//////////////////////////////////////////////////////
       
sf::Event event;
        while (window.pollEvent(event))
        {

if (event.type == sf::Event::Closed)
window.close();

// Pass the event to all the widgets (if there would be widgets)
gui.handleEvent(event);
}

        window.clear();

        // Draw all created widgets
        gui.draw();

        window.display();

}





//getch();
return EXIT_SUCCESS;
}





texus

You are writing to the file every frame.

You should move the code that opens and writes to the file to after the "while (window.isOpen())" loop.

Nabeel Nazir

now i am writing this at the end i.e. out of while (window.isOpen()) loop. ... it's still not working .. Please cehck it !



#include <SFML/Audio.hpp>
#include <TGUI/TGUI.hpp>
#include <conio.h>
//#include <cstdio>
#include <fstream>
//void function(const tgui::Callback& callback)
//{
//    if (callback.id == 1)
// {
// //std::cout << "Hello world" << std::endl;
// }
//
//
//
//}

int main()
{

std::list<sf::SoundBuffer> buffers;
std::list<sf::Sound> sounds;

sf::SoundBuffer buffer;
sf::SoundBuffer buffer1;
sf::SoundBuffer buffer2;
sf::SoundBuffer buffer3;
sf::SoundBuffer buffer4;
sf::SoundBuffer buffer5;
sf::Sound sound;

//sound.setBuffer(buffer);
sound.setPitch(1.5f);
sound.setVolume(80);
    // Create the window
    sf::RenderWindow window(sf::VideoMode(800, 600), "Window");
    tgui::Gui gui(window);

std::string name;

tgui::ListBox::Ptr listBox(gui);

std::ifstream ifile;
ifile.open("animals.txt");
if(ifile.is_open())
{
while(!ifile.eof())
{
ifile>>name;
ifile.ignore(10,'\n');
listBox->addItem(name);

}

}
else
{
std::cout<<"ifile.txt does not open properly!"<<std::endl;
}



    // Load the font (you should check the return value to make sure that it is loaded)
    gui.setGlobalFont("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/fonts/DejaVuSans.ttf");



    tgui::Button::Ptr button(gui);
    button->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
button->setPosition(280, 250);
    button->setText("SPEAK");
    button->setSize(200, 40);



listBox->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
listBox->setSize(150, 120);
    listBox->setItemHeight(30);
    listBox->setPosition(300, 30);
//   listBox->addItem("COW");
//   listBox->addItem("CAT");
//   //listBox->addItem("DONKEY");
//listBox->addItem("LION");
//listBox->addItem("GOAT");
listBox->setCallbackId(3);

tgui ::Callback callback;

button->bindCallback(tgui::Button::LeftMouseClicked);
button->setCallbackId(1);

tgui::EditBox::Ptr editBox(gui);
    editBox->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
    editBox->setBorders(6, 4, 6, 4);
    editBox->setPosition(230, 350);
    editBox->setText("Add Animal Name Here!");
editBox->setSize(360, 40);

tgui::Button::Ptr button1(gui);
    button1->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
button1->setPosition(280, 450);
    button1->setText("Add Animal");
    button1->setSize(200, 40);

button1->bindCallback(tgui::Button::LeftMouseClicked);
button1->setCallbackId(2);

buffer.loadFromFile("Lion Roar.wav");
//sound.setBuffer(buffer);
buffer1.loadFromFile("Goat Bah.wav");
//sound.setBuffer(buffer1);
buffer2.loadFromFile("Cat Meow.wav");
buffer3.loadFromFile("Single Cow.wav");
//sound.setBuffer(buffer2);

//   //tgui::Button::Ptr button(gui);
//   button->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black .conf");
//button->setPosition(380, 350);
//   button->setText("Add");
//   button->setSize(200, 40);


    while (window.isOpen())
    {
/////////////////////////////////////////////////////

while (gui.pollCallback(callback))
{

if(callback.id==1)
{
if (listBox->getSelectedItem()=="CAT")
{

sound.setBuffer(buffer2);
sound.play();

}

else if (listBox->getSelectedItem()=="LION")
{

sound.setBuffer(buffer);
sound.play();

}
else if (listBox->getSelectedItem()=="GOAT")
{
sound.setBuffer(buffer1);
sound.play();
}
else if(listBox->getSelectedItem()=="COW")
{
sound.setBuffer(buffer3);
sound.play();

}

}
/*else
{
std::cout<<"id is not valid!!!!"<<std::endl;
}*/
else if(callback.id==2)
{
//editBox->getText();

//listBox->setSelectedItem(listBox->getItems().size()-1); // Select the last item in the list box

name=editBox->getText();
listBox->addItem(name);


/*buffers.push_back(sf::SoundBuffer());
buffers.back().loadFromFile(editBox->getText() + ".wav");

sounds.push_back(sf::Sound());
sounds.back().setBuffer(buffers.back());
sounds.back().setPitch(1.5f);
sounds.back().setVolume(80);

auto it = sounds.begin();
std::advance(it, listBox->getSelectedItemIndex());
it->play();*/


}


}




//////////////////////////////////////////////////////
       
sf::Event event;
        while (window.pollEvent(event))
        {

if (event.type == sf::Event::Closed)
window.close();

// Pass the event to all the widgets (if there would be widgets)
gui.handleEvent(event);
}

        window.clear();

        // Draw all created widgets
        gui.draw();

        window.display();

}

std::ofstream ofile("animals.txt");
if (ifile.is_open())
{
auto& items = listBox->getItems();
for (auto it = items.cbegin(); it != items.cend(); ++it)
{
ofile << it->toAnsiString() << std::endl;
}
}




//getch();
return EXIT_SUCCESS;
}






texus

- When creating the ofile, you are checking if the ifile is open. You should check ofile instead.

- Close ifile when you are done with it. It is automatically closed when going out of scope, but because everything is inside the main function, this will be when the program quits. And you don't want the file to still be open for reading when at the same time opening it for writing.

- The following lines don't work correctly.
while(!ifile.eof())
{
    ifile>>name;


You should change it to the following to get the desired effect.
while(ifile>>name)
{

Nabeel Nazir

thanks it's doing well ... now i have to do last requirement of my assignment ... which is to add a new animal name and press speak button so that the voice of newly added animal will be played automatically ... you gave me piece of code for this but it's giving some kind of debug assertion error at run time when i add new animal in edit box and press add animal button ! here is the code !


#include <SFML/Audio.hpp>
#include <TGUI/TGUI.hpp>
#include <conio.h>
//#include <cstdio>
#include <fstream>
//void function(const tgui::Callback& callback)
//{
//    if (callback.id == 1)
// {
// //std::cout << "Hello world" << std::endl;
// }
//
//
//
//}

int main()
{

std::list<sf::SoundBuffer> buffers;
std::list<sf::Sound> sounds;

sf::SoundBuffer buffer;
sf::SoundBuffer buffer1;
sf::SoundBuffer buffer2;
sf::SoundBuffer buffer3;
sf::SoundBuffer buffer4;
sf::SoundBuffer buffer5;
sf::Sound sound;

//sound.setBuffer(buffer);
sound.setPitch(1.5f);
sound.setVolume(80);
    // Create the window
    sf::RenderWindow window(sf::VideoMode(800, 600), "Window");
    tgui::Gui gui(window);

std::string name;

tgui::ListBox::Ptr listBox(gui);

std::ifstream ifile;
ifile.open("animals.txt");
if(ifile.is_open())
{
while(ifile>>name)
{

ifile.ignore(10,'\n');
listBox->addItem(name);

}

}
else
{
std::cout<<"ifile.txt does not open properly!"<<std::endl;
}
ifile.close();


    // Load the font (you should check the return value to make sure that it is loaded)
    gui.setGlobalFont("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/fonts/DejaVuSans.ttf");



    tgui::Button::Ptr button(gui);
    button->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
button->setPosition(280, 250);
    button->setText("SPEAK");
    button->setSize(200, 40);



listBox->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
listBox->setSize(150, 120);
    listBox->setItemHeight(30);
    listBox->setPosition(300, 30);
//   listBox->addItem("COW");
//   listBox->addItem("CAT");
//   //listBox->addItem("DONKEY");
//listBox->addItem("LION");
//listBox->addItem("GOAT");
listBox->setCallbackId(3);

tgui ::Callback callback;

button->bindCallback(tgui::Button::LeftMouseClicked);
button->setCallbackId(1);

tgui::EditBox::Ptr editBox(gui);
    editBox->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
    editBox->setBorders(6, 4, 6, 4);
    editBox->setPosition(230, 350);
    editBox->setText("Add Animal Name Here!");
editBox->setSize(360, 40);

tgui::Button::Ptr button1(gui);
    button1->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
button1->setPosition(280, 450);
    button1->setText("Add Animal");
    button1->setSize(200, 40);

button1->bindCallback(tgui::Button::LeftMouseClicked);
button1->setCallbackId(2);

buffer.loadFromFile("Lion Roar.wav");
//sound.setBuffer(buffer);
buffer1.loadFromFile("Goat Bah.wav");
//sound.setBuffer(buffer1);
buffer2.loadFromFile("Cat Meow.wav");
buffer3.loadFromFile("Single Cow.wav");
//sound.setBuffer(buffer2);

//   //tgui::Button::Ptr button(gui);
//   button->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black .conf");
//button->setPosition(380, 350);
//   button->setText("Add");
//   button->setSize(200, 40);


    while (window.isOpen())
    {
/////////////////////////////////////////////////////

while (gui.pollCallback(callback))
{

if(callback.id==1)
{
if (listBox->getSelectedItem()=="CAT")
{

sound.setBuffer(buffer2);
sound.play();

}

else if (listBox->getSelectedItem()=="LION")
{

sound.setBuffer(buffer);
sound.play();

}
else if (listBox->getSelectedItem()=="GOAT")
{
sound.setBuffer(buffer1);
sound.play();
}
else if(listBox->getSelectedItem()=="COW")
{
sound.setBuffer(buffer3);
sound.play();

}

}
/*else
{
std::cout<<"id is not valid!!!!"<<std::endl;
}*/
else if(callback.id==2)
{
//editBox->getText();

//listBox->setSelectedItem(listBox->getItems().size()-1); // Select the last item in the list box

name=editBox->getText();
listBox->addItem(name);


buffers.push_back(sf::SoundBuffer());
buffers.back().loadFromFile(editBox->getText() + ".wav");

sounds.push_back(sf::Sound());
sounds.back().setBuffer(buffers.back());
sounds.back().setPitch(1.5f);
sounds.back().setVolume(80);

auto it = sounds.begin();
std::advance(it, listBox->getSelectedItemIndex());
it->play();


}


}




//////////////////////////////////////////////////////
       
sf::Event event;
        while (window.pollEvent(event))
        {

if (event.type == sf::Event::Closed)
window.close();

// Pass the event to all the widgets (if there would be widgets)
gui.handleEvent(event);
}

        window.clear();

        // Draw all created widgets
        gui.draw();

        window.display();

}

std::ofstream ofile("animals.txt");
if (ofile.is_open())
{
auto& items = listBox->getItems();
for (auto it = items.cbegin(); it != items.cend(); ++it)
{
ofile << it->toAnsiString() << std::endl;
}
}
else
{
std::cout<<"ofile does not open properly !"<<std::endl;
}
ofile.close();



//getch();
return EXIT_SUCCESS;
}






texus

Ok, I figured out what is happening. You are loading stuff so you are adding items to the list box. But no sound is being loaded that belongs to these items. So the amount of items in the list box will be bigger than the amount of loaded sounds.

So instead of just adding the read line to the list box, you should also load the corresponding sound.
while(ifile>>name)
{
    ifile.ignore(10,'\n');

    // Do something here similar to what you do when pressing the 'add animal' buttons.
}


texus

You don't just have to load the animal names from the text file, you also need to load the sound that belongs to the animal.

Something like this (untested)
while(ifile>>name)
{
    ifile.ignore(10,'\n');
    listBox->addItem(name);

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

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

Nabeel Nazir

hey please check what i am doing wrong ! i am adding DUCK as a new animal and it will add in listBox but when i select DUCK and pressing speak button , its not playing the voice of DUCK !


#include <SFML/Audio.hpp>
#include <TGUI/TGUI.hpp>
#include <conio.h>
//#include <cstdio>
#include <fstream>
//void function(const tgui::Callback& callback)
//{
//    if (callback.id == 1)
// {
// //std::cout << "Hello world" << std::endl;
// }
//
//
//
//}

int main()
{

std::list<sf::SoundBuffer> buffers;
std::list<sf::Sound> sounds;

sf::SoundBuffer buffer;
sf::SoundBuffer buffer1;
sf::SoundBuffer buffer2;
sf::SoundBuffer buffer3;
sf::SoundBuffer buffer4;
sf::SoundBuffer buffer5;
sf::Sound sound;

//sound.setBuffer(buffer);
sound.setPitch(1.5f);
sound.setVolume(80);
    // Create the window
    sf::RenderWindow window(sf::VideoMode(800, 600), "Window");
    tgui::Gui gui(window);

std::string name;

tgui::ListBox::Ptr listBox(gui);

tgui::EditBox::Ptr editBox(gui);
std::ifstream ifile;
ifile.open("animals.txt");
if(ifile.is_open())
{
while(ifile>>name)
{

ifile.ignore(10,'\n');
listBox->addItem(name);

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

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

}

}
else
{
std::cout<<"ifile.txt does not open properly!"<<std::endl;
}
ifile.close();


    // Load the font (you should check the return value to make sure that it is loaded)
    gui.setGlobalFont("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/fonts/DejaVuSans.ttf");



    tgui::Button::Ptr button(gui);
    button->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
button->setPosition(280, 250);
    button->setText("SPEAK");
    button->setSize(200, 40);



listBox->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
listBox->setSize(150, 120);
    listBox->setItemHeight(30);
    listBox->setPosition(300, 30);
//   listBox->addItem("COW");
//   listBox->addItem("CAT");
//   //listBox->addItem("DONKEY");
//listBox->addItem("LION");
//listBox->addItem("GOAT");
listBox->setCallbackId(3);

tgui ::Callback callback;

button->bindCallback(tgui::Button::LeftMouseClicked);
button->setCallbackId(1);


    editBox->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
    editBox->setBorders(6, 4, 6, 4);
    editBox->setPosition(230, 350);
    editBox->setText("Add Animal Name Here!");
editBox->setSize(360, 40);

tgui::Button::Ptr button1(gui);
    button1->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
button1->setPosition(280, 450);
    button1->setText("Add Animal");
    button1->setSize(200, 40);

button1->bindCallback(tgui::Button::LeftMouseClicked);
button1->setCallbackId(2);

buffer.loadFromFile("LION.wav");
//sound.setBuffer(buffer);
buffer1.loadFromFile("GOAT.wav");
//sound.setBuffer(buffer1);
buffer2.loadFromFile("CAT.wav");
buffer3.loadFromFile("COW.wav");
//sound.setBuffer(buffer2);

//   //tgui::Button::Ptr button(gui);
//   button->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black .conf");
//button->setPosition(380, 350);
//   button->setText("Add");
//   button->setSize(200, 40);


    while (window.isOpen())
    {
/////////////////////////////////////////////////////

while (gui.pollCallback(callback))
{

if(callback.id==1)
{
if (listBox->getSelectedItem()=="CAT")
{

sound.setBuffer(buffer2);
sound.play();

}

else if (listBox->getSelectedItem()=="LION")
{

sound.setBuffer(buffer);
sound.play();

}
else if (listBox->getSelectedItem()=="GOAT")
{
sound.setBuffer(buffer1);
sound.play();
}
else if(listBox->getSelectedItem()=="COW")
{
sound.setBuffer(buffer3);
sound.play();

}

}
/*else
{
std::cout<<"id is not valid!!!!"<<std::endl;
}*/
else if(callback.id==2)
{
//editBox->getText();

//listBox->setSelectedItem(listBox->getItems().size()-1); // Select the last item in the list box

name=editBox->getText();
listBox->addItem(name);


/*buffers.push_back(sf::SoundBuffer());
buffers.back().loadFromFile(editBox->getText() + ".wav");

sounds.push_back(sf::Sound());
sounds.back().setBuffer(buffers.back());
sounds.back().setPitch(1.5f);
sounds.back().setVolume(80);

auto it = sounds.begin();
std::advance(it, listBox->getSelectedItemIndex());
it->play();
*/

}


}




//////////////////////////////////////////////////////
       
sf::Event event;
        while (window.pollEvent(event))
        {

if (event.type == sf::Event::Closed)
window.close();

// Pass the event to all the widgets (if there would be widgets)
gui.handleEvent(event);
}

        window.clear();

        // Draw all created widgets
        gui.draw();

        window.display();

}

std::ofstream ofile("animals.txt");
if (ofile.is_open())
{
auto& items = listBox->getItems();
for (auto it = items.cbegin(); it != items.cend(); ++it)
{
ofile << it->toAnsiString() << std::endl;
}
}
else
{
std::cout<<"ofile does not open properly !"<<std::endl;
}
ofile.close();



//getch();
return EXIT_SUCCESS;
}






texus

The "if(callback.id==1)" part in your code still contains old code. You should clean up your code a bit more, because you aren't using half of the code anymore.

You now simply need something like this:
if(callback.id==1)
{
    auto it = sounds.begin();
    std::advance(it, listBox->getSelectedItemIndex());
    it->play();
}


Nabeel Nazir

When i am adding an animal through eidtBox and addbutton this will add animal in the list box .. but not playing the voice of animal when selected and speak button is pressed ! this is giving some kind of error!


#include <SFML/Audio.hpp>
#include <TGUI/TGUI.hpp>
#include <fstream>

int main()
{

std::list<sf::SoundBuffer> buffers;
std::list<sf::Sound> sounds;

sf::SoundBuffer buffer;
sf::SoundBuffer buffer1;
sf::SoundBuffer buffer2;
sf::SoundBuffer buffer3;
sf::SoundBuffer buffer4;
sf::SoundBuffer buffer5;
sf::Sound sound;

sound.setPitch(1.5f);
sound.setVolume(80);
    sf::RenderWindow window(sf::VideoMode(800, 600), "Window");
    tgui::Gui gui(window);

std::string name;

tgui::ListBox::Ptr listBox(gui);

tgui::EditBox::Ptr editBox(gui);
std::ifstream ifile;
ifile.open("animals.txt");
if(ifile.is_open())
{
while(ifile>>name)
{

ifile.ignore(10,'\n');
listBox->addItem(name);

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

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

}

}
else
{
std::cout<<"ifile.txt does not open properly!"<<std::endl;
}
ifile.close();


    // Load the font (you should check the return value to make sure that it is loaded)
    gui.setGlobalFont("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/fonts/DejaVuSans.ttf");



    tgui::Button::Ptr button(gui);
    button->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
button->setPosition(280, 250);
    button->setText("SPEAK");
    button->setSize(200, 40);



listBox->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
listBox->setSize(150, 120);
    listBox->setItemHeight(30);
    listBox->setPosition(300, 30);
//   listBox->addItem("COW");
//   listBox->addItem("CAT");
//   //listBox->addItem("DONKEY");
//listBox->addItem("LION");
//listBox->addItem("GOAT");
listBox->setCallbackId(3);

tgui ::Callback callback;

button->bindCallback(tgui::Button::LeftMouseClicked);
button->setCallbackId(1);


    editBox->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
    editBox->setBorders(6, 4, 6, 4);
    editBox->setPosition(230, 350);
    editBox->setText("Add Animal Name Here!");
editBox->setSize(360, 40);

tgui::Button::Ptr button1(gui);
    button1->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
button1->setPosition(280, 450);
    button1->setText("Add Animal");
    button1->setSize(200, 40);

button1->bindCallback(tgui::Button::LeftMouseClicked);
button1->setCallbackId(2);

buffer.loadFromFile("LION.wav");
//sound.setBuffer(buffer);
buffer1.loadFromFile("GOAT.wav");
//sound.setBuffer(buffer1);
buffer2.loadFromFile("CAT.wav");
buffer3.loadFromFile("COW.wav");
//sound.setBuffer(buffer2);

//   //tgui::Button::Ptr button(gui);
//   button->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black .conf");
//button->setPosition(380, 350);
//   button->setText("Add");
//   button->setSize(200, 40);


    while (window.isOpen())
    {
/////////////////////////////////////////////////////

while (gui.pollCallback(callback))
{

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


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

sounds.push_back(sf::Sound());
sounds.back().setBuffer(buffers.back());
*/
}
/*if (listBox->getSelectedItem()=="CAT")
{

sound.setBuffer(buffer2);
sound.play();

}

else if (listBox->getSelectedItem()=="LION")
{

sound.setBuffer(buffer);
sound.play();

}
else if (listBox->getSelectedItem()=="GOAT")
{
sound.setBuffer(buffer1);
sound.play();
}
else if(listBox->getSelectedItem()=="COW")
{
sound.setBuffer(buffer3);
sound.play();

}*/

//}
/*else
{
std::cout<<"id is not valid!!!!"<<std::endl;
}*/
if(callback.id==2)
{
//editBox->getText();

//listBox->setSelectedItem(listBox->getItems().size()-1); // Select the last item in the list box

name=editBox->getText();
listBox->addItem(name);

/*buffers.push_back(sf::SoundBuffer());
buffers.back().loadFromFile(editBox->getText() + ".wav");

sounds.push_back(sf::Sound());
sounds.back().setBuffer(buffers.back());
sounds.back().setPitch(1.5f);
sounds.back().setVolume(80);

auto it = sounds.begin();
std::advance(it, listBox->getSelectedItemIndex());
it->play();
*/

}


}
       
sf::Event event;
        while (window.pollEvent(event))
        {

if (event.type == sf::Event::Closed)
window.close();

// Pass the event to all the widgets (if there would be widgets)
gui.handleEvent(event);
}

        window.clear();

        // Draw all created widgets
        gui.draw();

        window.display();

}

std::ofstream ofile("animals.txt");
if (ofile.is_open())
{
auto& items = listBox->getItems();
for (auto it = items.cbegin(); it != items.cend(); ++it)
{
ofile << it->toAnsiString() << std::endl;
}
}
else
{
std::cout<<"ofile does not open properly !"<<std::endl;
}
ofile.close();

return EXIT_SUCCESS;
}






texus

You commented out too much code when callback.id is 2. You should still add the sound to the list when clicking the 'add animal' button.

I removed some of the commented parts during searching for the mistake.
#include <SFML/Audio.hpp>
#include <TGUI/TGUI.hpp>
#include <fstream>

int main()
{
std::list<sf::SoundBuffer> buffers;
std::list<sf::Sound> sounds;

    sf::RenderWindow window(sf::VideoMode(800, 600), "Window");
    tgui::Gui gui(window);

std::string name;

tgui::ListBox::Ptr listBox(gui);

tgui::EditBox::Ptr editBox(gui);
std::ifstream ifile;
ifile.open("animals.txt");
if(ifile.is_open())
{
while(ifile>>name)
{

ifile.ignore(10,'\n');
listBox->addItem(name);

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

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

}

}
else
{
std::cout<<"ifile.txt does not open properly!"<<std::endl;
}
ifile.close();


    // Load the font (you should check the return value to make sure that it is loaded)
    gui.setGlobalFont("TGUI/fonts/DejaVuSans.ttf");


    tgui::Button::Ptr button(gui);
    button->load("TGUI/widgets/Black.conf");
button->setPosition(280, 250);
    button->setText("SPEAK");
    button->setSize(200, 40);


listBox->load("TGUI/widgets/Black.conf");
listBox->setSize(150, 120);
    listBox->setItemHeight(30);
    listBox->setPosition(300, 30);
listBox->setCallbackId(3);

tgui ::Callback callback;

button->bindCallback(tgui::Button::LeftMouseClicked);
button->setCallbackId(1);


    editBox->load("TGUI/widgets/Black.conf");
    editBox->setBorders(6, 4, 6, 4);
    editBox->setPosition(230, 350);
    editBox->setText("Add Animal Name Here!");
editBox->setSize(360, 40);

tgui::Button::Ptr button1(gui);
    button1->load("TGUI/widgets/Black.conf");
button1->setPosition(280, 450);
    button1->setText("Add Animal");
    button1->setSize(200, 40);

button1->bindCallback(tgui::Button::LeftMouseClicked);
button1->setCallbackId(2);


    while (window.isOpen())
    {
/////////////////////////////////////////////////////

while (gui.pollCallback(callback))
{

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

if(callback.id==2)
{
name=editBox->getText();
listBox->addItem(name);

buffers.push_back(sf::SoundBuffer());
buffers.back().loadFromFile(editBox->getText() + ".wav");

sounds.push_back(sf::Sound());
sounds.back().setBuffer(buffers.back());
sounds.back().setPitch(1.5f);
sounds.back().setVolume(80);
/*
auto it = sounds.begin();
std::advance(it, listBox->getSelectedItemIndex());
it->play();
*/

}


}

sf::Event event;
        while (window.pollEvent(event))
        {

if (event.type == sf::Event::Closed)
window.close();

// Pass the event to all the widgets (if there would be widgets)
gui.handleEvent(event);
}

        window.clear();

        // Draw all created widgets
        gui.draw();

        window.display();

}

std::ofstream ofile("animals.txt");
if (ofile.is_open())
{
auto& items = listBox->getItems();
for (auto it = items.cbegin(); it != items.cend(); ++it)
{
ofile << it->toAnsiString() << std::endl;
}
}
else
{
std::cout<<"ofile does not open properly !"<<std::endl;
}
ofile.close();

return EXIT_SUCCESS;
}

Nabeel Nazir

i have copied your code in last reply  ... nothing is going so well ... :(

texus

I can't test it now, I'm not at home.

Try with your old code and look at what I changed in the part where callback.id is 2.
Maybe I just removed a little too much.


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();
}