www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/06/22/14:31:10

From: "J.A. Bijsterbosch" <bijster AT worldonline DOT nl>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: String input using Allegro
Date: 22 Jun 1998 17:03:29 GMT
Organization: World Online
Lines: 92
Message-ID: <01bd9d43$51b30ba0$LocalHost@default>
References: <3589A771 DOT 431CC375 AT netrover DOT com>
NNTP-Posting-Host: asd1-p87.worldonline.nl
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Hello Nicolas,

Nicolas Blais <eletech AT netrover DOT com> schreef in artikel
<3589A771 DOT 431CC375 AT netrover DOT com>...
> How do I get a sting input from the user's keyboard using allegro?  I
> already have a function to do so but it is too long and is buggy.

Well, in fact this could be more difficult than you think, if you want to
do a full blown editline routine. If you however just need backspace for
editing and no cursor movement and insert/overwrite functionality, I just
have a little routine that you'll be able to use. It uses the textprintf()
function of Allegro 3.0, but you should easily convert it to textout() if
you use version 2.2 still.

// input.cc

#include <string.h>
#include <allegro.h>

#define	ENTER		13
#define	BACKSPC	8
#define	ESCAPE	27

void editline(int x, int y, int col, char *str, int len)
{
   int done=0, ch;
   int tlen;
   char *temp = new char[len];
   strcpy( temp, str );

   while( !done ) {
      tlen = text_length(font, str);
      textprintf(screen, font, x,y, col, "%s", str);
      textout(screen,font, "_", x+tlen, y, col);
      switch(ch = (readkey() & 0xFF)) {
         case ESCAPE : 	strcpy( str, temp );
         case ENTER  :	done++; break;
         case BACKSPC:
         	if( strlen(str) ) {
               str[strlen(str)-1] = '\0';
               textout(screen,font, " ", x+tlen, y, col);
            } break;
         default:
         	if( strlen(str) < len-1 ) {
               str[strlen(str)] = ch;
               str[strlen(str)+1] = '\0';
            }
      }
   }

   delete[] temp;
}

int main( void )
{
   allegro_init();
   install_keyboard();

   if( set_gfx_mode(GFX_AUTODETECT, 640,480, 0,0) )
   	return -1;

   char prompt[] = "Your Name : ";
   char buff[40] = "";

   textprintf(screen,font, 10,100, 15, "%s%s", prompt, buff);

   editline( text_length(font,prompt)+10, 100, 15, buff, 40 );

   textprintf(screen,font, 10,200, 15, "%s%s", prompt, buff);

   readkey();

   return 0;
}

You could of course add a BITMAP and FONT variable to the editline routine,
if you want to use other output than to the screen, or if you don't want to
use the default font. Keep in mind however, that this function assumes a
fixed character width for every character. If not, you may have to redesign
the backspace part accordingly.

> Thanks, Nicolas.

Hope this gave you some idea... ;-))

-- 
Greetings from sunny Amsterdam

                Jan

email: bijster AT worldonline DOT nl
http://home.worldonline.nl/~bijster

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019