Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » scanf function

This thread is locked; no one can reply to it. rss feed Print
scanf function
sebastien nicaisse
Member #2,252
April 2002

Hello,

I'm new members and I discoved the allegro library. But, i have a little probleme. I want write a editable text.
I use the scanf function but it doesn't function.

Exist a function in allegro library which looks like to a scanf function ?

I thank to answer my question.

SEB
:D

Thomas Harte
Member #33
April 2000
avatar

No, Allegro has no equivalent of scanf, you must write your own! But what you can do is write a function to accept a string as input, then leave the parsing of contents of that string to sscanf. E.g.

1#include <stdarg.h>
2 
3int textscanf(BITMAP *bmp, const FONT *f, int x, int y, int colour, const char *format, ...)
4{
5 char buffer[512];
6 int bufptr, quit;
7 va_list ap;
8 int key, bcol;
9 
10 /* set text in use to the empty string */
11 sprintf(buffer, "");
12 bufptr = quit = 0;
13 
14 /* find out what colour the background is */
15 bcol = getpixel(x, y);
16 
17 /* take characters into buffer */
18 clear_keybuf();
19 while(!quit)
20 {
21 key = readkey();
22 
23 switch(key >> 8)
24 {
25 case KEY_ENTER : quit = 1; break;
26 case KEY_BACKSPACE : if(bufptr) bufptr--; break;
27 default : if(bufptr < 511) {buffer[bufptr] = key&0xff; bufptr++; buffer[bufptr] = '\0';} break;
28 }
29 
30 textout(bmp, font, buffer, x, y, colour);
31 rectfill(bmp, x+text_length(font, buffer), y, bmp->w, y+text_height(font), bcol);
32 }
33 
34 /* pass on to vscanf */
35 va_start(ap, format);
36 vscanf(format, ap);
37 va_end(ap);
38}

Which isn't code useful for everyone because of the assumptions made about the background (i.e. that it is a single colour) and the fact that you can't do anything else at the same time, so its really only useful on the screen bitmap, but I think it gets the point across?

Go to: