Text input in allegro
I am making an OGG player, but OGG isn't the problem. My problem is, getline(); doesn't seem to agree with allegro, presumably because there is no console, but I still need a way for the user to type in the song name to play it. If you have a way to scan a folder for all the files inside of it, that would help too, but I still need strings to work in allegro. Any suggestions?
It took some manual reading, but I figured out that when I call install_keyboard(); it stops all OS keyboard functions. So all I need to do is not call it until the music starts!!!
EDIT:
Ok, that still doesn't want to work... Can someone at least tell me what's wrong with it? (allegro and strings...)
[Edited by - Gamester3333 on August 24, 2006 5:56:00 PM]
EDIT:
Ok, that still doesn't want to work... Can someone at least tell me what's wrong with it? (allegro and strings...)
[Edited by - Gamester3333 on August 24, 2006 5:56:00 PM]
It has been a while since using Allegro, but from what I can remember you can no longer use C/C++ input handling once calling install_keyboard();
Maybe this thread can help.
Erick
Maybe this thread can help.
Erick
Ooooh, I missed this thread somehow. Here's a bit of code I keep in my back pocket for anytime someone asks how to handle text input in Allegro.
Here is the C version.
Here is the C++ version.
HTH
Here is the C version.
// edittext.c#include <allegro.h>#define BUFFERSIZE 128#define WHITE makecol(255, 255, 255)int main(){ BITMAP* buffer = NULL; char edittext[BUFFERSIZE]; int caret = 0; /* typical Allegro initialization */ allegro_init(); install_keyboard(); set_gfx_mode(GFX_AUTODETECT, 320, 240, 0, 0); buffer = create_bitmap(320, 240); do { if(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) { if(caret < BUFFERSIZE - 1) { edittext[caret] = ASCII; caret++; edittext[caret] = '\0'; } } else if(scancode == KEY_BACKSPACE) { if (caret > 0) caret--; edittext[caret] = '\0'; } } /* all drawing goes here */ clear(buffer); textout(buffer, font, edittext, 0, 10, WHITE); vline(buffer, caret * 8, 8, 18, WHITE); blit(buffer, screen, 0, 0, 0, 0, 320, 240); } while(!key[KEY_ESC]); destroy_bitmap(buffer); return 0;}END_OF_MAIN()
Here is the C++ version.
// edittext.cpp#include <allegro.h>#include <string>using namespace std;#define WHITE makecol(255, 255, 255)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()
HTH
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement