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.
I removed some of the commented parts during searching for the mistake.
Code Select
#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;
}