Code Search for Developers
 
 
  

exunicod.c from Allegro game programming library at Krugle


Show exunicod.c syntax highlighted

/*
 *    Example program for the Allegro library, by Eric Botcazou.
 *
 *    This program demonstrates the use of the 16-bit Unicode text
 *    encoding format with Allegro. 
 */


#include <stdlib.h>
#include <allegro.h>

#define AL_DATAFILE_NAME   "unifont.dat"

#define NLANGUAGES 8


char message_en[] = "W\x00" "e\x00" "l\x00" "c\x00" "o\x00" "m\x00" "e\x00" " \x00" "t\x00" "o\x00"
                    " \x00\x00\x00";

char message_fr[] = "B\x00" "i\x00" "e\x00" "n\x00" "v\x00" "e\x00" "n\x00" "u\x00" "e\x00"
                    " \x00" "\xE0\x00" " \x00\x00\x00";

char message_es[] = "B\x00" "i\x00" "e\x00" "n\x00" "v\x00" "e\x00" "n\x00" "i\x00" "d\x00" "o\x00"
                    " \x00" "a\x00" " \x00\x00\x00";

char message_el[] = "\x9A\x03\xB1\x03\xBB\x03\xCE\x03\xC2\x03"
                    " \x00\xAE\x03\xC1\x03\xB8\x03\xB1\x03\xC4\x03\xB5\x03"
                    " \x00\xC3\x03\xC4\x03\xBF\x03" " \x00\x00\x00";

char message_ru[] = "\x14\x04" ">\x04" "1\x04" "@\x04" ">\x04" " \x00" "?\x04" ">\x04"
                    "6\x04" "0\x04" ";\x04" ">\x04" "2\x04" "0\x04" "B\x04" "L\x04"
                    " \x00" "2\x04" " \x00\x00\x00";

char message_he[] = " \x00\xDC\x05\xD0\x05" " \x00\xDD\x05\xD9\x05\xD0\x05\xD1\x05\xD4\x05"
                    " \x00\xDD\x05\xD9\x05\xDB\x05\xD5\x05\xE8\x05\xD1\x05\x00\x00";

char message_ja[] = "x0\x88" "0F0S0]0\x00\x00";

char message_zh[] = "\"k\xCE\x8F\x7F" "O(u \x00\00\00";


char allegro_str[] = "A\x00" "l\x00" "l\x00" "e\x00" "g\x00" "r\x00" "o\x00\x00\x00";


struct MESSAGE {
   char *data;
   int right_to_left;
};


struct MESSAGE message[] = { {message_en, FALSE},
                             {message_fr, FALSE},
                             {message_es, FALSE},
                             {message_el, FALSE},
                             {message_ru, FALSE},
                             {message_he, TRUE},
                             {message_ja, TRUE},
                             {message_zh, FALSE} };

int speed[] = {1, 2, 4, 3, 2, 3, 2, 4};


