SFML Buttons -Question-

I've just learned the basics of SFML library (how to use it, in general way).
I saw the samples on the package of the library (those demonstation-programs). The thing is that I want to put a button on a window without using Windows API. I saw the code of that sample: This is it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include <cmath>

HWND Button;


////////////////////////////////////////////////////////////
/// Function called whenever one of our windows receives a message
///
////////////////////////////////////////////////////////////
LRESULT CALLBACK OnEvent(HWND Handle, UINT Message, WPARAM WParam, LPARAM LParam)
{
    switch (Message)
    {
        // Quit when we close the main window
        case WM_CLOSE :
        {
            PostQuitMessage(0);
            return 0;
        }

        // Quit when we click the "quit" button
        case WM_COMMAND :
        {
            if (reinterpret_cast<HWND>(LParam) == Button)
            {
                PostQuitMessage(0);
                return 0;
            }
        }
    }

    return DefWindowProc(Handle, Message, WParam, LParam);
}


////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \param Instance : Instance of the application
///
/// \return Error code
///
////////////////////////////////////////////////////////////
INT WINAPI WinMain(HINSTANCE Instance, HINSTANCE, LPSTR, INT)
{
    // Define a class for our main window
    WNDCLASS WindowClass;
    WindowClass.style         = 0;
    WindowClass.lpfnWndProc   = &OnEvent;
    WindowClass.cbClsExtra    = 0;
    WindowClass.cbWndExtra    = 0;
    WindowClass.hInstance     = Instance;
    WindowClass.hIcon         = NULL;
    WindowClass.hCursor       = 0;
    WindowClass.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_BACKGROUND);
    WindowClass.lpszMenuName  = NULL;
    WindowClass.lpszClassName = TEXT("SFML App");
    RegisterClass(&WindowClass);

    // Let's create the main window
    HWND Window = CreateWindow(TEXT("SFML App"), TEXT("SFML Win32"), WS_SYSMENU | WS_VISIBLE, 200, 200, 660, 520, NULL, NULL, Instance, NULL);

    // Add a button for exiting
    Button = CreateWindow(TEXT("BUTTON"), TEXT("Quit"), WS_CHILD | WS_VISIBLE, 560, 440, 80, 40, Window, NULL, Instance, NULL);

    // Let's create two SFML views
    HWND View1 = CreateWindow(TEXT("STATIC"), NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, 20,  20, 300, 400, Window, NULL, Instance, NULL);
    HWND View2 = CreateWindow(TEXT("STATIC"), NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, 340, 20, 300, 400, Window, NULL, Instance, NULL);
    sf::RenderWindow SFMLView1(View1);
    sf::RenderWindow SFMLView2(View2);

    // Load some images to display
    sf::Image Image1, Image2;
    if (!Image1.LoadFromFile("datas/win32/image1.jpg") || !Image2.LoadFromFile("datas/win32/image2.jpg"))
        return EXIT_FAILURE;
    sf::Sprite Sprite1(Image1);
    sf::Sprite Sprite2(Image2);
    Sprite1.SetCenter(Sprite1.GetSize() / 2.f);

    // Create a clock for measuring elapsed time
    sf::Clock Clock;

    // Loop until a WM_QUIT message is received
    MSG Message;
    Message.message = static_cast<UINT>(~WM_QUIT);
    while (Message.message != WM_QUIT)
    {
        if (PeekMessage(&Message, NULL, 0, 0, PM_REMOVE))
        {
            // If a message was waiting in the message queue, process it
            TranslateMessage(&Message);
            DispatchMessage(&Message);
        }
        else
        {
            // Clear views
            SFMLView1.Clear();
            SFMLView2.Clear();

            // Draw sprite 1 on view 1
            Sprite1.SetRotation(Clock.GetElapsedTime() * 100);
            SFMLView1.Draw(Sprite1);

            // Draw sprite 2 on view 2
            Sprite2.SetX(cos(Clock.GetElapsedTime()) * 100);
            SFMLView2.Draw(Sprite2);

            // Display each view on screen
            SFMLView1.Display();
            SFMLView2.Display();
        }
    }

    // Destroy the main window (all its child controls will be destroyed)
    DestroyWindow(Window);

    // Don't forget to unregister the window class
    UnregisterClass(TEXT("SFML App"), Instance);

    return EXIT_SUCCESS;
}
. This code displays on the screen a window split in 2. In both sides is the same image but it moves different. Down the window is a button "Quit". If I press that button, it closes the program.
Now, how can I do this without using WINAPI? The right question is if I can do this using the functions in SFML.
For instance, what can I put a button in the window created by the program below?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<SFML/Graphics.hpp>


int main()
{
    sf::RenderWindow wnd(sf::VideoMode(400, 500), "Window");
    while(wnd.IsOpened())
    {
        sf::Event ev;
        while(wnd.GetEvent(ev))
        {
            if(ev.Type==sf::Event::Closed)
            wnd.Close();
        }
        wnd.Clear();
        wnd.Display();
    }
    return EXIT_SUCCESS;
}

Thanks.
Hmm.

Here's what I suggest you do:
1.Load A Button Image(Position It)
2.Create a function that detects if the mouse cursor is over your button/image.
3. Make an if statement that detects if the mouse cursor is over the image/button, and detects whether the user has clicked it(sf::Mouse).

It's a good idea, a very good idea. Thanks. So when I click the image, is like I click on a button.
But in SFML doesn't exist a function that makes a button? Like Button function in WINAPI. (Just asking)
Ha, of course not.

SFML doesn't have a Button function. You can make one. Just follow those steps and you'll be fine. And another thing, when you create the image, make sure to auto crop it.
Allright! Thank you very much for help!
=] No problem. You did all the work(lol).
Look...hmm I fell a bit embarassed but I really want to do this program so I have to ask you a last one thing:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<SFML/Graphics.hpp>
#include<SFML/Audio.hpp>

int main()
{
    sf::RenderWindow wnd(sf::VideoMode(400, 500), "Window");
    sf::Image img;
    sf::Music msc;
    if(msc.OpenFromFile("rds.ogg"))
    return EXIT_FAILURE;
    if(!img.LoadFromFile("imgs.jpg"))
    return EXIT_FAILURE;
    sf::Sprite spr(img);
    spr.SetPosition(300.f, 300.f);
    while(wnd.IsOpened())
    {
        sf::Event ev;
        while(wnd.GetEvent(ev))
        {
            if(ev.Type==sf::Event::Closed)
            wnd.Close();
        }
        if(((wnd.GetInput().GetMouseX())==300.f)&&((wnd.GetInput().GetMouseY())==300.f))
        {
            if(wnd.GetInput().IsMouseButtonDown(sf::Mouse::Left))
            msc.Play();
        }
        wnd.Clear();
        wnd.Draw(spr);
        wnd.Display();
    }
    return EXIT_SUCCESS;
}
What am I doing wrong here? It just appear and disappear quickly like that old programs in Borland. I try to find the coodronates of the mouse by using the functions GetMouseX() and GetMouseY() and if is the left mouse button pressed, to play that music.
Thanks.
Last edited on
1
2
    if(msc.OpenFromFile("rds.ogg"))
    return EXIT_FAILURE;


If you have an rds.ogg, you are exiting the program bnefore you even get off the ground.
Topic archived. No new replies allowed.