rnd-20030406-2-src
[rocksndiamonds.git] / src / libgame / text.c
1 /***********************************************************
2 * Artsoft Retro-Game Library                               *
3 *----------------------------------------------------------*
4 * (c) 1994-2002 Artsoft Entertainment                      *
5 *               Holger Schemel                             *
6 *               Detmolder Strasse 189                      *
7 *               33604 Bielefeld                            *
8 *               Germany                                    *
9 *               e-mail: info@artsoft.org                   *
10 *----------------------------------------------------------*
11 * text.c                                                   *
12 ***********************************************************/
13
14 #include <stdio.h>
15 #include <stdarg.h>
16
17 #include "text.h"
18 #include "misc.h"
19
20
21 /* ========================================================================= */
22 /* font functions                                                            */
23 /* ========================================================================= */
24
25 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
26 static GC       font_clip_gc = None;
27
28 static void InitFontClipmasks()
29 {
30   static int last_num_fonts = 0;
31   XGCValues clip_gc_values;
32   unsigned long clip_gc_valuemask;
33   GC copy_clipmask_gc;
34   int i, j;
35
36   if (gfx.num_fonts == 0 || gfx.font_bitmap_info[0].bitmap == NULL)
37     return;
38
39   for (i=0; i < last_num_fonts; i++)
40   {
41     if (gfx.font_bitmap_info[i].clip_mask)
42     {
43       for (j=0; j < gfx.font_bitmap_info[i].last_num_chars; j++)
44         XFreePixmap(display, gfx.font_bitmap_info[i].clip_mask[j]);
45       free(gfx.font_bitmap_info[i].clip_mask);
46     }
47
48     gfx.font_bitmap_info[i].clip_mask = NULL;
49     gfx.font_bitmap_info[i].last_num_chars = 0;
50   }
51
52   last_num_fonts = gfx.num_fonts;
53
54   if (font_clip_gc)
55     XFreeGC(display, font_clip_gc);
56   font_clip_gc = None;
57
58   /* This stuff is needed because X11 (XSetClipOrigin(), to be precise) is
59      often very slow when preparing a masked XCopyArea() for big Pixmaps.
60      To prevent this, create small (tile-sized) mask Pixmaps which will then
61      be set much faster with XSetClipOrigin() and speed things up a lot. */
62
63   clip_gc_values.graphics_exposures = False;
64   clip_gc_valuemask = GCGraphicsExposures;
65   font_clip_gc = XCreateGC(display, window->drawable,
66                            clip_gc_valuemask, &clip_gc_values);
67
68   /* create graphic context structures needed for clipping */
69   clip_gc_values.graphics_exposures = False;
70   clip_gc_valuemask = GCGraphicsExposures;
71   copy_clipmask_gc = XCreateGC(display,
72                                gfx.font_bitmap_info[0].bitmap->clip_mask,
73                                clip_gc_valuemask, &clip_gc_values);
74
75   /* create only those clipping Pixmaps we really need */
76   for (i=0; i < gfx.num_fonts; i++)
77   {
78     if (gfx.font_bitmap_info[i].bitmap == NULL)
79       continue;
80
81     gfx.font_bitmap_info[i].clip_mask =
82       checked_calloc(gfx.font_bitmap_info[i].num_chars * sizeof(Pixmap));
83     gfx.font_bitmap_info[i].last_num_chars = gfx.font_bitmap_info[i].num_chars;
84
85     for (j=0; j < gfx.font_bitmap_info[i].num_chars; j++)
86     {
87       Bitmap *src_bitmap = gfx.font_bitmap_info[i].bitmap;
88       Pixmap src_pixmap = src_bitmap->clip_mask;
89       int xpos = j % gfx.font_bitmap_info[i].num_chars_per_line;
90       int ypos = j / gfx.font_bitmap_info[i].num_chars_per_line;
91       int width  = gfx.font_bitmap_info[i].width;
92       int height = gfx.font_bitmap_info[i].height;
93       int src_x = gfx.font_bitmap_info[i].src_x + xpos * width;
94       int src_y = gfx.font_bitmap_info[i].src_y + ypos * height;
95
96       gfx.font_bitmap_info[i].clip_mask[j] =
97         XCreatePixmap(display, window->drawable, width, height, 1);
98
99       XCopyArea(display, src_pixmap, gfx.font_bitmap_info[i].clip_mask[j],
100                 copy_clipmask_gc, src_x, src_y, width, height, 0, 0);
101     }
102   }
103
104   XFreeGC(display, copy_clipmask_gc);
105 }
106 #endif /* TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND */
107
108 void InitFontInfo(struct FontBitmapInfo *font_bitmap_info, int num_fonts,
109                   int (*select_font_function)(int))
110 {
111   gfx.num_fonts = num_fonts;
112   gfx.font_bitmap_info = font_bitmap_info;
113   gfx.select_font_function = select_font_function;
114   gfx.inverse_text_color = WHITE_PIXEL;
115
116 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
117   InitFontClipmasks();
118 #endif
119 }
120
121 void SetInverseTextColor(Pixel inverse_text_color)
122 {
123   gfx.inverse_text_color = inverse_text_color;
124 }
125
126 int getFontWidth(int font_nr)
127 {
128   int font_bitmap_id = gfx.select_font_function(font_nr);
129
130   return gfx.font_bitmap_info[font_bitmap_id].width;
131 }
132
133 int getFontHeight(int font_nr)
134 {
135   int font_bitmap_id = gfx.select_font_function(font_nr);
136
137   return gfx.font_bitmap_info[font_bitmap_id].height;
138 }
139
140 static char getFontCharPosition(int font_nr, char c)
141 {
142   int font_bitmap_id = gfx.select_font_function(font_nr);
143   struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
144   boolean default_font = (font->num_chars == DEFAULT_NUM_CHARS_PER_FONT);
145   int font_pos = c - 32;
146
147   /* map some special characters to their ascii values in default font */
148   if (default_font)
149     font_pos = MAP_FONT_ASCII(c) - 32;
150
151   /* this allows dynamic special characters together with special font */
152   if (font_pos < 0 || font_pos >= font->num_chars)
153     font_pos = 0;
154
155   return font_pos;
156 }
157
158 void getFontCharSource(int font_nr, char c, Bitmap **bitmap, int *x, int *y)
159 {
160   int font_bitmap_id = gfx.select_font_function(font_nr);
161   struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
162   int font_pos = getFontCharPosition(font_nr, c);
163
164   *bitmap = font->bitmap;
165   *x = font->src_x + (font_pos % font->num_chars_per_line) * font->width;
166   *y = font->src_y + (font_pos / font->num_chars_per_line) * font->height;
167 }
168
169 void DrawInitText(char *text, int ypos, int font_nr)
170 {
171   if (window &&
172       gfx.num_fonts > 0 &&
173       gfx.font_bitmap_info[font_nr].bitmap != NULL)
174   {
175     int text_width = strlen(text) * getFontWidth(font_nr);
176
177     ClearRectangle(window, 0, ypos, video.width, getFontHeight(font_nr));
178     DrawTextExt(window, (video.width - text_width) / 2, ypos, text, font_nr,
179                 BLIT_OPAQUE);
180     FlushDisplay();
181   }
182 }
183
184 void DrawTextFCentered(int y, int font_nr, char *format, ...)
185 {
186   char buffer[MAX_OUTPUT_LINESIZE + 1];
187   va_list ap;
188
189   va_start(ap, format);
190   vsprintf(buffer, format, ap);
191   va_end(ap);
192
193   if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
194     Error(ERR_EXIT, "string too long in DrawTextFCentered() -- aborting");
195
196   DrawText(gfx.sx + (gfx.sxsize - strlen(buffer) * getFontWidth(font_nr)) / 2,
197            gfx.sy + y, buffer, font_nr);
198 }
199
200 void DrawTextF(int x, int y, int font_nr, char *format, ...)
201 {
202   char buffer[MAX_OUTPUT_LINESIZE + 1];
203   va_list ap;
204
205   va_start(ap, format);
206   vsprintf(buffer, format, ap);
207   va_end(ap);
208
209   if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
210     Error(ERR_EXIT, "string too long in DrawTextF() -- aborting");
211
212   DrawText(gfx.sx + x, gfx.sy + y, buffer, font_nr);
213 }
214
215 void DrawText(int x, int y, char *text, int font_nr)
216 {
217   int mask_mode = BLIT_OPAQUE;
218
219   if (DrawingOnBackground(x, y))
220     mask_mode = BLIT_MASKED;
221
222   DrawTextExt(drawto, x, y, text, font_nr, mask_mode);
223
224   if (x < gfx.dx)
225     redraw_mask |= REDRAW_FIELD;
226   else if (y < gfx.vy || gfx.vy == 0)
227     redraw_mask |= REDRAW_DOOR_1;
228 }
229
230 void DrawTextExt(DrawBuffer *dst_bitmap, int dst_x, int dst_y, char *text,
231                  int font_nr, int mask_mode)
232 {
233   int font_bitmap_id = gfx.select_font_function(font_nr);
234   struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
235   int font_width = getFontWidth(font_nr);
236   int font_height = getFontHeight(font_nr);
237   Bitmap *src_bitmap;
238   int src_x, src_y;
239   char *text_ptr = text;
240
241   if (font->bitmap == NULL)
242     return;
243
244   /* add offset for drawing font characters */
245   dst_x += font->draw_x;
246   dst_y += font->draw_y;
247
248   while (*text_ptr)
249   {
250     char c = *text_ptr++;
251
252     getFontCharSource(font_nr, c, &src_bitmap, &src_x, &src_y);
253
254     if (mask_mode == BLIT_INVERSE)      /* special mode for text gadgets */
255     {
256 #if defined(TARGET_SDL)
257       /* blit normally (non-masked) */
258       BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y,
259                  font_width, font_height, dst_x, dst_y);
260
261       /* invert character */
262       SDLInvertArea(dst_bitmap, dst_x, dst_y, font_width, font_height,
263                     gfx.inverse_text_color);
264 #else
265       /* first step: draw solid colored rectangle (use "cursor" character) */
266       if (strlen(text) == 1)    /* only one char inverted => draw cursor */
267       {
268         Bitmap *cursor_bitmap;
269         int cursor_x, cursor_y;
270
271         getFontCharSource(font_nr, FONT_ASCII_CURSOR, &cursor_bitmap,
272                           &cursor_x, &cursor_y);
273
274         BlitBitmap(cursor_bitmap, dst_bitmap, cursor_x, cursor_y,
275                    font_width, font_height, dst_x, dst_y);
276       }
277
278       /* second step: draw masked black rectangle (use "space" character) */
279       SetClipOrigin(src_bitmap, src_bitmap->stored_clip_gc,
280                     dst_x - src_x, dst_y - src_y);
281       BlitBitmapMasked(src_bitmap, dst_bitmap, 0, 0,
282                        font_width, font_height, dst_x, dst_y);
283 #endif
284     }
285     else if (mask_mode == BLIT_MASKED)
286     {
287       /* clear font character background */
288       ClearRectangleOnBackground(dst_bitmap, dst_x, dst_y,
289                                  font_width, font_height);
290
291 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
292       /* use special font tile clipmasks */
293       {
294         int font_pos = getFontCharPosition(font_nr, c);
295
296         SetClipMask(src_bitmap, font_clip_gc, font->clip_mask[font_pos]);
297         SetClipOrigin(src_bitmap, font_clip_gc, dst_x, dst_y);
298       }
299 #else
300       SetClipOrigin(src_bitmap, src_bitmap->stored_clip_gc,
301                     dst_x - src_x, dst_y - src_y);
302 #endif
303
304       BlitBitmapMasked(src_bitmap, dst_bitmap, src_x, src_y,
305                        font_width, font_height, dst_x, dst_y);
306     }
307     else        /* normal, non-masked font blitting */
308     {
309       BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y,
310                  font_width, font_height, dst_x, dst_y);
311     }
312
313     dst_x += font_width;
314   }
315 }