1 /***********************************************************
2 * Artsoft Retro-Game Library *
3 *----------------------------------------------------------*
4 * (c) 1994-2002 Artsoft Entertainment *
6 * Detmolder Strasse 189 *
9 * e-mail: info@artsoft.org *
10 *----------------------------------------------------------*
12 ***********************************************************/
21 /* ========================================================================= */
23 /* ========================================================================= */
25 #define NUM_FONT_CHARS (FONT_LINES_PER_FONT * FONT_CHARS_PER_LINE)
27 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
28 static GC font_clip_gc = None;
30 static void InitFontClipmasks()
32 static boolean clipmasks_initialized = FALSE;
33 XGCValues clip_gc_values;
34 unsigned long clip_gc_valuemask;
38 if (gfx.num_fonts == 0 || gfx.font[0].bitmap == NULL)
41 if (!clipmasks_initialized)
43 for (i=0; i < gfx.num_fonts; i++)
44 gfx.font[i].clip_mask = NULL;
46 clipmasks_initialized = TRUE;
49 for (i=0; i < gfx.num_fonts; i++)
51 if (gfx.font[i].clip_mask)
52 for (j=0; j < NUM_FONT_CHARS; j++)
53 XFreePixmap(display, gfx.font[i].clip_mask[j]);
54 free(gfx.font[i].clip_mask);
56 gfx.font[i].clip_mask = NULL;
60 XFreeGC(display, font_clip_gc);
63 /* This stuff is needed because X11 (XSetClipOrigin(), to be precise) is
64 often very slow when preparing a masked XCopyArea() for big Pixmaps.
65 To prevent this, create small (tile-sized) mask Pixmaps which will then
66 be set much faster with XSetClipOrigin() and speed things up a lot. */
68 clip_gc_values.graphics_exposures = False;
69 clip_gc_valuemask = GCGraphicsExposures;
70 font_clip_gc = XCreateGC(display, window->drawable,
71 clip_gc_valuemask, &clip_gc_values);
73 /* create graphic context structures needed for clipping */
74 clip_gc_values.graphics_exposures = False;
75 clip_gc_valuemask = GCGraphicsExposures;
76 copy_clipmask_gc = XCreateGC(display, gfx.font[0].bitmap->clip_mask,
77 clip_gc_valuemask, &clip_gc_values);
79 /* create only those clipping Pixmaps we really need */
80 for (i=0; i < gfx.num_fonts; i++)
82 if (gfx.font[i].bitmap == NULL)
85 gfx.font[i].clip_mask = checked_calloc(NUM_FONT_CHARS * sizeof(Pixmap));
87 for (j=0; j < NUM_FONT_CHARS; j++)
89 Bitmap *src_bitmap = gfx.font[i].bitmap;
90 Pixmap src_pixmap = src_bitmap->clip_mask;
91 int xpos = j % FONT_CHARS_PER_LINE;
92 int ypos = j / FONT_CHARS_PER_LINE;
93 int width = gfx.font[i].width;
94 int height = gfx.font[i].height;
95 int src_x = gfx.font[i].src_x + xpos * width;
96 int src_y = gfx.font[i].src_y + ypos * height;
98 gfx.font[i].clip_mask[j] =
99 XCreatePixmap(display, window->drawable, width, height, 1);
101 XCopyArea(display, src_pixmap, gfx.font[i].clip_mask[j],
102 copy_clipmask_gc, src_x, src_y, width, height, 0, 0);
106 XFreeGC(display, copy_clipmask_gc);
108 #endif /* TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND */
110 void InitFontInfo(struct FontInfo *font_info, int num_fonts)
112 gfx.num_fonts = num_fonts;
113 gfx.font = font_info;
115 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
120 int getFontWidth(int font_nr)
122 return gfx.font[font_nr].width;
125 int getFontHeight(int font_nr)
127 return gfx.font[font_nr].height;
130 void DrawInitText(char *text, int ypos, int font_nr)
132 if (window && gfx.num_fonts > 0 && gfx.font[font_nr].bitmap != NULL)
134 int text_width = strlen(text) * gfx.font[font_nr].width;
136 ClearRectangle(window, 0, ypos, video.width, gfx.font[font_nr].height);
137 DrawTextExt(window, (video.width - text_width) / 2, ypos, text, font_nr,
143 void DrawTextFCentered(int y, int font, char *format, ...)
145 char buffer[MAX_OUTPUT_LINESIZE + 1];
148 va_start(ap, format);
149 vsprintf(buffer, format, ap);
152 if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
153 Error(ERR_EXIT, "string too long in DrawTextFCentered() -- aborting");
155 DrawText(gfx.sx + (gfx.sxsize - strlen(buffer) * gfx.font[font].width) / 2,
156 gfx.sy + y, buffer, font);
159 void DrawTextF(int x, int y, int font, char *format, ...)
161 char buffer[MAX_OUTPUT_LINESIZE + 1];
164 va_start(ap, format);
165 vsprintf(buffer, format, ap);
168 if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
169 Error(ERR_EXIT, "string too long in DrawTextF() -- aborting");
171 DrawText(gfx.sx + x, gfx.sy + y, buffer, font);
174 void DrawText(int x, int y, char *text, int font)
176 int mask_mode = FONT_OPAQUE;
178 if (DrawingOnBackground(x, y))
179 mask_mode = FONT_MASKED;
181 DrawTextExt(drawto, x, y, text, font, mask_mode);
184 redraw_mask |= REDRAW_FIELD;
185 else if (y < gfx.vy || gfx.vy == 0)
186 redraw_mask |= REDRAW_DOOR_1;
189 void DrawTextExt(DrawBuffer *dst_bitmap, int dst_x, int dst_y, char *text,
190 int font_nr, int mask_mode)
192 struct FontInfo *font = &gfx.font[font_nr];
193 boolean print_inverse = FALSE;
195 if (font->bitmap == NULL)
198 /* add offset for drawing font characters */
199 dst_x += font->draw_x;
200 dst_y += font->draw_y;
208 print_inverse = TRUE;
212 if (c >= 'a' && c <= 'z')
214 else if (c == 'ä' || c == 'Ä')
216 else if (c == 'ö' || c == 'Ö')
218 else if (c == 'ü' || c == 'Ü')
220 else if (c == '[' || c == ']') /* map to normal braces */
221 c = (c == '[' ? '(' : ')');
222 else if (c == '\\') /* bad luck ... */
225 if ((c >= 32 && c <= 95) || c == '°' || c == '´' || c == '|')
227 int src_x= font->src_x + ((c - 32) % FONT_CHARS_PER_LINE) * font->width;
228 int src_y= font->src_y + ((c - 32) / FONT_CHARS_PER_LINE) * font->height;
230 if (c == '°' || c == '´' || c == '|') /* map '°' and 'TM' signs */
232 src_x = font->src_x + FONT_CHARS_PER_LINE * font->width;
233 src_y = font->src_y + (c == '°' ? 1 : c == '´' ? 2 : 3) * font->height;
236 if (print_inverse) /* special mode for text gadgets */
238 /* first step: draw solid colored rectangle (use "cursor" character) */
239 BlitBitmap(font->bitmap, dst_bitmap,
240 font->src_x + FONT_CHARS_PER_LINE * font->width,
241 font->src_y + 3 * font->height,
242 font->width, font->height, dst_x, dst_y);
244 /* second step: draw masked black rectangle (use "space" character) */
245 SetClipOrigin(font->bitmap, font->bitmap->stored_clip_gc,
246 dst_x - src_x, dst_y - src_y);
247 BlitBitmapMasked(font->bitmap, dst_bitmap,
248 0, 0, font->width, font->height, dst_x, dst_y);
250 else if (mask_mode == FONT_MASKED)
252 /* clear font character background */
253 ClearRectangleOnBackground(dst_bitmap, dst_x, dst_y,
254 font->width, font->height);
256 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
257 /* use special font tile clipmasks */
259 int font_char = (c >= 32 && c <= 95 ? c - 32 : 0);
261 SetClipMask(font->bitmap, font_clip_gc,
262 font->clip_mask[font_char]);
263 SetClipOrigin(font->bitmap, font_clip_gc, dst_x, dst_y);
266 SetClipOrigin(font->bitmap, font->bitmap->stored_clip_gc,
267 dst_x - src_x, dst_y - src_y);
270 BlitBitmapMasked(font->bitmap, dst_bitmap, src_x, src_y,
271 font->width, font->height, dst_x, dst_y);
273 else /* normal, non-masked font blitting */
275 BlitBitmap(font->bitmap, dst_bitmap, src_x, src_y,
276 font->width, font->height, dst_x, dst_y);
280 dst_x += font->width;