Getting Input
Neil Black

How can I get input the way cin does? I want to make a text-based game, where the player may have to enter both integer and string values, and I can't figure out how. Please help me.

raccoon

How about using cin?

Marco Radaelli

If you're in Allegro graphics mode you have to write your own text input function, because Allegro doesn't provide one ATM. IIRC someone on the forums had already did it, try using the search function.

AFAIK, there's not automatic type conversion available, so you'll read text and you will have to call the right functions to convert the text to integers, floats, etc.

shangshu

This may not be any help but I seem to remember "overloading operators" from my c++ classes to get certain input. That's the subject I would research first.

gnolam

Read up on

Ninkazu

I did this a long time ago and it's pretty ghetto, but you can probably adapt it to your program

1void dinput(char* ret, const char* message, int x, int y, int limit, int col)
2{
3 textout_ex(screen, font, message, x, y, col, 0);
4 int newx = strlen(message)*8 + x;
5 char buf[256];
6 char buf2[256];
7 for (int kl = 0; kl < limit; kl++){
8 ret[kl] = '\0';
9 buf[kl] = '\0';
10 buf2[kl] = '\0';
11 }
12 int kc = 0, p;
13 char k = 0;
14 clear_keybuf();
15 while((kc >> 8) != KEY_ESC)
16 {
17 //can't be \ / ? * " : < > |
18 poll_keyboard();
19 if (keypressed())
20 {
21 kc = readkey();
22 k = kc & 0xff;
23 if ((kc >> 8) == KEY_BACKSPACE)
24 {
25 if (strlen(ret) > 0)
26 ret[strlen(ret)-1] = '\0';
27 for(p = 0; p < (int)(limit-strlen(ret)); p++)
28 buf[p] = ' ';
29 buf[p] = 0;
30 strcpy(buf2,ret);
31 strcat(buf2,buf);
32 textout_ex(screen,font,message,x,y,col,0);
33 textout_ex(screen,font,buf2,newx,y,col,0);
34 }
35 else if ((kc >> 8) == KEY_ENTER)
36 {
37 clear_keybuf();
38 return;
39 }
40 else if((kc >> 8) == KEY_ESC)
41 {
42 clear_keybuf();
43 ret[0] = '\0';
44 return;
45 }
46 else
47 {
48 if ((int)strlen(ret) < limit)
49 {
50 strcpy(buf2,ret);
51 int len = strlen(buf2);
52 buf2[len] = k;
53 buf2[len+1] = 0;
54 strcpy(ret,buf2);
55 textout_ex(screen,font,ret,newx,y,col,0);
56 }
57 }
58 while(key[kc >> 8]) { poll_keyboard(); }
59 }
60 }
61 ret[0] = '\0';
62 clear_keybuf();
63}

Johan Halmén

Allegro's gui works quite well for a simple text input. Just make a DIALOG with a d_edit_proc(). Then use atoi or itoa to convert between char* and int. In fact, I'm working on customising the d_edit_proc() so it would work with variable width coloured fonts, too.

Timorg

I usually lurk around here and don't usually have anything to say, but this time i can be useful. Yayyy.

This is what i use to get single line of input from the user, for high score or whatever.

I wrote it to get a console commands for a network game I wrote for a uni assignment, which is why it has the call back, so that the network code could run it in the back ground rather than stop completely and time out.

I hope its useful, it comes with a dev-cpp project. :)

-Tim

(Edited to mention that the code is attached to this message)

Neil Black

Someone needs to add a cin-like function to allegro. About the gui, I don't have any idea how to use it. I'm, like, super-noob or something. An the learning is slow. I did just figure out how to use timers, because I just now found the examples in the allegro folder. I remember another one showed the gui, so I could go look at that to learn it, but it looks bad and I'm not sure how I could make it work in my game, without making players say "What the h*** is this?!"

Timorg

Well to use the code I attached to my post, you link in the .c file,
put the following include statement in the modules you want to access it from

#include "get_user_input.h"

in your source file and go

get_user_input("Title:", "Sample Input");

and the global variable

char input[(255 + 1) * 6];

will contain the string that the user entered, you don't get much simpler than that. :D If you want to use it and are having problems reply here or pm me, and i will help.

Evert
Quote:

Someone needs to add a cin-like function to allegro.

No, they don't. For several reasons, one of them being that you don't want a general function that blocks everything else waiting for the user to press <enter>.

Audric

Yep, and what kind of visual feedback do you want?
- which font to use to display the characters to type ?
- and when the user types backspace, do we redraw the previous graphic behind the deleted letter ?
- what about the cursor? does it blink? how fast?
- how will it interact with the dual buffer system, or triple buffer etc. ?

It is all so specific, you need (want) to implement your own. Or use a generic GUI component.

BAF
Quote:

but it looks bad and I'm not sure how I could make it work in my game, without making players say "What the h*** is this?!"

That's the Allegro GUI for you. I'd recommend that you check out MASkinG or NASGUI (Google for links).

Evert

I use Allegro's GUI for everything, it's fine. Sure, you'll want to write your own functions for handling MSG_DRAW, but that's hardly an inconvenience.

GrantG

This page has an example of Allegro text input in both C and C++.
http://www.gamedev.net/reference/articles/article2130.asp

SonShadowCat

Check out gstream.

ImLeftFooted

Why is everyone against stringstream?

#include <sstream>
..
char *msg = "1.045";
float f;
std::stringstream(msg) >> f;

Evert

Does it work from an Allegro window?

ImLeftFooted

Ya sure, why not?

Thread #589412. Printed from Allegro.cc