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

Author Topic: Getting user input and displaying formatted text.  (Read 17926 times)

0 Members and 1 Guest are viewing this topic.

Raptor88

  • Full Member
  • ***
  • Posts: 111
    • View Profile
    • Email
Getting user input and displaying formatted text.
« on: September 13, 2012, 11:27:42 am »
1. What's the easiest way to get user input using SFML?
    Like having a text box where the user can type the player name.

2. What's the easiest way to display formatted text using SFML?
    Like help information.  I could type help screens in Photoshop and display the .png images.
    But is there a way to type formatted text like in a word processor (or notepad) and have SFML
    display the text on the screen with formatting intact? 

Any pre-written code to do items 1 and 2 that I can just insert into my program?

Thanks,
Raptor

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Getting user input and displaying formatted text.
« Reply #1 on: September 13, 2012, 11:41:29 am »
Quote
1. What's the easiest way to get user input using SFML?
    Like having a text box where the user can type the player name.
Text can be retrieved with the sf::Event::TextEntered event.

Quote
2. What's the easiest way to display formatted text using SFML?
sf::String (SFML 1.6), sf::Text (SFML 2.0).
See the corresponding tutorial and/or documentation.

Quote
Any pre-written code to do items 1 and 2 that I can just insert into my program?
Forums don't work this way ;)
We can help you understand things or solve problems, but we won't write your program.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
Re: Getting user input and displaying formatted text.
« Reply #2 on: September 13, 2012, 12:00:07 pm »
Any pre-written code to do items 1 and 2 that I can just insert into my program?
You could look at that code from the wiki, but it's not the nicest code and lacks some features. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Raptor88

  • Full Member
  • ***
  • Posts: 111
    • View Profile
    • Email
Re: Getting user input and displaying formatted text.
« Reply #3 on: September 13, 2012, 10:33:00 pm »
Quote
1. What's the easiest way to get user input using SFML?
    Like having a text box where the user can type the player name.
Text can be retrieved with the sf::Event::TextEntered event.

Quote
2. What's the easiest way to display formatted text using SFML?
sf::String (SFML 1.6), sf::Text (SFML 2.0).
See the corresponding tutorial and/or documentation.

Quote
Any pre-written code to do items 1 and 2 that I can just insert into my program?
Forums don't work this way ;)
We can help you understand things or solve problems, but we won't write your program.

Hi Laurent,

Thanks for taking the time to respond.  I previously saw the items you mentioned but was hoping there were higher level functions that were pre-written for use with SFML.  A class to get user input, echo to textbox allowing backspace to make corrections (similar to cmd.exe) would be a great thing to add to SFML so all users would have access to it.

Thanks,
Raptor

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Getting user input and displaying formatted text.
« Reply #4 on: September 13, 2012, 10:40:08 pm »
This would be out of the scope of SFML.
Laurent Gomila - SFML developer

Raptor88

  • Full Member
  • ***
  • Posts: 111
    • View Profile
    • Email
Re: Getting user input and displaying formatted text.
« Reply #5 on: September 13, 2012, 10:46:55 pm »
You could look at that code from the wiki, but it's not the nicest code and lacks some features. ;)

Hi eXpl0it3r,

Thanks for that link.  It will be very helpful later. 

For now, what I'm looking for is a way to display my help screens like how I would format them in a text editor.  Just plain text but with indenting, paragraphs, etc.  I guess I'll have to write a program that can read text files and store the text in a string (or strings) that SFML can display on the screen.  Either that or type the help screens in  a .png file using Photoshop, and display them that way.

Thanks for always helping out,
Raptor

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Getting user input and displaying formatted text.
« Reply #6 on: September 13, 2012, 11:00:36 pm »
If it's just notepad formatting then ifstream and strings are enough.
Back to C++ gamedev with SFML in May 2023

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
Re: Getting user input and displaying formatted text.
« Reply #7 on: September 13, 2012, 11:01:49 pm »
If you want a simple textbox/formatable textbox I suggest you take a look at some actual GUI libraries, for example Qt. It's way easier, more stable, doesn't need to update x times per second, safes you a lot of time, etc.

