Jump to content

  • Log In with Google      Sign In   
  • Create Account

Using sfml2.0 to input a name


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 BaneTrapper   Members   -  Reputation: 1246

Like
0Likes
Like

Posted 05 November 2012 - 12:35 PM

Hello.
Currently i am trying to find a easy way to make a input name in SFML 2.0

What i am trying to do is something like this
std::string Name;
case sf::Event::KeyPressed: //When key is pressed
			    Name.push_back(EventList.key.code); //Push back the key code
		    break;

The problem in this is that the EventList.key.code is SFML enum that is not "ascii" i think.
So what do i do?

This is the code i am using to add to namestring when a key is pressed, and it works flawlessly
But i want something more efficient!
//objApp.objHeroCreator.NameString is a std::string
case sf::Event::KeyPressed:
switch(EventList.key.code)
			    {
				    case sf::Keyboard::Num1:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('!');
					    else
						    objApp.objHeroCreator.NameString.push_back('1');
					    break;
				    case sf::Keyboard::Num2:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('@');
					    else
						    objApp.objHeroCreator.NameString.push_back('2');
					    break;
				    case sf::Keyboard::Num3:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('#');
					    else
						    objApp.objHeroCreator.NameString.push_back('3');
					    break;
				    case sf::Keyboard::Num4:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('$');
					    else
						    objApp.objHeroCreator.NameString.push_back('4');
					    break;
				    case sf::Keyboard::Num5:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('%');
					    else
						    objApp.objHeroCreator.NameString.push_back('5');
					    break;
				    case sf::Keyboard::Num6:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('^');
					    else
						    objApp.objHeroCreator.NameString.push_back('6');
					    break;
				    case sf::Keyboard::Num7:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('&');
					    else
						    objApp.objHeroCreator.NameString.push_back('7');
					    break;
				    case sf::Keyboard::Num8:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('*');
					    else
						    objApp.objHeroCreator.NameString.push_back('8');
					    break;
				    case sf::Keyboard::Num9:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('(');
					    else
						    objApp.objHeroCreator.NameString.push_back('9');
					    break;
				    case sf::Keyboard::Num0:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back(')');
					    else
						    objApp.objHeroCreator.NameString.push_back('0');
					    break;
				    case sf::Keyboard::Return:
					    sf::Mouse::setPosition(sf::Vector2i(objApp.Screen.getPosition().x + SCREEN_WIDTH /2,
														    objApp.Screen.getPosition().y + SCREEN_HEIGHT /2));
					    break;
				    case sf::Keyboard::Delete:
					    if(objApp.objHeroCreator.NameString.size() > 0)
					    {
						    objApp.objHeroCreator.NameString.erase(objApp.objHeroCreator.NameString.size() - 1);
					    }
					    break;
				    case sf::Keyboard::Back:
					    if(objApp.objHeroCreator.NameString.size() > 0)
					    {
						    objApp.objHeroCreator.NameString.erase(objApp.objHeroCreator.NameString.size() - 1);
					    }
					    break;
				    case sf::Keyboard::Space:
					    if(objApp.objHeroCreator.NameString.size() > 0)
						    objApp.objHeroCreator.NameString.push_back(' ');
					    break;
				    case sf::Keyboard::Q:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('Q');
					    else
						    objApp.objHeroCreator.NameString.push_back('q');
					    break;
				    case sf::Keyboard::W:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('W');
					    else
						    objApp.objHeroCreator.NameString.push_back('w');
					    break;
				    case sf::Keyboard::E:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('E');
					    else
						    objApp.objHeroCreator.NameString.push_back('e');
					    break;
				    case sf::Keyboard::R:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('R');
					    else
						    objApp.objHeroCreator.NameString.push_back('r');
					    break;
				    case sf::Keyboard::T:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('T');
					    else
						    objApp.objHeroCreator.NameString.push_back('t');
					    break;
				    case sf::Keyboard::Y:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('Y');
					    else
						    objApp.objHeroCreator.NameString.push_back('y');
					    break;
				    case sf::Keyboard::U:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('U');
					    else
						    objApp.objHeroCreator.NameString.push_back('u');
					    break;
				    case sf::Keyboard::I:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('I');
					    else
						    objApp.objHeroCreator.NameString.push_back('i');
					    break;
				    case sf::Keyboard:<img src='http://public.gamedev.net//public/style_emoticons/<#EMO_DIR#>/ohmy.png' class='bbc_emoticon' alt=':o' />:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('O');
					    else
						    objApp.objHeroCreator.NameString.push_back('o');
					    break;
				    case sf::Keyboard:<img src='http://public.gamedev.net//public/style_emoticons/<#EMO_DIR#>/tongue.png' class='bbc_emoticon' alt=':P' />:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('P');
					    else
						    objApp.objHeroCreator.NameString.push_back('p');
					    break;
				    case sf::Keyboard::A:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('A');
					    else
						    objApp.objHeroCreator.NameString.push_back('a');
					    break;
				    case sf::Keyboard::S:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('S');
					    else
						    objApp.objHeroCreator.NameString.push_back('s');
					    break;
				    case sf::Keyboard:<img src='http://public.gamedev.net//public/style_emoticons/<#EMO_DIR#>/biggrin.png' class='bbc_emoticon' alt=':D' />:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('D');
					    else
						    objApp.objHeroCreator.NameString.push_back('d');
					    break;
				    case sf::Keyboard::F:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('F');
					    else
						    objApp.objHeroCreator.NameString.push_back('f');
					    break;
				    case sf::Keyboard::G:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('G');
					    else
						    objApp.objHeroCreator.NameString.push_back('g');
					    break;
				    case sf::Keyboard::H:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('H');
					    else
						    objApp.objHeroCreator.NameString.push_back('h');
					    break;
				    case sf::Keyboard::J:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('J');
					    else
						    objApp.objHeroCreator.NameString.push_back('j');
					    break;
				    case sf::Keyboard::K:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('K');
					    else
						    objApp.objHeroCreator.NameString.push_back('k');
					    break;
				    case sf::Keyboard::L:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('L');
					    else
						    objApp.objHeroCreator.NameString.push_back('l');
					    break;
				    case sf::Keyboard::Z:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('Z');
					    else
						    objApp.objHeroCreator.NameString.push_back('z');
					    break;
				    case sf::Keyboard::X:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('X');
					    else
						    objApp.objHeroCreator.NameString.push_back('x');
					    break;
				    case sf::Keyboard::C:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('C');
					    else
						    objApp.objHeroCreator.NameString.push_back('c');
					    break;
				    case sf::Keyboard::V:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('V');
					    else
						    objApp.objHeroCreator.NameString.push_back('V');
					    break;
				    case sf::Keyboard::B:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('B');
					    else
						    objApp.objHeroCreator.NameString.push_back('B');
					    break;
				    case sf::Keyboard::N:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('N');
					    else
						    objApp.objHeroCreator.NameString.push_back('n');
					    break;
				    case sf::Keyboard::M:
					    if(Shift)
						    objApp.objHeroCreator.NameString.push_back('M');
					    else
						    objApp.objHeroCreator.NameString.push_back('m');
					    break;
				    default:
					    break;
			    }

