Allegro Tutorial: Alternative Text Input (not using Allegro GUI)
Okay, I didn’t program this, I actually got it from a friend. I’m not sure who wrote it (my friend didn’t) but he said it was freeware.
#include allegro.h
#include winalleg.h
#include string
using namespace std;
#define WHITE makecol(255, 255, 255)
void init();
void deinit();
int main()
{
// typical Allegro initialization
allegro_init();
install_keyboard();
set_gfx_mode(GFX_AUTODETECT, 320, 240, 0, 0);
// all variables are here
BITMAP* buffer = create_bitmap(320, 240); // initialize the double buffer
string edittext; // an empty string for editting
string::iterator iter = edittext.begin(); // string iterator
int caret = 0; // tracks the text caret
bool insert = true; // true if text should be inserted
// the game loop
do
{
while(keypressed())
{
int newkey = readkey();
char ASCII = newkey & 0xff;
char scancode = newkey >> 8;
// a character key was pressed; add it to the string
if(ASCII >= 32 && ASCII <= 126)
{
// add the new char, inserting or replacing as need be
if(insert || iter == edittext.end())
iter = edittext.insert(iter, ASCII);
else
edittext.replace(caret, 1, 1, ASCII);
// increment both the caret and the iterator
caret++;
iter++;
}
// some other, "special" key was pressed; handle it here
else
switch(scancode)
{
case KEY_DEL:
if(iter != edittext.end()) iter = edittext.erase(iter);
break;
case KEY_BACKSPACE:
if(iter != edittext.begin())
{
caret--;
iter--;
iter = edittext.erase(iter);
}
break;
case KEY_RIGHT:
if(iter != edittext.end()) caret++, iter++;
break;
case KEY_LEFT:
if(iter != edittext.begin()) caret--, iter--;
break;
case KEY_INSERT:
insert = !insert;
break;
default:
break;
}
}
// clear screen
clear(buffer);
// output the string to the screen
textout(buffer, font, edittext.c_str(), 0, 10, WHITE);
// output some stats using Allegro's printf functions
textprintf(buffer, font, 0, 20, WHITE, "length: %d", edittext.length());
textprintf(buffer, font, 0, 30, WHITE, "capacity: %d", edittext.capacity());
textprintf(buffer, font, 0, 40, WHITE, "empty?: %d", edittext.empty());
if(insert)
textout(buffer, font, "Inserting", 0, 50, WHITE);
else
textout(buffer, font, "Replacing", 0, 50, WHITE);
// draw the caret
vline(buffer, caret * 8, 8, 18, WHITE);
// blit to screen
blit(buffer, screen, 0, 0, 0, 0, 320, 240);
}while(!key[KEY_ESC]); // end of game loop
// clean up
destroy_bitmap(buffer);
set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
return 0;
}
END_OF_MAIN()
void init() {
int depth, res;
allegro_init();
depth = desktop_color_depth();
if (depth == 0) depth = 32;
set_color_depth(depth);
res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
if (res != 0) {
allegro_message(allegro_error);
exit(-1);
}
install_timer();
install_keyboard();
install_mouse();
/* add other initializations here */
}
void deinit() {
clear_keybuf();
/* add other deinitializations here */
}

The code works pretty well, but do you have any idea to use it to make some kind of login in my game? I have tried to call the code as a function and write the user then destroy the bitmap and call the funcion again but the text input remains in the same place and modifying the same variable.
Are you re-drawing the screen each time? If you don’t refresh what’s being drawn to the screen it would look like the name isn’t changing even if it really is.
Yes I do. I made a cycle that reads mouse click’s position, then if the values are in a certain area I used a goto to go to another cycle the write the name press enter and then goto the first cycle where the screen is re-drawn.
Actually the main problem here is that the code does not modify another variable, if it run the program and modify “name” variable i.e. when it returns to the first cycle and click the “Last name” variable it continues modifying the “name” variable