Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » I'm with a doubt about text on screen

This thread is locked; no one can reply to it. rss feed Print
I'm with a doubt about text on screen
Jefferson Almeida
Member #12,659
March 2011

I'm making a map editor and will have two options, one to edit an existing map and another to create a new map.
Assuming that the person choose "new map", he'll have to type the name of the map, as I do so that it can enter text on the screen?

William Labbett
Member #4,486
March 2004
avatar

A4 or A5 ?

Jefferson Almeida
Member #12,659
March 2011

oh sorry, A5 friend!

William Labbett
Member #4,486
March 2004
avatar

Here's some code I wrote for the same purpose :-

You need to pass

1) an ALLEGRO_DISPLAY * - one you've created with al_create_display of course.

2) an ALLEGRO_EVENT_QUEUE * - one you've attached the keyboard and display to .

3) a char * that points to some memory - this where the filename gets stored.

so have something like :

char filename[MAX_LENGTH_FOR_FILENAME_STRING] = ""; .. in the caller

4) as you can see - there's two fonts but you could adapt it to be simpler and use just one font - in my example the fonts are true type fonts.

The WHITE and DARK_RED etc... are

#define WHITE al_map_rgb(255, 255, 255)

Loose the #include <feature_representations.h> line but make sure you replace all the symbolic constants with your own - ie MAX_LENGTH_FOR_FRD_STRING, QUIT_DUE_TO_PROBLEM, etc....

Perhaps you make use of this ?????? I know it's a bit an effort just to get a filename but it works here...

If you want to use it, let me know if you have any problems..

#SelectExpand
1#include <allegro5/allegro_ttf.h> 2#include <allegro5/allegro_primitives.h> 3#include <feature_representations.h> 4 5#include "helper.h" 6#include "globals.h" 7 8 9 10 11#define AAA_B 1 12 13 14int get_filename_screen(ALLEGRO_DISPLAY *display, ALLEGRO_EVENT_QUEUE *q, char *filename, ALLEGRO_FONT *big_font, ALLEGRO_FONT *small_font) 15{ 16 ALLEGRO_EVENT event; 17 18 int current_character = 0; 19 20 int string[MAX_LENGTH_FOR_FRD_STRING]; 21 22 al_flush_event_queue(q); 23 24 int key; 25 26 int shift_key_pressed; 27 28 int ascii_value; 29 30 if(filename[0] != '\0') 31 { 32 printf(" get_filename_screen() : string not zeroed.\n"); 33 return QUIT_DUE_TO_PROBLEM; 34 } 35 36 37 strcat( filename, ".frd"); 38 39 //printf(" in get_filename_screen.\n"); 40 41 do 42 { 43 if( !al_is_event_queue_empty(q) ) 44 { 45 while( al_get_next_event(q, &event) ) 46 { 47 switch( event.type ) 48 { 49 case ALLEGRO_EVENT_DISPLAY_CLOSE: 50 return USER_QUIT_PROGRAM; 51 52 case ALLEGRO_EVENT_KEY_CHAR: 53 key = event.keyboard.keycode; 54 55 shift_key_pressed = (event.keyboard.modifiers & ALLEGRO_KEYMOD_SHIFT) ? 1 : 0; 56 57 if(key == ALLEGRO_KEY_ESCAPE) 58 { 59 return USER_QUIT_PROGRAM; 60 } 61 else if( key == ALLEGRO_KEY_BACKSPACE) 62 { 63 if( current_character > 0 ) 64 { 65 filename[current_character - 1] = 0; 66 current_character -= 1; 67 strcat( filename, ".frd"); 68 } 69 } 70 else if( current_character < MAX_LENGTH_FOR_FRD_STRING - 5 && (ascii_value = get_ascii_value( key, shift_key_pressed )) != -1 ) 71 { 72 //printf(" here %d \n", (int) ascii_value); 73 filename[ current_character++ ] = (char) ascii_value; 74 filename[current_character] = 0; 75 strcat( filename, ".frd"); 76 } 77 else if( key == ALLEGRO_KEY_ENTER && current_character != 0) 78 { 79 return FILENAME_STRING_OKAY; 80 } 81 break; 82 83 } 84 85 86 87 88 } 89 } 90 91 al_clear_to_color( DARK_RED ); 92 93 al_draw_text( big_font, WHITE, 100, 100, 0, "Enter a name for the file."); 94 95 96 al_draw_textf( small_font, WHITE, 100, 140, 0, "Name for file : %s", filename); 97 98 al_draw_text( small_font, WHITE, 100, 200, 0, "Press ENTER when filename is finished."); 99 100 101 al_flip_display(); 102 103 } while(1); 104 105 return 0; 106}

