Menu

Show posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Show posts Menu

Messages - Nabeel Nazir

#1
Help requests / Re:
01 October 2013, 00:09:55
what advance function actually do ?
Please reply ASAP !

if(callback.id==1)
{
auto it = sounds.begin();
std::advance(it, listBox->getSelectedItemIndex());
it->play();
}
#2
Help requests / Re:
28 September 2013, 21:19:56
kindly explain auto keyword and toAnsiString() function more because i never use them ever before !
#3
Help requests / Re:
28 September 2013, 11:46:54
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;
}

#5
i have copied your code in last reply  ... nothing is going so well ... :(
#6
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;
}





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





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





#11
Help requests / Re:
17 September 2013, 07:29:53
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;
}





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




#14
Help requests / Re:
16 September 2013, 19:18:32
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;
    }
}

   
#15
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!