How to ask a player for his name and store it in a variable?

high,

im working on a tile based RPG and have a question. i want to ask the player
for their name, then store it in a variable. how should i go about taking
input for this? do i really have to make a switch statement checking if ANY
key has pressed and then log that specific key? this would be big and ugly
and im hoping im missing something obvious =) thanks for any help_________________________________________________________________
Stop worrying about overloading your inbox - get MSN Hotmail Extra Storage!
http://join.msn.com/?pgmarket=en-us&page=hotmail/es2&ST=1/go/onm00200362ave/direct/01/

No, not really. When you receive an SDL_event e using SDL_PollEvent(&e) which
type is SDL_KEYDOWN, you simple pick out the ASCII value of the key which
has been pressed:

SDL_Event e;
string s;// these are safer than char buf[80]… no overruns
while(SDL_PollEvent(&e)) {
switch( e.type ) {
case SDL_KEYDOWN:
if(s.length() < MAXLENGTH)
s = s + string(1,(char)(e.keysym.unicode&0xFF));
break;

You have to enable the unicode system with SDL_EnableUNICODE(1) for this to
work. Also, I’m not sure keyboard events are registered at all of you haven’t
opened a window/screen with SDL_SetVideoMode, but this I’m not sure of -
check the docs. At least you have to initialize the SDL with SDL_INIT_VIDEO.

You might want to check for the RETURN key, ESC key, BACKSPACE etc, and
soon you will have your own fance string-input routine :wink:

About the string(1,(char)(e.keysym.unicode&0xFF) thingie:

  1. e.keysym.unicode&0xFF picks out the ASCII (0-255) part of the input
    character.
  2. We have to type-cast this to char for the string class to understand it.
  3. The string class has no direct constructor for a single character,
    so we use the string(int,char) which duplicates the given character
    x times, with x set to 1 for one time :slight_smile:

good luck,

/Olof

Graveyard Filla: "[SDL] how to ask a player for his name and store it…

#high,#
#im working on a tile based RPG and have a question. i want to ask the player
#for their name, then store it in a variable. how should i go about taking
#input for this? do i really have to make a switch statement checking if ANY
#key has pressed and then log that specific key? this would be big and ugly
#and im hoping im missing something obvious =) thanks for any help

#_________________________________________________________________
#Stop worrying about overloading your inbox - get MSN Hotmail Extra Storage!
#http://join.msn.com/?pgmarket=en-us&page=hotmail/es2&ST=1/go/onm00200362ave/direct/01/

#_______________________________________________
#SDL mailing list
#SDL at libsdl.org
#http://www.libsdl.org/mailman/listinfo/sdl

olof,

thanks alot for that explanation. i wont get to try it for a little while
(still working on more important things then asking the player for his name
=) ) but ill let you know how it goes. by the way, any idea on how to
implement the backspace and enter stuff? should i use the same method for
input when taking these keys? or should i do the standard
(if keys == SDLK_ESCAPE)
erase last letter in string

how would i go about erasing the last letter? im using c++ if that matters.
thanks again. also, for the enter thing, i would just have to start a new
string? dont think ill be needing it, anyway though. thanks again!_________________________________________________________________
FREE pop-up blocking with the new MSN Toolbar ? get it now!
http://toolbar.msn.com/go/onm00200415ave/direct/01/

Graveyard Filla wrote:

olof,

thanks alot for that explanation. i wont get to try it for a little
while (still working on more important things then asking the player
for his name =) ) but ill let you know how it goes. by the way, any
idea on how to implement the backspace and enter stuff? should i use
the same method for input when taking these keys? or should i do the
standard (if keys == SDLK_ESCAPE)
erase last letter in string

how would i go about erasing the last letter? im using c++ if that
matters. thanks again. also, for the enter thing, i would just have
to start a new string? dont think ill be needing it, anyway though.
thanks again!

A quite simplified and unoptimized input func: (there’s a lot room for
improvement here, e.g. works properly only on a black background – it
implements a blinking cursor though :wink: )

char* CFont::getString(SDL_Surface screen, int x, int y, int maxLen) {
char string[1024]; // Temporary string
string[0] = ‘\0’;
SDL_EnableUNICODE(1);
SDL_Event event;
SDLKey skey;
int pos = 0;
char ch;
while(skey != SDLK_RETURN) {
while( SDL_PollEvent( &event ) ){
/
We are only worried about SDL_KEYDOWN */
ch = ‘\0’;
skey = SDLK_k;
switch( event.type ){
case SDL_KEYDOWN:
skey = event.key.keysym.sym;
if ( skey != SDLK_RETURN )
if ( (event.key.keysym.unicode & 0xFF80) == 0 ) {
ch = event.key.keysym.unicode & 0x7F;
}
break;
default:
break;
}
if (skey == SDLK_BACKSPACE && event.type == SDL_KEYDOWN && pos > 0)
pos–; }
if (pos < maxLen)
if (ch != ‘\0’)
if ((ch >= ‘a’ && ch <= ‘z’) ||
(ch >= ‘A’ && ch <= ‘Z’) || ch == ’ ') {
string[pos]=ch;
pos++;
};

  string[pos] = '\0';
}
SDL_Rect r;
r.x = x; r.y = y;
r.w = (maxLen+1)*charWidth;
r.h = charWidth;
SDL_FillRect(screen,&r,0);
// Drawing and blinking cursor implementation
if ((SDL_GetTicks() % 500) > 250) {
    drawString(screen,x,y,"%s",string);
  } else {
    drawString(screen,x,y,"%s_",string);
  }
SDL_Flip(screen);

}
SDL_EnableUNICODE(0);
return NULL;
}