SFML community forums
Help => Graphics => Topic started by: Mikea on July 23, 2009, 12:10:08 am
-
Hello, I'm making a highscore system, and to make that, I need the name of the player, and it would be really lame If i asked the player to input the name in the console. So. I'd like some help.
So far, I can only assume I need a std::string and a way to get input. right?
-
You need the sf::Event::TextEntered event to catch text input, and a sf::String to display it.
-
sorry to ask.. I looked in the Doc for it, but I can't understand how should I get the text I enter and store it in sf::string.
I have my basic event handler.
switch(Game->mAppEvent.Type)
{
case sf::Event::Closed:
//Game->PopState();
//Game->Quit();
break;
case sf::Event::TextEntered:
// I suppose i do the work here. right?
break;
case sf::Event::KeyPressed:
switch(Game->mAppEvent.Key.Code)
{
case sf::Key::P:
break;
case sf::Key::O:
break;
case sf::Key::I:
break;
case sf::Key::Return:
loopit = false;
break;
default: break;
}
break;
default: break;
}
how do I work with those two?
-
std::string str;
sf::String text;
while (window.IsOpened())
{
sf::Event event;
while (window.GetEvent(event))
{
if (event.Type == sf::Event::TextEntered)
{
// Only handle ASCII -- it's up to you if you want to handle other encodings
if (event.Text.Unicode < 128)
{
str += static_cast<char>(event.Text.Unicode);
text.SetText(str);
}
}
}
window.Draw(text);
...
}
-
I googled some help yesterday night and found this also.
if ( event.Type == sf::Event::TextEntered )
{
int textSize = m_completeText.size();
unsigned short unicode = event.Text.Unicode;
if (unicode == 8) //If backspace
if (textSize > 0)
m_completeText.erase(textSize - 1, 1);
else if (unicode >= 32 && unicode <= 126)
m_completeText += (char)unicode;
else if (unicode >= 192 && unicode <= 255)
m_completeText += (char)unicode;
}
If anyone is interested.
Thanks a lot Laurent :D