Of course if you need this in game or at least rendered via OpenGL, then you'll have to do it yourself (or use existing GUI libraries). ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Raptor88

  • Full Member
  • ***
  • Posts: 111
    • View Profile
    • Email
Re: Getting user input and displaying formatted text.
« Reply #8 on: September 13, 2012, 11:54:31 pm »
If it's just notepad formatting then ifstream and strings are enough.

I think I'll try having the help info in one text file (maybe more) that is distributed with my craps game program.  When the user clicks "HELP", then I'll read the text file and display it in the SFML window, either scrollable or page-by-page.  That way, I can edit the text file as much as I want and my text file to sfml display routine will still work.

Wish someone already wrote this code so I wouldn't have to re-invent the wheel.

Thanks,
Raptor (Who's writing his first C++ program)
« Last Edit: September 14, 2012, 12:19:07 am by Raptor88 »

Raptor88

  • Full Member
  • ***
  • Posts: 111
    • View Profile
    • Email
Re: Getting user input and displaying formatted text.
« Reply #9 on: September 14, 2012, 12:17:16 am »
If you want a simple textbox/formatable textbox I suggest you take a look at some actual GUI libraries, for example Qt. It's way easier, more stable, doesn't need to update x times per second, safes you a lot of time, etc.

Of course if you need this in game or at least rendered via OpenGL, then you'll have to do it yourself (or use existing GUI libraries). ;)

Thanks for the QT suggestion.  First I heard of it so checked some youtube tutorials on it.  Looks very nice indeed.

For my craps game, I need to allow the user to enter setup info like different odds payout for the different bets used at different casinos, initial stake amount in $, and stuff like that.  Even a console type input where I would list the bets to change with a letter that the user could select and then type the odds desired would be fine.  Something like:

a. Snake-Eyes (2)
b. Trey (3)
c. Seven (7)
d. Eleven (11)
e. Box-Cars (12)

The user could type the letter.  The software would ask for the odds desired.  User would type the odds.  Like the odds pay out for a Snake-Eyes might be 30:1 or 31:1 depending on the casio he normally plays at.

Later, when everything is working, I could improve the UI.  Then I could list the bets with a text box next to each and the user can click in the desired text box to change the odds pay off. 

Just looking for the simplest way to do this for now, just to get my program working.

Thanks,
Raptor

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Getting user input and displaying formatted text.
« Reply #10 on: September 14, 2012, 12:35:59 am »
Quote
Wish someone already wrote this code so I wouldn't have to re-invent the wheel.
Getting from notepad formatted.txt to sf::Text is few lines:
        std::ifstream file("whatever.txt");
        std::stringstream stream;
        stream<<file.rdbuf();
        sf::Text text(stream.str(),/* some font */,30);
Quote
For my craps game, I need to allow the user to enter setup info like different odds payout for the different bets used at different casinos, initial stake amount in $, and stuff like that.  Even a console type input where I would list the bets to change with a letter that the user could select and then type the odds desired would be fine.  Something like:
A class that your state(assuming you have state engine) will send all caught textentered events and display it every frame and interface with returncodes or link with pointers with your game class.
Quote
When the user clicks "HELP", then I'll read the text file and display it in the SFML window, either scrollable or page-by-page.
A state that only displays help and catches arrow keys or mouse moves or scrolls to move around and any other even to quit itself, but I don't know how your (state) engine is laid out.
Back to C++ gamedev with SFML in May 2023

Raptor88

  • Full Member
  • ***
  • Posts: 111
    • View Profile
    • Email
Re: Getting user input and displaying formatted text.
« Reply #11 on: September 14, 2012, 02:14:24 am »
Getting from notepad formatted.txt to sf::Text is few lines:
        std::ifstream file("whatever.txt");
        std::stringstream stream;
        stream<<file.rdbuf();
        sf::Text text(stream.str(),/* some font */,30);

That was simple ;).  I'll use that base to form scrollable or page-by-page, forward/backward help info.  Thanks :)

