[SDL] Entering usernames while in SDL window

Johannes Prix johannes.prix at kfunigraz.ac.at
Wed Jul 10 22:15:01 PDT 2002


Jason Brunson wrote:
> Does anyone have any examples of code that allows users to enter their 
> names while in the SDL subsystem?  Example: User has completed the level 
> or game and is the prompted to enter their name to be stored in a huigh 
> score list.
>  
> Thanks, Jason

Here is what we use in freedroid (freedroid.sourceforge.net):
You can see if in action there and judge if that is what you would like 
to have.
Also we use the BFont library for displaying font.
Just cut away any 'DebugPrintf' calls.  They only do debug messages to 
log likes.  See ya, jp.


/*-----------------------------------------------------------------
  * Desc: should do roughly what getchar() does, but in raw
  * 	 (SLD) keyboard mode.
  *
  * Return: the (SDLKey) of the next key-pressed event cast to (int)
  *
  *-----------------------------------------------------------------*/
int
getchar_raw (void)
{
   SDL_Event event;
   int Returnkey;

   //  keyboard_update ();   /* treat all pending keyboard-events */

   while (1)
     {
       SDL_WaitEvent (&event);    /* wait for next event */

       if (event.type == SDL_KEYDOWN)
	{
	  /*
	   * here we use the fact that, I cite from SDL_keyboard.h:
	   * "The keyboard syms have been cleverly chosen to map to ASCII"
	   * ... I hope that this design feature is portable, and durable ;)
	   */
	  Returnkey = (int) event.key.keysym.sym;
	  if ( event.key.keysym.mod & KMOD_SHIFT ) Returnkey = toupper( 
(int)event.key.keysym.sym );
	  return ( Returnkey );
	}
       else if (event.type == SDL_KEYUP)
	{
	  // do nothing here, but don't push this event either
	}
       else
	{
	  SDL_PushEvent (&event);  /* put this event back into the queue */
	  keyboard_update ();  /* and treat it the usual way */
	  continue;
	}

     } /* while(1) */

} /* getchar_raw() */





/*-----------------------------------------------------------------
  * @Desc: reads a string of "MaxLen" from User-input, and echos it
  *        either to stdout or using graphics-text, depending on the
  *        parameter "echo":	echo=0    no echo
  * 		               	echo=1    print using printf
  *         			echo=2    print using graphics-text
  *
  *     values of echo > 2 are ignored and treated like echo=0
  *
  *  NOTE: MaxLen is the maximal _strlen_ of the string (excl. \0 !)
  *
  * @Ret: char *: String is allocated _here_!!!
  *       (dont forget to free it !)
  *
  *-----------------------------------------------------------------*/
char *
GetString (int MaxLen, int echo)
{
   char *input;		/* Pointer auf eingegebenen String */
   int key;             /* last 'character' entered */
   int curpos;		/* zaehlt eingeg. Zeichen mit */
   int finished;
   int x0, y0, height;
   SDL_Rect store_rect, tmp_rect;
   SDL_Surface *store = NULL;

   if (echo == 1)		/* echo to stdout */
     {
       printf ("\nGetString(): sorry, echo=1 currently not implemented!\n");
       return NULL;
     }

   x0 = MyCursorX;
   y0 = MyCursorY;
   height = FontHeight (GetCurrentFont());

   store = SDL_CreateRGBSurface(0, SCREENLEN, height, ne_bpp, 0, 0, 0, 0);
   Set_Rect (store_rect, x0, y0, SCREENLEN, height);
   SDL_BlitSurface (ne_screen, &store_rect, store, NULL);

   /* Speicher fuer Eingabe reservieren */
   input     = MyMalloc (MaxLen + 5);

   memset (input, '.', MaxLen);
   input[MaxLen] = 0;

   finished = FALSE;
   curpos = 0;

   while ( !finished  )
     {
       Copy_Rect( store_rect, tmp_rect);
       SDL_BlitSurface (store, NULL, ne_screen, &tmp_rect);
       PutString (ne_screen, x0, y0, input);
       SDL_Flip (ne_screen);

       key = getchar_raw ();

       if (key == SDLK_RETURN)
	{
	  input[curpos] = 0;
	  finished = TRUE;
	}
       else if (isprint (key) && (curpos < MaxLen) )
	{
	  /* printable characters are entered in string */
	  input[curpos] = (char) key;
	  curpos ++;
	}
       else if (key == SDLK_BACKSPACE)
	{
	  if ( curpos > 0 ) curpos --;
	  input[curpos] = '.';
	}

     } /* while(!finished) */

   DebugPrintf (2, "\n\nchar *GetString(..):  The final string is:\n");
   DebugPrintf (2,  input );
   DebugPrintf (2, "\n\n");


   return (input);

} /* GetString() */





More information about the SDL mailing list