int main(int argc, char *argv[])
{
   AL_DATAFILE *data;
   AL_FONT *f;
   AL_BITMAP *mesg_bmp[NLANGUAGES];
   int length[NLANGUAGES], pos[NLANGUAGES];
   int i, nmesgs, height, hpad, delta;
   char *mesg, buf[256], tmp[256], tmp2[256];

   /* set the text encoding format BEFORE initializing the library */
   al_set_uformat(U_UNICODE);

   /*  past this point, every string that we pass to or retrieve
    *  from any Allegro API call must be in 16-bit Unicode format
    */

   allegro_init();
   al_install_keyboard();
   al_install_timer();

   /* load the datafile containing the Unicode al_font_8x8 */
   al_replace_filename(buf, al_uconvert_ascii(argv[0], tmp), al_uconvert_ascii(AL_DATAFILE_NAME, tmp2), sizeof(buf));
   data = al_load_datafile(buf);
   if (!data) {
      al_show_message(al_uconvert_ascii("Unable to load %s\n", tmp), buf);
      return -1;
   }

   /* set the graphics mode */
   if (al_set_gfx_mode(AL_GFX_SAFE, 640, 480, 0, 0) != 0) {
      al_show_message(al_uconvert_ascii("Unable to set a graphic mode\n", tmp));
      return -1;
   }

   /* set the window title for windowed modes */
   al_set_window_title(al_uconvert_ascii("Unicode example program", tmp));

   al_clear_to_color(al_screen, al_make_color(255, 255, 255));

   /* set transparent text drawing mode */
   al_text_mode(-1);

   /* get a handle to the Unicode al_font_8x8 */
   f = data[0].dat;
   height = al_text_height(f) + 8;

   /* find the number of actually displayed messages */
   nmesgs = MIN(AL_SCREEN_H/height, NLANGUAGES);
   hpad = (AL_SCREEN_H - nmesgs*height)/(nmesgs+1);

   /* prepare the bitmaps */
   for (i=0; i<nmesgs; i++) {

      /* the regular Standard C string manipulation functions don't work
       * with 16-bit Unicode, so we use the Allegro Unicode API
       */
      mesg = malloc(al_ustrsize(message[i].data) + al_ustrsizez(allegro_str));

      if (message[i].right_to_left) {
         al_ustrcpy(mesg, allegro_str);
         al_ustrcat(mesg, message[i].data);
      }
      else {
         al_ustrcpy(mesg, message[i].data);
         al_ustrcat(mesg, allegro_str);
      }

      length[i] = al_text_length(f, mesg) + 8;
      pos[i] = ((float)rand()/RAND_MAX) * AL_SCREEN_W;
      mesg_bmp[i] = al_create_system_bitmap(length[i], height);
      al_clear_to_color(mesg_bmp[i], al_make_color(255, 255, 255));
      al_put_text(mesg_bmp[i], f, mesg, 8, 1, al_make_color(0, 0, 0));

      free(mesg);
   }

   /* do the scrolling */
   while (!al_key_pressed()) {
      for (i=0; i<nmesgs; i++) {
         al_blit(mesg_bmp[i], al_screen, 0, 0, pos[i], hpad + i*(height+hpad), length[i], height);
         delta = pos[i] + length[i] - AL_SCREEN_W;
         if (delta > 0)
            al_blit(mesg_bmp[i], al_screen, length[i] - delta, 0, 0, hpad + i*(height+hpad), delta, height);

         pos[i] += speed[i];
         if (pos[i] >= AL_SCREEN_W)
            pos[i] -= AL_SCREEN_W;
      }

      al_rest(33);
   }

   /* free allocated resources */
   for (i=0; i<nmesgs; i++)
      al_destroy_bitmap(mesg_bmp[i]);

   al_destroy_datafile(data);

   return 0;
}
AL_END_OF_MAIN();




See more files for this project here

Allegro game programming library

Allegro is a cross-platform library intended for use in computer games and other types of multimedia programming.

Project homepage: http://sourceforge.net/projects/alleg
Programming language(s): Assembly,C,Shell Script
License: other

  allegro.pcx
  ex12bit.c
  ex3buf.c
  ex3d.c
  exaccel.c
  exalpha.c
  example.dat
  example.h
  examples.txt
  exbitmap.c
  exblend.c
  excamera.c
  excolmap.c
  excustom.c
  exdata.c
  exdbuf.c
  exdodgy.c
  exexedat.c
  exfixed.c
  exflame.c
  exflip.c
  exgui.c
  exhello.c
  exjoy.c
  exkeys.c
  exlights.c
  exmem.c
  exmidi.c
  exmouse.c
  expal.c
  expat.c
  exquat.c
  exrgbhsv.c
  exsample.c
  exscale.c
  exscn3d.c
  exscroll.c
  exshade.c
  exspline.c
  exsprite.c
  exstars.c
  exstream.c
  exswitch.c
  extimer.c
  extrans.c
  extruec.c
  exunicod.c
  exupdate.c
  exxfade.c
  exzbuf.c
  mysha.pcx
  planet.pcx
  running.dat
  running.h
  unifont.dat