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

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.