Quote
A class that your state(assuming you have state engine) will send all caught text entered events and display it every frame and interface with return codes or link with pointers with your game class.
...snip...
A state that only displays help and catches arrow keys or mouse moves or scrolls to move around and any other even to quit itself, but I don't know how your (state) engine is laid out.

Now that I've worked out most of the details, and made my PrimaryMenu and craps table layout in Photoshop, I'm going to start writing my code.  I plan to use the "Manage different screens in a game" engine since it should allow exiting the crap table screen, go to the PrimaryMenu screen for user input, then return to the crap table screen with all chips still displayed where they were.  Link to that game engine is here:
https://github.com/SFML/SFML/wiki/TutorialScreens

I'll have a PrimaryMenu screen, SubMenu screens depending on which button is clicked, and a GameScreen.  The GameScreen will have the image of the craps table and allow the user to place different bets for up to 4 players.  It will show the running balance of the stake for each player.  It will simulate a real crap table operation exactly as possible with every possible bet.

Thanks,
Raptor
« Last Edit: September 14, 2012, 04:32:34 am by Raptor88 »

Omegaclawe

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Getting user input and displaying formatted text.
« Reply #12 on: September 14, 2012, 04:20:42 am »
For a project I've been working on, I've more or less made a console emulator... y'don't want its code... I'm not very good at "simple" and terrible at comments, but I can leave you with the idea behind the relevant parts:

- When looking for input, check which keys the user has pressed.
- Pass ASCII value of pressed keys to input class, translating arrows/delete/enter into special characters (some may be done for you)
- Check received text for specific special characters. If it isn't one of these, drop it at the appropriate position in a string, and increment the location
- If it is a key to move through the string, change your location variable accordingly. If it is delete/enter, submit the string or delete at the location.
- Draw the string to the screen.
- Use the built in functions of sf::Text and the location to find where to put the cursor. This will give you a proper X/Y value, though scaling can get... complicated. Draw a box here.
-- Alternate Method: Insert a Vertical Bar (|) into the string at the cursor location before drawing. Simpler, but messes with Kearning.

Might be more... elegant methods, but that should be enough for you to get coding one on your own, which, in the long run, will help you far more than dumping a bunch of code on you.

Raptor88

  • Full Member
  • ***
  • Posts: 111
    • View Profile
    • Email
Re: Getting user input and displaying formatted text.
« Reply #13 on: September 14, 2012, 04:38:15 am »
For a project I've been working on, I've more or less made a console emulator... y'don't want its code... I'm not very good at "simple" and terrible at comments, but I can leave you with the idea behind the relevant parts:

- When looking for input, check which keys the user has pressed.
- Pass ASCII value of pressed keys to input class, translating arrows/delete/enter into special characters (some may be done for you)
- Check received text for specific special characters. If it isn't one of these, drop it at the appropriate position in a string, and increment the location
- If it is a key to move through the string, change your location variable accordingly. If it is delete/enter, submit the string or delete at the location.
- Draw the string to the screen.
- Use the built in functions of sf::Text and the location to find where to put the cursor. This will give you a proper X/Y value, though scaling can get... complicated. Draw a box here.
-- Alternate Method: Insert a Vertical Bar (|) into the string at the cursor location before drawing. Simpler, but messes with Kearning.

Might be more... elegant methods, but that should be enough for you to get coding one on your own, which, in the long run, will help you far more than dumping a bunch of code on you.

Hi Omegaclawe,

Thanks for your suggestions.  Every bit helps in coming up with a plan on how to get/process user string input. 

Raptor

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Getting user input and displaying formatted text.
« Reply #14 on: September 14, 2012, 08:01:48 am »
Quote
        std::ifstream file("whatever.txt");
        std::stringstream stream;
        stream<<file.rdbuf();
I'm not sure if it is standard. This code is "cleaner":
std::ifstream file("whatever.txt");
std::istream_iterator<char> begin(file);
std::istream_iterator<char> end;
std::string contents(begin, end);

Quote
- When looking for input, check which keys the user has pressed.
- Pass ASCII value of pressed keys to input class
No, watch the TextEntered event, not keys!
Laurent Gomila - SFML developer

 

anything