#include <allegro_font.h>
#include <allegro_font.h>
#include <allegro_ttf.h>

main()

  {
    char catch_all_buffer[31];
    al_init();
    al_init_font_addon();
    al_install_keyboard();
    al_init_font_addon();
    al_init_ttf_addon();

    ALLEGRO_DISPLAY* display = al_create_display(800, 600);
    al_clear_to_color(al_map_rgb(0, 0, 0));
    ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue();
    al_register_event_source(queue, al_get_keyboard_event_source());
    al_register_event_source(queue, al_get_display_event_source(display));
    ALLEGRO_USTR* str = al_ustr_new(" ");
    int pos = (int)al_ustr_size(str);
    ALLEGRO_FONT *font16 = al_load_font("DroidSerif.ttf" , 24, 0);
    ALLEGRO_COLOR color_yellow = al_map_rgb(255,255,0);
    ALLEGRO_COLOR color_white = al_map_rgb(255,255,255);
    al_clear_to_color(al_map_rgb(0, 0, 0));

    int quit = false;

    while(!quit)
      {
         al_draw_ustr(font16, al_map_rgb(255,255,0), 100,100, ALLEGRO_ALIGN_LEFT, str);
         al_flip_display();

         ALLEGRO_EVENT e;
         al_wait_for_event(queue, &e);
         switch(e.type)

           {
              case ALLEGRO_EVENT_DISPLAY_CLOSE:
                 quit = true;
                 break;
              case ALLEGRO_EVENT_KEY_CHAR:

              if(e.keyboard.unichar >= 32 && e.keyboard.unichar <= 127)
                 {
                    pos += al_ustr_append_chr(str, e.keyboard.unichar);
                 }
              else if(e.keyboard.keycode == ALLEGRO_KEY_BACKSPACE)
                 {
                    if(al_ustr_prev(str, &pos))
                       {
                          al_ustr_truncate(str, pos);
                          al_clear_to_color(al_map_rgb(0, 0, 0));
                          al_flip_display();
                       }
                 }
              else if(e.keyboard.keycode == ALLEGRO_KEY_ENTER)
                 {
                    al_ustr_to_buffer(str, catch_all_buffer, 30);
                    al_draw_ustr(font16, color_white, 100,200, 0, str);
                    al_flip_display();
                    al_draw_textf(font16, color_white,100,300,ALLEGRO_ALIGN_LEFT, "%s", catch_all_buffer);
                    al_flip_display();
                    break;
                 }

           }
      }
  }


