Im trying to make a save/load box in my editor but how do i get input from the user so the user can write wich file to load/save
I'm working on a similar thing, I can show you
the code when I've finished it...
There is so many ways to do that!
One simple way is to use allegro gui functions.
Or write your own and take input from key[] or readkey()
Read the manual and use your imagination!
The most easy way (IMO) would be to use aDime
It's pretty easy to use, so you don't need to bother with the allegro gui.
This is a real crappy well example and it's
not for long finished and contains at least
one bug but as I'm quite new to c++ and
allegro this is the best I've achieved since
my last post:
1 | bool string_is_finished=false; |
2 | char string[256]; |
3 | int cursor; |
4 | for (cursor=0; cursor<255; string[cursor++]=0); |
5 | cursor=0; |
6 | while(!string_is_finished) |
7 | { |
8 | if(keypressed()) |
9 | { |
10 | if(key[KEY_ENTER]) |
11 | { |
12 | string_is_finished=true; |
13 | } |
14 | if (key[KEY_BACKSPACE]) |
15 | { |
16 | if (cursor>-1) |
17 | { |
18 | string[cursor]=0; |
19 | rest(25); |
20 | clear_keybuf(); |
21 | if(cursor>0){cursor--;} |
22 | } |
23 | } |
24 | if(!(key[KEY_ENTER]||key[KEY_BACKSPACE])) |
25 | { |
26 | string[cursor]=readkey()&0xff; |
27 | cursor++; |
28 | } |
29 | clear_bitmap(screen); |
30 | textout(screen,font,string,100,50,15); |
31 | } |
32 | } |
btw: if you or another one do improvement on this
code, please post it so I can learn/use it...
C:
1 | // edittext.c |
2 | #include <allegro.h> |
3 | |
4 | int main() |
5 | { |
6 | BITMAP* buffer = NULL; |
7 | char edittext[128]; |
8 | int caret = 0; |
9 | |
10 | /* typical Allegro initialization */ |
11 | allegro_init(); |
12 | install_keyboard(); |
13 | set_gfx_mode(GFX_AUTODETECT, 320, 240, 0, 0); |
14 | |
15 | buffer = create_bitmap(320, 240); |
16 | |
17 | do |
18 | { |
19 | if(keypressed()) |
20 | { |
21 | int newkey = readkey(); |
22 | char ASCII = newkey & 0xff; |
23 | char scancode = newkey >> 8; |
24 | |
25 | /* a character key was pressed; add it to the string */ |
26 | if(ASCII >= 32 && ASCII <= 126) |
27 | { |
28 | edittext[caret] = ASCII; |
29 | caret++; |
30 | edittext[caret] = '\0'; |
31 | } |
32 | else if(scancode == KEY_BACKSPACE) |
33 | { |
34 | caret--; |
35 | edittext[caret] = '\0'; |
36 | } |
37 | } |
38 | |
39 | /* all drawing goes here */ |
40 | clear(buffer); |
41 | textout(buffer, font, edittext, 0, 10, makecol(255, 255, 255)); |
42 | vline(buffer, caret * 8, 8, 18, makecol(255, 255, 255)); |
43 | blit(buffer, screen, 0, 0, 0, 0, 320, 240); |
44 | |
45 | } |
46 | while(!key[KEY_ESC]); |
47 | |
48 | destroy_bitmap(buffer); |
49 | |
50 | return 0; |
51 | } |
52 | END_OF_MAIN() |
C++:
1 | // edittext.cpp |
2 | #include <allegro.h> |
3 | #include <string> |
4 | using namespace std; |
5 | |
6 | #define WHITE makecol(255, 255, 255) |
7 | |
8 | int main() |
9 | { |
10 | // typical Allegro initialization |
11 | allegro_init(); |
12 | install_keyboard(); |
13 | set_gfx_mode(GFX_AUTODETECT, 320, 240, 0, 0); |
14 | |
15 | // all variables are here |
16 | BITMAP* buffer = create_bitmap(320, 240); // initialize the double buffer |
17 | string edittext; // an empty string for editting |
18 | string::iterator iter = edittext.begin(); // string iterator |
19 | int caret = 0; // tracks the text caret |
20 | bool insert = true; // true of should text be inserted |
21 | |
22 | // the game loop |
23 | do |
24 | { |
25 | while(keypressed()) |
26 | { |
27 | int newkey = readkey(); |
28 | char ASCII = newkey & 0xff; |
29 | char scancode = newkey >> 8; |
30 | |
31 | // a character key was pressed; add it to the string |
32 | if(ASCII >= 32 && ASCII <= 126) |
33 | { |
34 | // add the new char, inserting or replacing as need be |
35 | if(insert || iter == edittext.end()) |
36 | iter = edittext.insert(iter, ASCII); |
37 | else |
38 | edittext.replace(caret, 1, 1, ASCII); |
39 | |
40 | // increment both the caret and the iterator |
41 | caret++; |
42 | iter++; |
43 | } |
44 | // some other, "special" key was pressed; handle it here |
45 | else |
46 | switch(scancode) |
47 | { |
48 | case KEY_DEL: |
49 | if(iter != edittext.end()) iter = edittext.erase(iter); |
50 | break; |
51 | |
52 | case KEY_BACKSPACE: |
53 | if(iter != edittext.begin()) |
54 | { |
55 | caret--; |
56 | iter--; |
57 | iter = edittext.erase(iter); |
58 | } |
59 | break; |
60 | |
61 | case KEY_RIGHT: |
62 | if(iter != edittext.end()) caret++, iter++; |
63 | break; |
64 | |
65 | case KEY_LEFT: |
66 | if(iter != edittext.begin()) caret--, iter--; |
67 | break; |
68 | |
69 | case KEY_INSERT: |
70 | if(insert) insert = 0; else insert = 1; |
71 | break; |
72 | |
73 | default: |
74 | |
75 | break; |
76 | } |
77 | } |
78 | |
79 | // clear screen |
80 | clear(buffer); |
81 | |
82 | // output the string to the screen |
83 | textout(buffer, font, edittext.c_str(), 0, 10, WHITE); |
84 | |
85 | // output some stats using Allegro's printf functions |
86 | textprintf(buffer, font, 0, 20, WHITE, "length: %d", edittext.length()); |
87 | textprintf(buffer, font, 0, 30, WHITE, "capacity: %d", edittext.capacity()); |
88 | textprintf(buffer, font, 0, 40, WHITE, "empty?: %d", edittext.empty()); |
89 | if(insert) |
90 | textout(buffer, font, "Inserting", 0, 50, WHITE); |
91 | else |
92 | textout(buffer, font, "Replacing", 0, 50, WHITE); |
93 | |
94 | // draw the caret |
95 | vline(buffer, caret * 8, 8, 18, WHITE); |
96 | |
97 | // blit to screen |
98 | blit(buffer, screen, 0, 0, 0, 0, 320, 240); |
99 | |
100 | }while(!key[KEY_ESC]); // end of game loop |
101 | |
102 | // clean up |
103 | destroy_bitmap(buffer); |
104 | set_gfx_mode(GFX_TEXT, 0, 0, 0, 0); |
105 | |
106 | return 0; |
107 | } |
108 | END_OF_MAIN() |
The C++ version has some extra functionality, I think (gotta love the string class).
Hmm... I smell an AMC library around the corner. This would make a fairly nice AMC module, so you don't have to reprogram input all of the time.
I see a possible buffer overflow with 23's C code
Yeah. In the C code. Stupid language.
A simple check to see if you're at the end of the string buffer is all you need really to prevent it (both the beginning and the end , stick with C++ you!
)
I know, I know If your game stops gameplay long enough for me to enter 128 characters it probably sucks anyway
If only it were that easy to rag on C.
Typically unless you provide the explicit checks on buffer size that you NEED in C, C++-style code unfortunately usually turns the problem from a buffer overflow exploit/crash into a process that runs and runs until it runs out of memory from a 1GB long string (causing a crash anyway and making the system slow in the process). One might say that C++ is more dangerous from taking too much data because it just makes the system slow and crashes softly, whereas in C you crash hard and fast and the system moves on. Although you do prevent root exploits which is the most important of all.
EDIT: haha 23 I like that line of reasoning!
Here; is it fixed now? Might as well correct it since I post it a fair bit
1 | // edittext.c |
2 | #include <allegro.h> |
3 | |
4 | #define BUFFERSIZE 128 |
5 | |
6 | int main() |
7 | { |
8 | BITMAP* buffer = NULL; |
9 | char edittext[BUFFERSIZE]; |
10 | int caret = 0; |
11 | |
12 | /* typical Allegro initialization */ |
13 | allegro_init(); |
14 | install_keyboard(); |
15 | set_gfx_mode(GFX_AUTODETECT, 320, 240, 0, 0); |
16 | |
17 | buffer = create_bitmap(320, 240); |
18 | |
19 | do |
20 | { |
21 | if(keypressed()) |
22 | { |
23 | int newkey = readkey(); |
24 | char ASCII = newkey & 0xff; |
25 | char scancode = newkey >> 8; |
26 | |
27 | /* a character key was pressed; add it to the string */ |
28 | if(ASCII >= 32 && ASCII <= 126) |
29 | { |
30 | if(caret < BUFFERSIZE - 1) |
31 | { |
32 | edittext[caret] = ASCII; |
33 | caret++; |
34 | edittext[caret] = '\0'; |
35 | } |
36 | } |
37 | else if(scancode == KEY_BACKSPACE) |
38 | { |
39 | if (caret > 0) caret--; |
40 | edittext[caret] = '\0'; |
41 | } |
42 | } |
43 | |
44 | /* all drawing goes here */ |
45 | clear(buffer); |
46 | textout(buffer, font, edittext, 0, 10, makecol(255, 255, 255)); |
47 | vline(buffer, caret * 8, 8, 18, makecol(255, 255, 255)); |
48 | blit(buffer, screen, 0, 0, 0, 0, 320, 240); |
49 | |
50 | } |
51 | while(!key[KEY_ESC]); |
52 | |
53 | destroy_bitmap(buffer); |
54 | |
55 | return 0; |
56 | } |
57 | END_OF_MAIN() |