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, now i have to add an edit box and a button namely "Add Animal " , my requirement is to write any name of animal in the edit box and press "Add Animal " button and that particular animal should be added in the list box ! ... ALSO--> is there any way that when new animal will be added it's voice i.e.  .wav file will be added automatically at back end and when speak button is pressed it's voice will be played ?

texus

Adding an edit box is the same as adding a button or a list box:
tgui::EditBox::Ptr editBox(gui);
editBox->load("...");

And then look at the documentation for the other functions you can use. But you probably only need setPosition and setSize.

When the "Add animal" button gets pressed, you can use the addItem function to add the contents of the edit box to the list box (editBox->getText() for getting text in edit box).

The animal will be added to the back of the list box, but it still has to be selected.
listBox->setSelectedItem(listBox->getItems().size()-1); // Select the last item in the list box

----------
What follows below is a suggestion of what you can do to play the sound. I didn't test it as I wrote it so it might contain mistakes, and it definately isn't the only way. But you might get some ideas from it.

Now you can even make the sound load dynamically. At the beginning of the program you define this:
std::list<sf::SoundBuffer> buffers;
std::list<sf::Sound> sounds;


When adding a new item to the list box, e.g. with the "Add animal" button then you load the sound. This has the limitation that the sound needs to have the exact name as the animal though (if CAT is entered in the edit box then it will load CAT.wav).
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);


And then when the speak button is pressed, you play the correct sound. There are just as many sounds as there are items in the list box, and e.g. the third item in the list box matches the third sound in the list, so you can use listBox->getSelectedItemIndex().
auto it = sounds.begin();
std::advance(it, listBox->getSelectedItemIndex());
it->play();

Nabeel Nazir

what i am doing wrong here, i am adding the new animal in the edit box and when press add animal button it will be added in the list (good with it !) but when i close my window and compile my code again it will not show the recently added animal in the list box !

#include <SFML/Audio.hpp>
#include <TGUI/TGUI.hpp>
#include <conio.h>

//void function(const tgui::Callback& callback)
//{
//    if (callback.id == 1)
// {
// //std::cout << "Hello world" << std::endl;
// }
//
//
//
//}

int main()
{
char name[50];
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);

    // 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);


tgui::ListBox::Ptr listBox(gui);
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(2);

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->addItem(editBox->getText());
listBox->setSelectedItem(listBox->getItems().size()-1); // Select the last item in the list box
}

}


//////////////////////////////////////////////////////
       
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

Quotebut when i close my window and compile my code again it will not show the recently added animal in the list box !
Obviously. What you want to do is save a the contents of the list in a text file and load it again when starting the program (look at std::ifstream and std::ofstream).


Nabeel Nazir

can i store the selected item from listBox in a character array or any character variable so that i may pass this character array in addItem() function of listBox, and it will store new animal name in the listBox !?

texus

Quotehmmm, i didn't get you !
You want to be able to keep your added animals even after the program was quit, right?

So at the beginning of the program you have something like this:
std::ifstream ifile("animals.txt");
if (ifile.is_open())
{
    // Read the contents of the file and put the read animals in the listbox with addItem
}


At the end of the program you save the animals that you have in your listbox to a file, so that the next time you start the program you will still have all animals.
std::ofstream ofile("animals.txt");
if (ifile.is_open())
{
    auto& items = listBox->getItems();
    for (auto& item : items)
    {
        ofile << item.toAnsiString() << std::endl;
    }
}


Again, this code is only for guidance and may contain mistakes. I also deliberately left out the way to load the text file so that I don't give you the complete solution.

Edit: 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;
}

texus

Quotecan i store the selected item from listBox in a character array or any character variable so that i may pass this character array in addItem() function of listBox, and it will store new animal name in the listBox !?
I have no idea what you mean with this, but lets break it down in smaller parts.

Quotecan i store the selected item from listBox in a character array
std::string selectedItem = listBox->getSelectedItem();

Quoteso that i may pass this character array in addItem() function of listBox
listBox->addItem(selectedItem);

Nabeel Nazir

sorry i wrote it little bit wrong i should have ask like this "can i store the animal name written in editBox in a string and then pass this string to addItem() function of listBox so that it may be permanently store in the listBox as an item" !
I do it like this in my code , but its not saving the item in the listBox permanently i.e. when compiling my code second time the newly added animal name disappears from listBox ! ...


#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::string name;
/*tgui::ListBox::Ptr listBox(gui);
std::string name;
std::ifstream ifile("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;
}*/
//std::string name;
/*int n=20;
char *name=new char[n];*/
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);

    // 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);


tgui::ListBox::Ptr listBox(gui);
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(2);

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())
{

while(!ofile.eof())
{
ofile << .toAnsiString() << std::endl;
}
}*/





//getch();
return EXIT_SUCCESS;
}





Nabeel Nazir

Second solution you told me was difficult for me to code .. i have to complete this assignment as soon as possible .. is there any way other then file handling! ?
You can see in my above code i tried it (reading/writing from file i.e. file handling).. But it was not running well!

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