Edited by BaneTrapper, 05 November 2012 - 12:36 PM.

Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


Sponsor:

#2 Servant of the Lord   Crossbones+   -  Reputation: 21022

Like
3Likes
Like

Posted 05 November 2012 - 01:26 PM

For text input, you're supposed to use sf::Event::TextEvent instead of keypresses - it wraps this for you.

TextEvent gives each character, when entered, as a UTF-32 character in a Uint32. I haven't messed around with Unicode before, but I think SFML also provides a way to convert it - check out this thread on the SFML forums.
It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#3 kidman171   Members   -  Reputation: 498

Like
2Likes
Like

Posted 05 November 2012 - 02:17 PM

I was actually just working on getting a user's name today. Here is a snippet of the code I used:

while( window.pollEvent(event) )
{
switch( event.type )
{  
case sf::Event::TextEntered:
  if( event.text.unicode < 128 )
  {
   std::string name = playerName.getString();
   if( event.text.unicode == 13 ) // return key
   {
	// finished entering name
   } else if( event.text.unicode == 8 ) { // backspace
	if( name.size() > 0 ) name.resize( name.size() - 1 );
   } else {
	name += static_cast<char>(event.text.unicode);
   }
   playerName.setString( name );
  }
  break;
// ... more cases
}
}

Edited by kidman171, 05 November 2012 - 02:19 PM.


#4 BaneTrapper   Members   -  Reputation: 1246

Like
0Likes
Like

Posted 05 November 2012 - 04:35 PM

For text input, you're supposed to use sf::Event::TextEvent instead of keypresses - it wraps this for you.

TextEvent gives each character, when entered, as a UTF-32 character in a Uint32. I haven't messed around with Unicode before, but I think SFML also provides a way to convert it - check out this thread on the SFML forums.


Thanks on info

I was actually just working on getting a user's name today. Here is a snippet of the code I used:

while( window.pollEvent(event) )
{
switch( event.type )
{ 
case sf::Event::TextEntered:
  if( event.text.unicode < 128 )
  {
   std::string name = playerName.getString();
   if( event.text.unicode == 13 ) // return key
   {
    // finished entering name
   } else if( event.text.unicode == 8 ) { // backspace
    if( name.size() > 0 ) name.resize( name.size() - 1 );
   } else {
    name += static_cast<char>(event.text.unicode);
   }
   playerName.setString( name );
  }
  break;
// ... more cases
}
}


Thanks for code, good to have example shown.
That be all i need help with.

Edited by BaneTrapper, 05 November 2012 - 04:41 PM.

Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS