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 struct FontBitmapInfo *getFontBitmapInfo(int font_nr)
135 int font_bitmap_id = gfx.select_font_function(font_nr);
137 return &gfx.font_bitmap_info[font_bitmap_id];
140 int getFontWidth(int font_nr)
142 int font_bitmap_id = gfx.select_font_function(font_nr);
144 return gfx.font_bitmap_info[font_bitmap_id].width;
147 int getFontHeight(int font_nr)
149 int font_bitmap_id = gfx.select_font_function(font_nr);
151 return gfx.font_bitmap_info[font_bitmap_id].height;
154 int getTextWidth(char *text, int font_nr)
156 return (text != NULL ? strlen(text) * getFontWidth(font_nr) : 0);
159 static char getFontCharPosition(int font_nr, char c)
161 int font_bitmap_id = gfx.select_font_function(font_nr);
162 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
163 boolean default_font = (font->num_chars == DEFAULT_NUM_CHARS_PER_FONT);
164 int font_pos = c - 32;
166 /* map some special characters to their ascii values in default font */
168 font_pos = MAP_FONT_ASCII(c) - 32;
170 /* this allows dynamic special characters together with special font */
171 if (font_pos < 0 || font_pos >= font->num_chars)
177 void getFontCharSource(int font_nr, char c, Bitmap **bitmap, int *x, int *y)
179 int font_bitmap_id = gfx.select_font_function(font_nr);
180 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
181 int font_pos = getFontCharPosition(font_nr, c);
183 *bitmap = font->bitmap;
184 *x = font->src_x + (font_pos % font->num_chars_per_line) * font->width;
185 *y = font->src_y + (font_pos / font->num_chars_per_line) * font->height;
188 void DrawInitText(char *text, int ypos, int font_nr)
192 gfx.font_bitmap_info[font_nr].bitmap != NULL)
194 ClearRectangle(window, 0, ypos, video.width, getFontHeight(font_nr));
195 DrawTextExt(window, (video.width - getTextWidth(text, font_nr)) / 2, ypos,
196 text, font_nr, BLIT_OPAQUE);
201 void DrawTextF(int x, int y, int font_nr, char *format, ...)
203 char buffer[MAX_OUTPUT_LINESIZE + 1];
206 va_start(ap, format);
207 vsprintf(buffer, format, ap);
210 if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
211 Error(ERR_EXIT, "string too long in DrawTextF() -- aborting");
213 DrawText(gfx.sx + x, gfx.sy + y, buffer, font_nr);
216 void DrawTextFCentered(int y, int font_nr, char *format, ...)
218 char buffer[MAX_OUTPUT_LINESIZE + 1];
221 va_start(ap, format);
222 vsprintf(buffer, format, ap);
225 if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
226 Error(ERR_EXIT, "string too long in DrawTextFCentered() -- aborting");
228 DrawText(gfx.sx + (gfx.sxsize - getTextWidth(buffer, font_nr)) / 2,
229 gfx.sy + y, buffer, font_nr);
232 void DrawTextS(int x, int y, int font_nr, char *text)
234 DrawText(gfx.sx + x, gfx.sy + y, text, font_nr);
237 void DrawTextSCentered(int y, int font_nr, char *text)
239 DrawText(gfx.sx + (gfx.sxsize - getTextWidth(text, font_nr)) / 2,
240 gfx.sy + y, text, font_nr);
243 void DrawText(int x, int y, char *text, int font_nr)
245 int mask_mode = BLIT_OPAQUE;
247 if (DrawingOnBackground(x, y))
248 mask_mode = BLIT_ON_BACKGROUND;
250 DrawTextExt(drawto, x, y, text, font_nr, mask_mode);
253 redraw_mask |= REDRAW_FIELD;
254 else if (y < gfx.vy || gfx.vy == 0)
255 redraw_mask |= REDRAW_DOOR_1;
258 void DrawTextExt(DrawBuffer *dst_bitmap, int dst_x, int dst_y, char *text,
259 int font_nr, int mask_mode)
261 int font_bitmap_id = gfx.select_font_function(font_nr);
262 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
263 int font_width = getFontWidth(font_nr);
264 int font_height = getFontHeight(font_nr);
266 int border_1 = gfx.sx + gfx.sxsize;
267 int border_2 = gfx.dx + gfx.dxsize;
268 int dst_x_start = dst_x;
272 char *text_ptr = text;
274 if (font->bitmap == NULL)
277 /* skip text to be printed outside the window (left/right will be clipped) */
278 if (dst_y < 0 || dst_y + font_height > video.height)
281 /* add offset for drawing font characters */
282 dst_x += font->draw_xoffset;
283 dst_y += font->draw_yoffset;
287 char c = *text_ptr++;
290 c = ' '; /* print space instaed of newline */
292 getFontCharSource(font_nr, c, &src_bitmap, &src_x, &src_y);
294 /* clip text at the left side of the window */
302 /* clip text at the right side of the window */
304 if (dst_x + font_width > video.width)
307 /* (this does not work well when trying to print text to whole screen) */
308 if ((dst_x_start < border_1 && dst_x + font_width > border_1) ||
309 (dst_x_start < border_2 && dst_x + font_width > border_2))
313 if (mask_mode == BLIT_INVERSE) /* special mode for text gadgets */
315 /* first step: draw solid colored rectangle (use "cursor" character) */
316 if (strlen(text) == 1) /* only one char inverted => draw cursor */
318 Bitmap *cursor_bitmap;
319 int cursor_x, cursor_y;
321 getFontCharSource(font_nr, FONT_ASCII_CURSOR, &cursor_bitmap,
322 &cursor_x, &cursor_y);
324 BlitBitmap(cursor_bitmap, dst_bitmap, cursor_x, cursor_y,
325 font_width, font_height, dst_x, dst_y);
328 #if defined(TARGET_SDL)
329 /* second step: draw masked inverted character */
330 SDLCopyInverseMasked(src_bitmap, dst_bitmap, src_x, src_y,
331 font_width, font_height, dst_x, dst_y);
333 /* second step: draw masked black rectangle (use "space" character) */
334 SetClipOrigin(src_bitmap, src_bitmap->stored_clip_gc,
335 dst_x - src_x, dst_y - src_y);
336 BlitBitmapMasked(src_bitmap, dst_bitmap, 0, 0,
337 font_width, font_height, dst_x, dst_y);
340 else if (mask_mode == BLIT_MASKED || mask_mode == BLIT_ON_BACKGROUND)
342 if (mask_mode == BLIT_ON_BACKGROUND)
344 /* clear font character background */
345 ClearRectangleOnBackground(dst_bitmap, dst_x, dst_y,
346 font_width, font_height);
349 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
350 /* use special font tile clipmasks */
352 int font_pos = getFontCharPosition(font_nr, c);
354 SetClipMask(src_bitmap, font_clip_gc, font->clip_mask[font_pos]);
355 SetClipOrigin(src_bitmap, font_clip_gc, dst_x, dst_y);
358 SetClipOrigin(src_bitmap, src_bitmap->stored_clip_gc,
359 dst_x - src_x, dst_y - src_y);
362 BlitBitmapMasked(src_bitmap, dst_bitmap, src_x, src_y,
363 font_width, font_height, dst_x, dst_y);
365 else /* normal, non-masked font blitting */
367 BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y,
368 font_width, font_height, dst_x, dst_y);
375 void DrawTextToTextArea(int x, int y, char *text, int font_nr, int line_length,
376 int area_xsize, int area_ysize, int mask_mode)
379 int font_height = getFontHeight(font_nr);
384 while (*text && area_line < area_ysize)
386 char buffer[MAX_OUTPUT_LINESIZE + 1];
389 for (i = 0; i < line_length && *text; i++)
390 if ((buffer[i] = *text++) == '\n')
392 buffer[MIN(i, area_xsize)] = '\0';
394 DrawTextExt(drawto, x, y + area_line * font_height, buffer, font_nr,
400 redraw_mask |= REDRAW_FIELD;
403 boolean RenderLineToBuffer(char **src_buffer_ptr, char *dst_buffer,
404 int *dst_buffer_len, boolean last_line_was_empty,
407 char *text_ptr = *src_buffer_ptr;
408 char *buffer = dst_buffer;
409 int buffer_len = *dst_buffer_len;
410 boolean buffer_filled = FALSE;
417 /* skip leading whitespaces */
418 while (*text_ptr == ' ' || *text_ptr == '\t')
424 /* look for end of next word */
425 while (*word_ptr != ' ' && *word_ptr != '\t' && *word_ptr != '\0')
435 else if (*text_ptr == '\n') /* special case: force empty line */
440 /* prevent printing of multiple empty lines */
441 if (buffer_len > 0 || !last_line_was_empty)
442 buffer_filled = TRUE;
444 else if (word_len < line_length - buffer_len)
446 /* word fits into text buffer -- add word */
449 buffer[buffer_len++] = ' ';
451 strncpy(&buffer[buffer_len], text_ptr, word_len);
452 buffer_len += word_len;
453 buffer[buffer_len] = '\0';
454 text_ptr += word_len;
456 else if (buffer_len > 0)
458 /* not enough space left for word in text buffer -- print buffer */
460 buffer_filled = TRUE;
464 /* word does not fit at all into empty text buffer -- cut word */
466 strncpy(buffer, text_ptr, line_length);
467 buffer[line_length] = '\0';
468 text_ptr += line_length;
469 buffer_filled = TRUE;
476 *src_buffer_ptr = text_ptr;
477 *dst_buffer_len = buffer_len;
479 return buffer_filled;
482 void DrawTextWrapped(int x, int y, char *text, int font_nr, int line_length,
485 char *text_ptr = text;
486 char buffer[line_length + 1];
488 int current_line = 0;
489 int font_height = getFontHeight(font_nr);
491 while (*text_ptr && current_line < max_lines)
496 RenderLineToBuffer(&text_ptr, buffer, &buffer_len, TRUE, line_length);
498 DrawText(x, y + current_line * font_height, buffer, font_nr);
503 int DrawTextFromFile(int x, int y, char *filename, int font_nr,
504 int line_length, int max_lines)
506 int font_height = getFontHeight(font_nr);
507 char line[MAX_LINE_LEN];
508 char buffer[line_length + 1];
510 int current_line = 0;
513 if (current_line >= max_lines)
516 if (filename == NULL)
519 if (!(file = fopen(filename, MODE_READ)))
525 while (!feof(file) && current_line < max_lines)
528 boolean last_line_was_empty = TRUE;
530 /* read next line of input file */
531 if (!fgets(line, MAX_LINE_LEN, file))
534 /* skip comments (lines directly beginning with '#') */
538 /* cut trailing newline from input line */
539 for (line_ptr = line; *line_ptr; line_ptr++)
541 if (*line_ptr == '\n' || *line_ptr == '\r')
548 if (strlen(line) == 0) /* special case: force empty line */
553 while (*line_ptr && current_line < max_lines)
555 boolean buffer_filled = RenderLineToBuffer(&line_ptr,
561 DrawText(x, y + current_line * font_height, buffer, font_nr);
564 last_line_was_empty = (buffer_len == 0);
574 if (buffer_len > 0 && current_line < max_lines)
576 DrawText(x, y + current_line * font_height, buffer, font_nr);