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