why this code is not working , i want to add a button into my window !

Started by Nabeel Nazir, 08 September 2013, 18:56:34

Nabeel Nazir



#include <SFML/window.hpp>
#include <SFML/system.hpp>
#include <SFML/audio.hpp>
#include <SFML/graphics.hpp>
#include <TGUI/TGUI.hpp>
//#include <SFML/Video.hpp>
//#include <conio.h>

//#include <iostream>

int main()
{
    // 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");

    while (window.isOpen())
    {
        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();

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/Button/BabyBlue/Normal.conf");

        window.display();
   








}
return EXIT_SUCCESS;
}



:(



Nabeel Nazir

Please also tell me how can i make list Box in this code ???
and secondly why font file is not loading in this code ???? I need help  ! :(

texus

You are creating a button every frame. You should only create it once.
You should also call the window.clear function (you should uncomment it). In sfml you must redraw every frame entirely.

The code should look more like this:
#include <SFML/Audio.hpp>
#include <TGUI/TGUI.hpp>
#include <conio.h>

int main()
{
    // 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("TGUI/fonts/DejaVuSans.ttf");

    tgui::Button::Ptr button(gui);
    button->load("TGUI/widgets/Black.conf");

    while (window.isOpen())
    {
        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();
}
return EXIT_SUCCESS;
}



What errors are you getting in the command prompt?

To create a list box, just do like you would create a button:
tgui::ListBox::Ptr listBox(gui);
listBox->load("TGUI/widgets/Black.conf");

And then use setSize to give it the correct size, addItem function to add items to it, ...
Here is the list of other function you can call on the listbox: https://tgui.eu/dcmtn/v0.6/classtgui_1_1ListBox.html.

Nabeel Nazir


texus

Obviously your program should be able to find the files.

It is looking for "TGUI/fonts/DejaVuSans.ttf" and if I am not mistaking then VS look for it next to the executable. So you need a folder called TGUI next to your executable (in the Debug or Release folder). Or use a full path like you were using, but then the program doesn't work on other computers.

Edit: In your current setup it should be something like "../TGUI-0.6-alpha2/fonts/DejaVuSans.ttf" at the moment.

texus

The error is quite clear, so I can only ask one thing: "Are you 100% sure that that file exists?"

Try with "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", which is the default style and should work. If that works then you can try with creating your own config file. But if this is just a small test project then you might want to keep the default Black style until you have more experience with tgui.

Edit: Did you just delete your previous post?

Nabeel Nazir

thanks a lot ... now its working ! :))) !
that's why i remove other reply's !
yeah this is a small project given by my university professor...
this project is simple but complex and i am sure you guys will help me in completing this task :))
the project is about to create a list box which contains different animal names .. and a button.. when a particular name of animal  is selected .. and button is pressed then voice of that animal should be played ! 

texus

Good to hear that it is working now.

But I changed my forum settings now so you can no longer delete posts, its really confusing if posts are suddenly missing.

Nabeel Nazir

I have made the button and list box .. now its time to move on to the next step !
is there any way to add a ".wav" sound file in this code ? so that when a particular animal is selected and speak button is pressed then the selected animal's voice will be played ? i need help !

texus

I think I can write your assignment in less than 100 lines, so I can't give to many details (otherwise I will be making half of your assignment).

You need to load the sounds with sfml.
Then insert the names of the animals into the list box with the addItem function.
When receiving callback from the button (see part 3 of introcuction tutorial for more information about callbacks), you should check which animal is selected with the getSelectedItem function. And depending on the string that the function returns you should play the corresponding sound.

Nabeel Nazir

Re:
#10
thanks ! :)