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 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
26 static GC font_clip_gc = None;
28 static void InitFontClipmasks()
30 XGCValues clip_gc_values;
31 unsigned long clip_gc_valuemask;
35 /* This stuff is needed because X11 (XSetClipOrigin(), to be precise) is
36 often very slow when preparing a masked XCopyArea() for big Pixmaps.
37 To prevent this, create small (tile-sized) mask Pixmaps which will then
38 be set much faster with XSetClipOrigin() and speed things up a lot. */
40 clip_gc_values.graphics_exposures = False;
41 clip_gc_valuemask = GCGraphicsExposures;
42 font_clip_gc = XCreateGC(display, window->drawable,
43 clip_gc_valuemask, &clip_gc_values);
45 /* create graphic context structures needed for clipping */
46 clip_gc_values.graphics_exposures = False;
47 clip_gc_valuemask = GCGraphicsExposures;
48 copy_clipmask_gc = XCreateGC(display,
49 gfx.font_bitmap_info[0].bitmap->clip_mask,
50 clip_gc_valuemask, &clip_gc_values);
52 /* create only those clipping Pixmaps we really need */
53 for (i=0; i < gfx.num_fonts; i++)
55 if (gfx.font_bitmap_info[i].bitmap == NULL)
58 gfx.font_bitmap_info[i].clip_mask =
59 checked_calloc(gfx.font_bitmap_info[i].num_chars * sizeof(Pixmap));
61 for (j=0; j < gfx.font_bitmap_info[i].num_chars; j++)
63 Bitmap *src_bitmap = gfx.font_bitmap_info[i].bitmap;
64 Pixmap src_pixmap = src_bitmap->clip_mask;
65 int xpos = j % gfx.font_bitmap_info[i].num_chars_per_line;
66 int ypos = j / gfx.font_bitmap_info[i].num_chars_per_line;
67 int width = gfx.font_bitmap_info[i].width;
68 int height = gfx.font_bitmap_info[i].height;
69 int src_x = gfx.font_bitmap_info[i].src_x + xpos * width;
70 int src_y = gfx.font_bitmap_info[i].src_y + ypos * height;
72 gfx.font_bitmap_info[i].clip_mask[j] =
73 XCreatePixmap(display, window->drawable, width, height, 1);
75 XCopyArea(display, src_pixmap, gfx.font_bitmap_info[i].clip_mask[j],
76 copy_clipmask_gc, src_x, src_y, width, height, 0, 0);
80 XFreeGC(display, copy_clipmask_gc);
83 static void FreeFontClipmasks()
87 if (gfx.num_fonts == 0 || gfx.font_bitmap_info[0].bitmap == NULL)
90 for (i=0; i < gfx.num_fonts; i++)
92 if (gfx.font_bitmap_info[i].clip_mask)
94 for (j=0; j < gfx.font_bitmap_info[i].num_chars; j++)
95 XFreePixmap(display, gfx.font_bitmap_info[i].clip_mask[j]);
96 free(gfx.font_bitmap_info[i].clip_mask);
99 gfx.font_bitmap_info[i].clip_mask = NULL;
100 gfx.font_bitmap_info[i].num_chars = 0;
104 XFreeGC(display, font_clip_gc);
107 #endif /* TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND */
109 void InitFontInfo(struct FontBitmapInfo *font_bitmap_info, int num_fonts,
110 int (*select_font_function)(int))
112 gfx.num_fonts = num_fonts;
113 gfx.font_bitmap_info = font_bitmap_info;
114 gfx.select_font_function = select_font_function;
116 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
121 void FreeFontInfo(struct FontBitmapInfo *font_bitmap_info)
123 if (font_bitmap_info == NULL)
126 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
130 free(font_bitmap_info);
133 int getFontWidth(int font_nr)
135 int font_bitmap_id = gfx.select_font_function(font_nr);
137 return gfx.font_bitmap_info[font_bitmap_id].width;
140 int getFontHeight(int font_nr)
142 int font_bitmap_id = gfx.select_font_function(font_nr);
144 return gfx.font_bitmap_info[font_bitmap_id].height;
147 int getTextWidth(char *text, int font_nr)
149 return (text != NULL ? strlen(text) * getFontWidth(font_nr) : 0);
152 static char getFontCharPosition(int font_nr, char c)
154 int font_bitmap_id = gfx.select_font_function(font_nr);
155 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
156 boolean default_font = (font->num_chars == DEFAULT_NUM_CHARS_PER_FONT);
157 int font_pos = c - 32;
159 /* map some special characters to their ascii values in default font */
161 font_pos = MAP_FONT_ASCII(c) - 32;
163 /* this allows dynamic special characters together with special font */
164 if (font_pos < 0 || font_pos >= font->num_chars)
170 void getFontCharSource(int font_nr, char c, Bitmap **bitmap, int *x, int *y)
172 int font_bitmap_id = gfx.select_font_function(font_nr);
173 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
174 int font_pos = getFontCharPosition(font_nr, c);
176 *bitmap = font->bitmap;
177 *x = font->src_x + (font_pos % font->num_chars_per_line) * font->width;
178 *y = font->src_y + (font_pos / font->num_chars_per_line) * font->height;
181 void DrawInitText(char *text, int ypos, int font_nr)
185 gfx.font_bitmap_info[font_nr].bitmap != NULL)
187 ClearRectangle(window, 0, ypos, video.width, getFontHeight(font_nr));
188 DrawTextExt(window, (video.width - getTextWidth(text, font_nr)) / 2, ypos,
189 text, font_nr, BLIT_OPAQUE);
194 void DrawTextFCentered(int y, int font_nr, char *format, ...)
196 char buffer[MAX_OUTPUT_LINESIZE + 1];
199 va_start(ap, format);
200 vsprintf(buffer, format, ap);
203 if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
204 Error(ERR_EXIT, "string too long in DrawTextFCentered() -- aborting");
206 DrawText(gfx.sx + (gfx.sxsize - getTextWidth(buffer, font_nr)) / 2,
207 gfx.sy + y, buffer, font_nr);
210 void DrawTextF(int x, int y, int font_nr, char *format, ...)
212 char buffer[MAX_OUTPUT_LINESIZE + 1];
215 va_start(ap, format);
216 vsprintf(buffer, format, ap);
219 if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
220 Error(ERR_EXIT, "string too long in DrawTextF() -- aborting");
222 DrawText(gfx.sx + x, gfx.sy + y, buffer, font_nr);
225 void DrawText(int x, int y, char *text, int font_nr)
227 int mask_mode = BLIT_OPAQUE;
229 if (DrawingOnBackground(x, y))
230 mask_mode = BLIT_ON_BACKGROUND;
232 DrawTextExt(drawto, x, y, text, font_nr, mask_mode);
235 redraw_mask |= REDRAW_FIELD;
236 else if (y < gfx.vy || gfx.vy == 0)
237 redraw_mask |= REDRAW_DOOR_1;
240 void DrawTextExt(DrawBuffer *dst_bitmap, int dst_x, int dst_y, char *text,
241 int font_nr, int mask_mode)
243 int font_bitmap_id = gfx.select_font_function(font_nr);
244 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
245 int font_width = getFontWidth(font_nr);
246 int font_height = getFontHeight(font_nr);
249 char *text_ptr = text;
251 if (font->bitmap == NULL)
254 /* add offset for drawing font characters */
255 dst_x += font->draw_x;
256 dst_y += font->draw_y;
260 char c = *text_ptr++;
262 getFontCharSource(font_nr, c, &src_bitmap, &src_x, &src_y);
264 if (mask_mode == BLIT_INVERSE) /* special mode for text gadgets */
266 /* first step: draw solid colored rectangle (use "cursor" character) */
267 if (strlen(text) == 1) /* only one char inverted => draw cursor */
269 Bitmap *cursor_bitmap;
270 int cursor_x, cursor_y;
272 getFontCharSource(font_nr, FONT_ASCII_CURSOR, &cursor_bitmap,
273 &cursor_x, &cursor_y);
275 BlitBitmap(cursor_bitmap, dst_bitmap, cursor_x, cursor_y,
276 font_width, font_height, dst_x, dst_y);
279 #if defined(TARGET_SDL)
280 /* second step: draw masked inverted character */
281 SDLCopyInverseMasked(src_bitmap, dst_bitmap, src_x, src_y,
282 font_width, font_height, dst_x, dst_y);
284 /* second step: draw masked black rectangle (use "space" character) */
285 SetClipOrigin(src_bitmap, src_bitmap->stored_clip_gc,
286 dst_x - src_x, dst_y - src_y);
287 BlitBitmapMasked(src_bitmap, dst_bitmap, 0, 0,
288 font_width, font_height, dst_x, dst_y);
291 else if (mask_mode == BLIT_MASKED || mask_mode == BLIT_ON_BACKGROUND)
293 if (mask_mode == BLIT_ON_BACKGROUND)
295 /* clear font character background */
296 ClearRectangleOnBackground(dst_bitmap, dst_x, dst_y,
297 font_width, font_height);
300 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
301 /* use special font tile clipmasks */
303 int font_pos = getFontCharPosition(font_nr, c);
305 SetClipMask(src_bitmap, font_clip_gc, font->clip_mask[font_pos]);
306 SetClipOrigin(src_bitmap, font_clip_gc, dst_x, dst_y);
309 SetClipOrigin(src_bitmap, src_bitmap->stored_clip_gc,
310 dst_x - src_x, dst_y - src_y);
313 BlitBitmapMasked(src_bitmap, dst_bitmap, src_x, src_y,
314 font_width, font_height, dst_x, dst_y);
316 else /* normal, non-masked font blitting */
318 BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y,
319 font_width, font_height, dst_x, dst_y);