/* EDIT - sorry it's a lazyish post but I've had a long day and I've started a glass of wine */

/* EDIT 2 */

#SelectExpand
1int get_ascii_value(int keycode, int shift_key) 2{ 3 4 if(keycode >= ALLEGRO_KEY_A && keycode <= ALLEGRO_KEY_Z) 5 { 6 printf(" returning %d.\n", (keycode - ALLEGRO_KEY_A + 97) - shift_key ? 32 : 0); 7 return (keycode - ALLEGRO_KEY_A + 97) - (shift_key ? 32 : 0); 8 } 9 10 if( keycode >= ALLEGRO_KEY_0 && keycode <= ALLEGRO_KEY_9) 11 { 12 return keycode - ALLEGRO_KEY_0 + 48; 13 } 14 15 if( keycode == ALLEGRO_KEY_MINUS ) 16 { 17 if(shift_key) return '_'; 18 else 19 return '-'; 20 } 21 22 23 return -1; 24}

Hope I haven't put you off programming ;)

Jefferson Almeida
Member #12,659
March 2011

edit

I'm trying to put this code inside of a void, I did some change... That do not worked for now but compile without errors, I think is a good signal! Thanks

William Labbett
Member #4,486
March 2004
avatar

just let me know if you're stuck

Jefferson Almeida
Member #12,659
March 2011

Well, I do not get it to work yet, but I'm packing some things to see. =)

Edgar Reynaldo
Member #8,592
May 2007
avatar

Jefferson Almeida
Member #12,659
March 2011

edit 1: I changed the MAX_LENGTH_FOR_FRD_STRING, worked with bug

http://oi56.tinypic.com/2vljkvb.jpg

