Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Input output

This thread is locked; no one can reply to it. rss feed Print
Input output
Fire Wolf
Member #7,640
August 2006

I know how to output char
textout_ex( screen, font, "", x, y, makecol( 255, 0, 0), makecol( 0, 0, 0) );

How can I output an int?

I have looked around for documentation on it can't find anything...

Also how do I input...

gnolam
Member #2,030
March 2002
avatar

Quote:

How can I output an int?

I have looked around for documentation on it can't find anything...

You can't have looked very hard...
textprintf_ex()
It's in the manual.

Quote:

Also how do I input...

Parse error. Statement too vague.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Fire Wolf
Member #7,640
August 2006

you know, input...

In my normal c++ I would normally type
cin >> example;

Where example is the value stored.

as an example...

I just tried textprintf_ex

I get
'textprintf_ex' : cannot convert parameter 7 from 'int' to 'const char *'

gnolam
Member #2,030
March 2002
avatar

My mention of the manual was a hint.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Fire Wolf
Member #7,640
August 2006

Ugg took me ages to get my head around it, mental block I suppose...

int money;
textprintf_ex(screen, font, 10, 10, makecol(255, 100, 200),
1, "money: %d", money);

Johan Halmén
Member #1,550
September 2001

Input is a more complex task. I guess the best solution is to use a GUI element. If you use Allegro GUI, make a simple dialog, like:

char *my_input = "        ";
DIALOG dlg[] =
{
   /* (proc)      (x) (y) (w) (h) (fg) (bg)      (key) (flags) (d1) (d2) (dp)           (dp2) (dp3) */
   { d_edit_proc, 20, 28, 84, 16, 0,   16777215, 0,    0,      4,   0,   (void*)my_input, NULL, NULL },
   { NULL,        0,  0,  0,  0,  0,   0,        0,    0,      0,   0,   NULL,          NULL, NULL }
};

popup_dialog(dlg, -1);

After that you have your input in my_input. But RTFM, to get the size of the field correct. And to get the dialog closed properly with <enter>.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

Fire Wolf
Member #7,640
August 2006

What can I use to sort of stick the GUI to the background, and when it is clicked on you can edit the field, then when I click on a button confirm it stores what is in the box.

E.g. Entering a characters name and clicking confirm.

James Stanley
Member #7,275
May 2006
avatar

Can you not read? ;) It stores what's in the box automatically, seeing as dp is a pointer to 'my_input'. You use update_dialog (check the manual, don't just do do_dialog(); and expect it to work). You don't need to do anything to stick it to the screen, it does it automatically. If you need to run some code at the same time, either implement your own proc, and run it from within the dialog, or use some other code I can't quite remember, either way RTFM.

Also, see here

Fire Wolf
Member #7,640
August 2006

But it disapears... I mean, the dialog disapears, I want it to stay there till I click confirm

James Stanley
Member #7,275
May 2006
avatar

Did you read the Allegro GUI Clinic that I linked to? That's a great resource and also where I learnt how to use dialogs. You will need a d_screen_proc (or something like that, I don't remember the name), so that the dialog can keep control of the screen.

When people post pieces of code here, they aren't necessarily tested or complete, and you are likely to have to interpret it or add bits.

EDIT:
It's called a d_clear_proc.

EDIT2:
Or, if you want a function like Input() from Blitz Basic (I highly doubt you've heard of it, but I can't think of another example, it's similar to console input), you would just implement a loop like below, possibly in a function to make it easier to use. This code is untested, incomplete, and is pseudo-code, because I'm not in a programming mood:

while(!character == 10) { //Carriage return
  character = readkey();
  if(!character == 8 /*I _THINK_ that is backspace, check a few other keys as well. Google for an ASCII code table.*/) {
    strcat(theinput, character);
    lettercount++;
  } else {
    theinput[lettercount] = '';
    lettercount--;
  }
}

You might want to add:
A blinking cursor
Arrow Key support
Home/End Key support
Multi-Line support
Anything else I can't think of right now

Derezo
Member #1,666
April 2001
avatar

Quote:

I THINK that is backspace

Capture it as an int and use if(character>>8 == KEY_BACKSPACE)
To add the character to the string AND it with 0xFF. Example: theinput[lettercount] = character&0xff;

Or if you're using C++, just use this, which I retrieved using the "search" function:

1#include <allegro.h>
2#include <string>
3using namespace std;
4 
5#define WHITE makecol(255, 255, 255)
6 
7int main()
8{
9 // typical Allegro initialization
10 allegro_init();
11 install_keyboard();
12 set_gfx_mode(GFX_AUTODETECT, 320, 240, 0, 0);
13 
14 // all variables are here
15 BITMAP* buffer = create_bitmap(320, 240); // initialize the double buffer
16 string edittext; // an empty string for editting
17 string::iterator iter = edittext.begin(); // string iterator
18 int caret = 0; // tracks the text caret
19 bool insert = true; // true of should text be inserted
20
21 // the game loop
22 do
23 {
24 while(keypressed())
25 {
26 int newkey = readkey();
27 char ASCII = newkey & 0xff;
28 char scancode = newkey >> 8;
29 
30 // a character key was pressed; add it to the string
31 if(ASCII >= 32 && ASCII <= 126)
32 {
33 // add the new char, inserting or replacing as need be
34 if(insert || iter == edittext.end())
35 iter = edittext.insert(iter, ASCII);
36 else
37 edittext.replace(caret, 1, 1, ASCII);
38 
39 // increment both the caret and the iterator
40 caret++;
41 iter++;
42 }
43 // some other, "special" key was pressed; handle it here
44 else
45 switch(scancode)
46 {
47 case KEY_DEL:
48 if(iter != edittext.end()) iter = edittext.erase(iter);
49 break;
50 
51 case KEY_BACKSPACE:
52 if(iter != edittext.begin())
53 {
54 caret--;
55 iter--;
56 iter = edittext.erase(iter);
57 }
58 break;
59
60 case KEY_RIGHT:
61 if(iter != edittext.end()) caret++, iter++;
62 break;
63
64 case KEY_LEFT:
65 if(iter != edittext.begin()) caret--, iter--;
66 break;
67
68 case KEY_INSERT:
69 if(insert) insert = 0; else insert = 1;
70 break;
71 
72 default:
73 
74 break;
75 }
76 }
77
78 // clear screen
79 clear(buffer);
80 
81 // output the string to the screen
82 textout(buffer, font, edittext.c_str(), 0, 10, WHITE);
83 
84 // output some stats using Allegro's printf functions
85 textprintf(buffer, font, 0, 20, WHITE, "length: %d", edittext.length());
86 textprintf(buffer, font, 0, 30, WHITE, "capacity: %d", edittext.capacity());
87 textprintf(buffer, font, 0, 40, WHITE, "empty?: %d", edittext.empty());
88 if(insert)
89 textout(buffer, font, "Inserting", 0, 50, WHITE);
90 else
91 textout(buffer, font, "Replacing", 0, 50, WHITE);
92 
93 // draw the caret
94 vline(buffer, caret * 8, 8, 18, WHITE);
95 
96 // blit to screen
97 blit(buffer, screen, 0, 0, 0, 0, 320, 240);
98 
99 }while(!key[KEY_ESC]); // end of game loop
100
101 // clean up
102 destroy_bitmap(buffer);
103 set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
104
105 return 0;
106}
107END_OF_MAIN()

Written by 23yrold3yrold.

"He who controls the stuffing controls the Universe"

Go to: