File: exunicod.c

package info (click to toggle)
allegro4 2%3A4.0.1-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 17,052 kB
  • ctags: 12,972
  • sloc: ansic: 109,525; asm: 16,672; cpp: 3,221; sh: 1,761; makefile: 556; pascal: 105; perl: 73
file content (163 lines) | stat: -rw-r--r-- 4,974 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 *    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 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[])
{
   DATAFILE *data;
   FONT *f;
   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 */
   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();
   install_keyboard();
   install_timer();

   /* load the datafile containing the Unicode font */
   replace_filename(buf, uconvert_ascii(argv[0], tmp), uconvert_ascii(DATAFILE_NAME, tmp2), sizeof(buf));
   data = load_datafile(buf);
   if (!data) {
      allegro_message(uconvert_ascii("Unable to load %s\n", tmp), buf);
      return -1;
   }

   /* set the graphics mode */
   if (set_gfx_mode(GFX_SAFE, 640, 480, 0, 0) != 0) {
      allegro_message(uconvert_ascii("Unable to set a graphic mode\n", tmp));
      return -1;
   }

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

   clear_to_color(screen, makecol(255, 255, 255));

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

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

   /* find the number of actually displayed messages */
   nmesgs = MIN(SCREEN_H/height, NLANGUAGES);
   hpad = (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(ustrsize(message[i].data) + ustrsizez(allegro_str));

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

      length[i] = text_length(f, mesg) + 8;
      pos[i] = ((float)rand()/RAND_MAX) * SCREEN_W;
      mesg_bmp[i] = create_system_bitmap(length[i], height);
      clear_to_color(mesg_bmp[i], makecol(255, 255, 255));
      textout(mesg_bmp[i], f, mesg, 8, 1, makecol(0, 0, 0));

      free(mesg);
   }

   /* do the scrolling */
   while (!keypressed()) {
      for (i=0; i<nmesgs; i++) {
         blit(mesg_bmp[i], screen, 0, 0, pos[i], hpad + i*(height+hpad), length[i], height);
         delta = pos[i] + length[i] - SCREEN_W;
         if (delta > 0)
            blit(mesg_bmp[i], screen, length[i] - delta, 0, 0, hpad + i*(height+hpad), delta, height);

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

      rest(33);
   }

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

   unload_datafile(data);

   return 0;
}
END_OF_MAIN();