Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to make dialog boxes with sfml in a simple way?  (Read 6108 times)

0 Members and 2 Guests are viewing this topic.

mmeddyy

  • Newbie
  • *
  • Posts: 4
    • View Profile
How to make dialog boxes with sfml in a simple way?
« on: January 03, 2019, 05:31:01 pm »
Hello
I'm using sfml 2.5.1 with c++, and I'm really new to it. My problem is that I don't know how to create dialog boxes. I have been searching about this online, but I can't quite get how to do it. Could anyone tell me a simple way to create dialog boxes or tell me where to find related tutorials? Thank you!

p.s. something similar to the attached picture would be pretty fine for me


Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: How to make dialog boxes with sfml in a simple way?
« Reply #1 on: January 03, 2019, 05:53:21 pm »
The simplest way to recreate something like your attached screenshot would be to just use a sf::RectangleShape to draw the dialog box background, and then use sf::Text to draw the words on top of it.

If you need something more advanced than that you may need to be more specific with your question.

mmeddyy

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: How to make dialog boxes with sfml in a simple way?
« Reply #2 on: January 04, 2019, 04:07:38 am »
The simplest way to recreate something like your attached screenshot would be to just use a sf::RectangleShape to draw the dialog box background, and then use sf::Text to draw the words on top of it.

If you need something more advanced than that you may need to be more specific with your question.

Thanks, I'll try it.

mmeddyy

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: How to make dialog boxes with sfml in a simple way?
« Reply #3 on: January 04, 2019, 03:50:59 pm »
Now, I have another question.
How to deal with  "both" KeyPressed and TextEntered in the while loop? It seems that when there is  case sf::Event::TextEntered: , case sf::Event::KeyPressed: will not be detected. Could anyone help me with this, please? Thank you.


 while (window.pollEvent(event))
        {
            switch(event.type)
            {
            case(sf::Event::Closed):
                window.close();
                break;
            case sf::Event::KeyPressed:
                if(event.key.code == sf::Keyboard::Q)
                {
                    sd.play();
                }
                else if(event.key.code == sf::Keyboard::P)
                {
                    if(music.getStatus() == sf::Sound::Playing)
                        music.pause();
                    else
                        music.play();
                }
           break;
           
            case sf::Event::TextEntered:
                    if(event.text.unicode >= 32 && event.text.unicode <= 126)
                        sentence += static_cast<char>(event.text.unicode);
                    else if(event.text.unicode == 8 && sentence.getSize() > 0)
                        sentence.erase(sentence.getSize() - 1, sentence.getSize());

                    text.setString(sentence);
             break;
            }
        }

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: How to make dialog boxes with sfml in a simple way?
« Reply #4 on: January 04, 2019, 07:42:05 pm »
What do you mean by that? For each key press you get a KeyPressed and for text entered you get TextEntered (sometimes it takes more key presses to enter text and text entered interprets the pressed keys into a unicode character for you).
Back to C++ gamedev with SFML in May 2023

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to make dialog boxes with sfml in a simple way?
« Reply #5 on: January 05, 2019, 12:00:45 am »
Shouldn't you get both (keypressed and textentered) events when you press a "simple" key?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

mmeddyy

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: How to make dialog boxes with sfml in a simple way?
« Reply #6 on: January 05, 2019, 03:23:56 am »
I mean that in some situation I want to press a key to do something, but how does the program know that I'm just pressing a key not entering texts if both events happen? And in other situations, I need to input texts.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to make dialog boxes with sfml in a simple way?
« Reply #7 on: January 05, 2019, 04:45:02 pm »
You'll always get all events that match what you did (so pressing 't' for example will trigger both KeyPressed and TextEntered events). It's then your code that must choose which are relevant, according to the current context. If you're writing some kind of UI controls, then the key point is focus, ie. keyboard events are sent to the focused control, and then which events are used and which are ignored, depends on what kind of control it is, how it is configured, etc.
Laurent Gomila - SFML developer

 

anything