#SelectExpand
1 2int get_ascii_value(int keycode, int shift_key) { 3 if(keycode >= ALLEGRO_KEY_A && keycode <= ALLEGRO_KEY_Z) { 4 printf(" returning %d.\n", (keycode - ALLEGRO_KEY_A + 97) - shift_key ? 32 : 0); 5 return (keycode - ALLEGRO_KEY_A + 97) - (shift_key ? 32 : 0); 6 } 7 if(keycode >= ALLEGRO_KEY_0 && keycode <= ALLEGRO_KEY_9) { 8 return keycode - ALLEGRO_KEY_0 + 48; 9 } 10 if(keycode == ALLEGRO_KEY_MINUS) { 11 if(shift_key) return '_'; 12 else 13 return '-'; 14 } 15} 16 17void test() { 18 int get_filename_screen; 19 bool quit; 20 char *filename; 21 ALLEGRO_EVENT ev; 22 int current_character = 0; 23 int MAX_LENGTH_FOR_FRD_STRING; 24 int key; 25 int shift_key_pressed; 26 int ascii_value; 27 if(filename[0] != '\0') { 28 printf(" get_filename_screen() : string not zeroed.\n"); 29 //return QUIT_DUE_TO_PROBLEM; 30 } 31 strcat(filename, ".frd"); 32 33 do { 34 if(!al_is_event_queue_empty(event_queue)) { 35 while(al_get_next_event(event_queue, &ev)) { 36 switch(ev.type) { 37 case ALLEGRO_EVENT_DISPLAY_CLOSE: 38 quit = true; 39 break; 40 case ALLEGRO_EVENT_KEY_CHAR: 41 key = ev.keyboard.keycode; 42 shift_key_pressed = (ev.keyboard.modifiers & ALLEGRO_KEYMOD_SHIFT) ? 1 : 0; 43 if(key == ALLEGRO_KEY_ESCAPE) { 44 quit = true; 45 } 46 else if(key == ALLEGRO_KEY_BACKSPACE) { 47 if(current_character > 0) { 48 filename[current_character - 1] = 0; 49 current_character -= 1; 50 strcat(filename, ".frd"); 51 } 52 } 53 else if( current_character < MAX_LENGTH_FOR_FRD_STRING - 5 && (ascii_value = get_ascii_value(key, shift_key_pressed)) != -1) { 54 //printf(" here %d \n", (int) ascii_value); 55 filename[ current_character++ ] = (char) ascii_value; 56 filename[current_character] = 0; 57 strcat(filename, ".frd"); 58 } 59 else if(key == ALLEGRO_KEY_ENTER && current_character != 0) { 60 } 61 break; 62 } 63 } 64 } 65 al_clear_to_color(al_map_rgb(0,0,0)); 66 67 al_draw_text(font, WHITE, 100, 100, 0, "Enter a name for the file."); 68 al_draw_textf(font, WHITE, 100, 140, 0, "Name for file : %s", filename); 69 al_draw_text(font, WHITE, 100, 200, 0, "Press ENTER when filename is finished."); 70 71 al_flip_display(); 72 73 } while(1); 74 75}

Edgar Reynaldo
Member #8,592
May 2007
avatar

int get_ascii_value(int keycode, int shift_key) {
   if(keycode >= ALLEGRO_KEY_A && keycode <= ALLEGRO_KEY_Z) {
      printf(" returning %d.\n", (keycode - ALLEGRO_KEY_A + 97) - shift_key ? 32 : 0);
      return (keycode - ALLEGRO_KEY_A + 97) - (shift_key ? 32 : 0);
   }
   if(keycode >= ALLEGRO_KEY_0 && keycode <= ALLEGRO_KEY_9) {
      return keycode - ALLEGRO_KEY_0 + 48;
   }
   if(keycode == ALLEGRO_KEY_MINUS) {
      if(shift_key) return '_';
      else
      return '-';
   }
}

Always compile with the -Wall flag with gcc/MinGW. It would have warned you that get_ascii_value is not returning a value when it should have. You should return -1 after the third 'if' statement to indicate it was not a valid character.

Sempre compile com o "-Wall" sinalizador com gcc / MinGW. Teria avisei que get_ascii_value não está retornando um valor, quando deveria ter. Você deve retornar -1, após o terceiro 'if ' para indicar que ele não era um caractere válido.

void test() {
   int get_filename_screen;
   bool quit;
char *filename;
ALLEGRO_EVENT ev; int current_character = 0;
int MAX_LENGTH_FOR_FRD_STRING;
int key; int shift_key_pressed; int ascii_value;

You didn't allocate memory for filename. You didn't initialize MAX_LENGTH_FOR_FRD_STRING either.

Você não alocar memória para o filename. Você não quer inicializar MAX_LENGTH_FOR_FRD_STRING.

   char* filename = 0;
   int MAX_LENGTH_FOR_FRD_STRING = 128;

   filename = (char*)malloc((MAX_LENGTH_FOR_FRD_STRING + 1)*sizeof(char));
   if (!filename) {return ERROR;}
   memset(filename , '\0' , MAX_LENGTH_FOR_FRD_STRING + 1);

Jefferson Almeida
Member #12,659
March 2011

Thanks a lot! :)

Go to: