Hi there,
I'm new to these forums and Allegro itself.
I am creating a program in which I am using Allegro to graph basic math functions. I am accomplishing this by a simple loop of putpixel()'s in which I step the x value up each loop. My y value for putpixel() is obtained through the formula y=(a+(x*b)+(x*x*c)+(x*x*x*d)+(x*x*x*x*e)) where a, b, c, d, and e are doubles. I would really like to allow the user to enter values for these doubles so that they can choose the shape of the graph, but I can't seem to figure out how to that. I know the C++ reserved word cin does not work in Allegro. Is there something similar that I could use in Allegro? I have looked through a bunch of tutorials and FAQ's and I haven't found anything. Could someone either show me what command I should use or point me in the right direction.
Thanks,
Matt
You'd have to use either key[KEY_whatever] or readkey() (most likely readkey) to obtain user input key by key then take that data and use it for something. Sorry I can't help more than that; I haven't actually done this myself, just how to do it.:'(
Thanks for your help spork.
Looking at both key[] and readkey(), it appears they both only accept input from one key. If I wanted to type a number like 1.34 (or any other double), wouldn't it just accept the first key pressed? Is there a way to tie multiple "pressings"? (excuse my non-C++ jargon
)
Another thought, in my college C++ class, we learned to import data from other programs using #include <fstream>. Although that doesn't work, is there away for Allegro to access data from other programs?
Thanks Again!
Matt
If I wanted to type a number like 1.34 (or any other double), wouldn't it just accept the first key pressed? Is there a way to tie multiple "pressings"?
Read the input key by key and save it in a buffer. Then when the user has hit ENTER (or whtever you want to use to terminate input), convert the contents of the buffer to a numeric format.
Or use the Allegro GUI routines, most notably d_edit_proc()
...import data from other programs using #include <fstream>. Although that doesn't work...
What's not working with fstream? Using it in your code concurrently with Allegro, or fstream "doesn't work at all"?
| 1 | #include <iostream> |
| 2 | #include <fstream> |
| 3 | |
| 4 | using namespace std; |
| 5 | |
| 6 | int main(void) |
| 7 | { |
| 8 | int input; |
| 9 | int result; |
| 10 | |
| 11 | // ifstream means input from file |
| 12 | ifstream fin; |
| 13 | // ostream means output to file |
| 14 | ofstream fout; |
| 15 | |
| 16 | fin.open("input_file.txt"); |
| 17 | fout.open("output_file.txt"); |
| 18 | |
| 19 | // Grab data until the end of file (EOF) is reached |
| 20 | fin >> input; |
| 21 | while (!fin.eof()) |
| 22 | { |
| 23 | if (input < 100) |
| 24 | result = input + 100; |
| 25 | else |
| 26 | result = input; |
| 27 | |
| 28 | fout << result; |
| 29 | fin >> input; |
| 30 | } |
| 31 | fin.close(); |
| 32 | fout.close(); |
| 33 | |
| 34 | return 0; |
| 35 | } |
i had a couple of functions that did exactly what your talking about....
...but i just spent 15 minutes looking through my dev files and i think it may be deleted.(check,oh gods, check before delete!)...
someone here should know it, since i got it from the code gallery, although the link seems to be broken...
..there was a couple of lines of hexidecimal magic, to take the actual ascii value of the keypress and put it into a string, inside a loop that went till enter.
...try the code gallery, theres only 3 files there...you should recognize the one...:)
I am not sure but I think this is the snipit you want.
| 1 | #include <allegro.h> |
| 2 | |
| 3 | #define BUFFERSIZE 128 |
| 4 | #define WHITE makecol(255, 255, 255) |
| 5 | #define BLACK makecol(0,0,0) |
| 6 | |
| 7 | int main() |
| 8 | { |
| 9 | /* typical Allegro initialization */ |
| 10 | allegro_init(); |
| 11 | install_keyboard(); |
| 12 | set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0); |
| 13 | |
| 14 | char edittext[BUFFERSIZE]; |
| 15 | int caret = 0; |
| 16 | BITMAP *buffer = create_bitmap(200,20); |
| 17 | //clean out string |
| 18 | int i; |
| 19 | for(i=0;i<BUFFERSIZE;i++) |
| 20 | edittext<i> = '\0'; |
| 21 | //text loop |
| 22 | do { |
| 23 | if(keypressed()) { |
| 24 | int newkey = readkey(); |
| 25 | char ASCII = newkey & 0xff; |
| 26 | char scancode = newkey >> 8; |
| 27 | /* a character key was pressed; add it to the string */ |
| 28 | if(ASCII >= 32 && ASCII <= 126) { |
| 29 | if(caret < BUFFERSIZE - 1) { |
| 30 | edittext[caret] = ASCII; |
| 31 | caret++; |
| 32 | edittext[caret] = '\0'; |
| 33 | } |
| 34 | } |
| 35 | else if(scancode == KEY_BACKSPACE) { |
| 36 | if (caret > 0) caret--; |
| 37 | edittext[caret] = '\0'; |
| 38 | } |
| 39 | } |
| 40 | /* all drawing goes here */ |
| 41 | clear_to_color(buffer,WHITE); |
| 42 | textout_ex(buffer,font,edittext,5,(buffer->h / 2) - 4,BLACK,-1); |
| 43 | vline(buffer,(caret*8) + 6,(buffer->h / 2) - 6,(buffer->h / 2) + 5,BLACK); |
| 44 | vline(buffer,(caret*8) + 7,(buffer->h / 2) - 6,(buffer->h / 2) + 5,BLACK); |
| 45 | blit(buffer,screen,0,0,25,15,300,200); |
| 46 | } while(!key[KEY_ENTER]); |
| 47 | |
| 48 | destroy_bitmap(buffer); |
| 49 | allegro_exit(); |
| 50 | return 0; |
| 51 | } |
| 52 | END_OF_MAIN(); |
Why code when the allegro wiki is there for you 
http://awiki.tomasu.org/bin/view/Main/TextInputC
there's a c++ version as well.
Why code
That is basicly identical code.
[edit] But I didn't have that link so thanks.