Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » A scanf/gets function

This thread is locked; no one can reply to it. rss feed Print
A scanf/gets function
CJ
Member #806
December 2000

I¡m trying to make a function like scanf or gets with Allegro, but I don't know well how to use the readkey() function, Has anybody an example of how to do this? Can I use getch() with Allegro?

Omikron
Member #1,413
February 2001

Hmmm... interesting question, why don't you try and see what happens.

amarillion
Member #940
January 2001
avatar

There is an example of readkey in the allegro\examples directory. I don't remember what it is called. Anyway, readkey() just waits until the user presses a key, and then returns it.

lwithers
Member #630
September 2000

Once you have called install_keyboard(), you will no longer be able to use getch(). If you are writing a console application, you won't be able to use install_keyboard() on several platforms since they must have a graphics window open to bind the keyboard to.
So your best option may be to not use install_keyboard() and to simply use scanf() or gets().
Alternatively, if you do set a graphics mode, why not just use the Allegro GUI functions? See the docs under d_edit_proc, for instance.

CJ
Member #806
December 2000

Amarillion: Have you noted how returns the key readkey()? Look at the allegro docs and you will see what I mean, I don't know how to use the keys in those ways.

CJ
Member #806
December 2000

Ok,I'm sorry, I didn't read the docs well before asking. This is the equivalent function to getch():
key=(readkey() & 0xff);
And other thing, don't try to use getch() because the computer hangs.

Marty Dill
Member #277
April 2000
avatar

I have written code to do this ... if you want, I will post it.

wit
Member #858
November 2000
avatar

Hey CJ!
If you need a gets() like function for allegro I can post mine,too.

WIT

CJ
Member #806
December 2000

Yes, please post me those functions, I'll save some work and I'm very lazy. Thanks in advance.

Marty Dill
Member #277
April 2000
avatar

Here ya go ... nothing special, really. A few modifications would make it much more flexible.
const int KeyOffset=0xff;
char Buf[255];
char* txt;
int Count=0;
char k;
int Current;
do
{
Current=readkey();
k=Current & KeyOffset;

if(k!=13 && k!=8 && Count<10) // max name length of 10 characters
{
textprintf(screen,game[font2].dat,50+15*Count,65,215,"%c",k);
Buf[Count]=k;
Count++;
}
if(k==8 && Count>0)
{
Count--;
textprintf(screen,game[font2].dat,50+15*Count,65,Config.MenuColor,"%c",Buf[Count]);
Buf[Count]=' ';
}

}while(!key[KEY_ENTER]);
Buf[Count]='\0';
txt = (char*)malloc(sizeof(char)*strlen(Buf));
strcpy(txt,Buf);

Bob
Free Market Evangelist
September 2000
avatar

Here's mine:
char s[80];
DIALOG dlg[] = {
. { d_edit_proc, X, Y, W, text_height(font), makecol(0, 0, 0), makecol(255, 255, 255), 0, D_EXIT, 0, 0, s, NULL, NULL},
{ NULL }
};
do_dialog(dlg, 0);
And that's it. 's' now has the string.

--
- Bob
[ -- All my signature links are 404 -- ]

wit
Member #858
November 2000
avatar

Hmm...here it is:
char * ask_name(){
char txt[25]="Put your name ";
char a_char;
int a_int;
int i = 13;
int finished = 0;
int x=300;
int y=300;
clear_keybuf();
hline(screen,x-3,y-3,x+200,makecol(0,255,0));
hline(screen,x-3,y+11,x+200,makecol(0,255,0));
while(finished==0 && i < 24){
textprintf(screen,font,x,y,makecol(0,255,0),"%s",txt);
hline(screen,(i*8+x),y+8,(i*8+x+8),makecol(0,255,0));
a_char=readkey();
a_int=a_char;
if(a_int==13){ finished=1;}
else{
if(a_int==8){
if(i>0){
txt[i-1]=' ';
hline(screen,(i*8+x),y+8,(i*8+x+8),makecol(0,0,0));
i--;
}
}
else{
txt[i]=a_char;
hline(screen,(i*8+x),y+8,(i*8+x+8),makecol(0,0,0));
i++;
}
}
}

return(txt);
}
This fuction asks the player name in Abyss2.
Have no idea if this helps.[:P
WIT

CJ
Member #806
December 2000

Thank you very much for your functions guys!

Go to: