Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Getting Input

This thread is locked; no one can reply to it. rss feed Print
Getting Input
Neil Black
Member #7,867
October 2006
avatar

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
Member #7,478
July 2006

How about using cin?

Marco Radaelli
Member #3,028
December 2002
avatar

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
Member #8,168
December 2006

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
Member #2,030
March 2002
avatar

Read up on

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

Ninkazu
Member #7,861
October 2006

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
Member #1,550
September 2001

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.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.

Timorg
Member #2,028
March 2002

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)

____________________________________________________________________________________________
"c is much better than c++ if you don't need OOP simply because it's smaller and requires less load time." - alethiophile
OMG my sides are hurting from laughing so hard... :D

Neil Black
Member #7,867
October 2006
avatar

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
Member #2,028
March 2002

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.

____________________________________________________________________________________________
"c is much better than c++ if you don't need OOP simply because it's smaller and requires less load time." - alethiophile
OMG my sides are hurting from laughing so hard... :D

Evert
Member #794
November 2000
avatar

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
Member #907
January 2001

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
Member #2,981
December 2002
avatar

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
Member #794
November 2000
avatar

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
Member #8,173
December 2006
avatar

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

SonShadowCat
Member #1,548
September 2001
avatar

ImLeftFooted
Member #3,935
October 2003
avatar

Why is everyone against stringstream?

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

Evert
Member #794
November 2000
avatar

Does it work from an Allegro window?

ImLeftFooted
Member #3,935
October 2003
avatar

Ya sure, why not?

Go to: