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 static int last_num_fonts = 0;
31 XGCValues clip_gc_values;
32 unsigned long clip_gc_valuemask;
36 if (gfx.num_fonts == 0 || gfx.font_bitmap_info[0].bitmap == NULL)
39 for (i=0; i < last_num_fonts; i++)
41 if (gfx.font_bitmap_info[i].clip_mask)
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);
48 gfx.font_bitmap_info[i].clip_mask = NULL;
49 gfx.font_bitmap_info[i].last_num_chars = 0;
52 last_num_fonts = gfx.num_fonts;
55 XFreeGC(display, font_clip_gc);
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. */
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);
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);
75 /* create only those clipping Pixmaps we really need */
76 for (i=0; i < gfx.num_fonts; i++)
78 if (gfx.font_bitmap_info[i].bitmap == NULL)
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;
85 for (j=0; j < gfx.font_bitmap_info[i].num_chars; j++)
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;
96 gfx.font_bitmap_info[i].clip_mask[j] =
97 XCreatePixmap(display, window->drawable, width, height, 1);
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);
104 XFreeGC(display, copy_clipmask_gc);
106 #endif /* TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND */
108 void InitFontInfo(struct FontBitmapInfo *font_bitmap_info, int num_fonts,
109 int (*select_font_function)(int))
111 gfx.num_fonts = num_fonts;
112 gfx.font_bitmap_info = font_bitmap_info;
113 gfx.select_font_function = select_font_function;
115 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
120 int getFontWidth(int font_nr)
122 int font_bitmap_id = gfx.select_font_function(font_nr);
124 return gfx.font_bitmap_info[font_bitmap_id].width;
127 int getFontHeight(int font_nr)
129 int font_bitmap_id = gfx.select_font_function(font_nr);
131 return gfx.font_bitmap_info[font_bitmap_id].height;
134 static char getFontCharPosition(int font_nr, char c)
136 int font_bitmap_id = gfx.select_font_function(font_nr);
137 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
138 boolean default_font = (font->num_chars == DEFAULT_NUM_CHARS_PER_FONT);
139 int font_pos = c - 32;
141 /* map some special characters to their ascii values in default font */
143 font_pos = MAP_FONT_ASCII(c) - 32;
145 /* this allows dynamic special characters together with special font */
146 if (font_pos < 0 || font_pos >= font->num_chars)
152 void getFontCharSource(int font_nr, char c, Bitmap **bitmap, int *x, int *y)
154 int font_bitmap_id = gfx.select_font_function(font_nr);
155 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
156 int font_pos = getFontCharPosition(font_nr, c);
158 *bitmap = font->bitmap;
159 *x = font->src_x + (font_pos % font->num_chars_per_line) * font->width;
160 *y = font->src_y + (font_pos / font->num_chars_per_line) * font->height;
163 void DrawInitText(char *text, int ypos, int font_nr)
167 gfx.font_bitmap_info[font_nr].bitmap != NULL)
169 int text_width = strlen(text) * getFontWidth(font_nr);
171 ClearRectangle(window, 0, ypos, video.width, getFontHeight(font_nr));
172 DrawTextExt(window, (video.width - text_width) / 2, ypos, text, font_nr,
178 void DrawTextFCentered(int y, int font_nr, char *format, ...)
180 char buffer[MAX_OUTPUT_LINESIZE + 1];
183 va_start(ap, format);
184 vsprintf(buffer, format, ap);
187 if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
188 Error(ERR_EXIT, "string too long in DrawTextFCentered() -- aborting");
190 DrawText(gfx.sx + (gfx.sxsize - strlen(buffer) * getFontWidth(font_nr)) / 2,
191 gfx.sy + y, buffer, font_nr);
194 void DrawTextF(int x, 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 DrawTextF() -- aborting");
206 DrawText(gfx.sx + x, gfx.sy + y, buffer, font_nr);
209 void DrawText(int x, int y, char *text, int font_nr)
211 int mask_mode = BLIT_OPAQUE;
213 if (DrawingOnBackground(x, y))
214 mask_mode = BLIT_ON_BACKGROUND;
216 DrawTextExt(drawto, x, y, text, font_nr, mask_mode);
219 redraw_mask |= REDRAW_FIELD;
220 else if (y < gfx.vy || gfx.vy == 0)
221 redraw_mask |= REDRAW_DOOR_1;
224 void DrawTextExt(DrawBuffer *dst_bitmap, int dst_x, int dst_y, char *text,
225 int font_nr, int mask_mode)
227 int font_bitmap_id = gfx.select_font_function(font_nr);
228 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
229 int font_width = getFontWidth(font_nr);
230 int font_height = getFontHeight(font_nr);
233 char *text_ptr = text;
235 if (font->bitmap == NULL)
238 /* add offset for drawing font characters */
239 dst_x += font->draw_x;
240 dst_y += font->draw_y;
244 char c = *text_ptr++;
246 getFontCharSource(font_nr, c, &src_bitmap, &src_x, &src_y);
248 if (mask_mode == BLIT_INVERSE) /* special mode for text gadgets */
250 /* first step: draw solid colored rectangle (use "cursor" character) */
251 if (strlen(text) == 1) /* only one char inverted => draw cursor */
253 Bitmap *cursor_bitmap;
254 int cursor_x, cursor_y;
256 getFontCharSource(font_nr, FONT_ASCII_CURSOR, &cursor_bitmap,
257 &cursor_x, &cursor_y);
259 BlitBitmap(cursor_bitmap, dst_bitmap, cursor_x, cursor_y,
260 font_width, font_height, dst_x, dst_y);
263 #if defined(TARGET_SDL)
264 /* second step: draw masked inverted character */
265 SDLCopyInverseMasked(src_bitmap, dst_bitmap, src_x, src_y,
266 font_width, font_height, dst_x, dst_y);
268 /* second step: draw masked black rectangle (use "space" character) */
269 SetClipOrigin(src_bitmap, src_bitmap->stored_clip_gc,
270 dst_x - src_x, dst_y - src_y);
271 BlitBitmapMasked(src_bitmap, dst_bitmap, 0, 0,
272 font_width, font_height, dst_x, dst_y);
275 else if (mask_mode == BLIT_MASKED || mask_mode == BLIT_ON_BACKGROUND)
277 if (mask_mode == BLIT_ON_BACKGROUND)
279 /* clear font character background */
280 ClearRectangleOnBackground(dst_bitmap, dst_x, dst_y,
281 font_width, font_height);
284 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
285 /* use special font tile clipmasks */
287 int font_pos = getFontCharPosition(font_nr, c);
289 SetClipMask(src_bitmap, font_clip_gc, font->clip_mask[font_pos]);
290 SetClipOrigin(src_bitmap, font_clip_gc, dst_x, dst_y);
293 SetClipOrigin(src_bitmap, src_bitmap->stored_clip_gc,
294 dst_x - src_x, dst_y - src_y);
297 BlitBitmapMasked(src_bitmap, dst_bitmap, src_x, src_y,
298 font_width, font_height, dst_x, dst_y);
300 else /* normal, non-masked font blitting */
302 BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y,
303 font_width, font_height, dst_x, dst_y);