23yrold3yrold
Member #1,134
March 2001
|
In 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() |
In C: 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() |
-- Software Development == Church Development Step 1. Build it. Step 2. Pray.
|