1 /***********************************************************
2 * Artsoft Retro-Game Library *
3 *----------------------------------------------------------*
4 * (c) 1994-2006 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 int 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 = (unsigned char)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;
189 /* ========================================================================= */
190 /* simple text drawing functions */
191 /* ========================================================================= */
193 void DrawInitText(char *text, int ypos, int font_nr)
195 if (window != NULL &&
197 gfx.font_bitmap_info[font_nr].bitmap != NULL)
199 int x = (video.width - getTextWidth(text, font_nr)) / 2;
201 int width = video.width;
202 int height = getFontHeight(font_nr);
204 ClearRectangle(drawto, 0, y, width, height);
205 DrawTextExt(drawto, x, y, text, font_nr, BLIT_OPAQUE);
207 /* this makes things significantly faster than directly drawing to window */
208 BlitBitmap(drawto, window, 0, y, width, height, 0, y);
212 void DrawTextF(int x, int y, int font_nr, char *format, ...)
214 char buffer[MAX_OUTPUT_LINESIZE + 1];
217 va_start(ap, format);
218 vsprintf(buffer, format, ap);
221 if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
222 Error(ERR_EXIT, "string too long in DrawTextF() -- aborting");
224 DrawText(gfx.sx + x, gfx.sy + y, buffer, font_nr);
227 void DrawTextFCentered(int y, int font_nr, char *format, ...)
229 char buffer[MAX_OUTPUT_LINESIZE + 1];
232 va_start(ap, format);
233 vsprintf(buffer, format, ap);
236 if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
237 Error(ERR_EXIT, "string too long in DrawTextFCentered() -- aborting");
239 DrawText(gfx.sx + (gfx.sxsize - getTextWidth(buffer, font_nr)) / 2,
240 gfx.sy + y, buffer, font_nr);
243 void DrawTextS(int x, int y, int font_nr, char *text)
245 DrawText(gfx.sx + x, gfx.sy + y, text, font_nr);
248 void DrawTextSCentered(int y, int font_nr, char *text)
250 DrawText(gfx.sx + (gfx.sxsize - getTextWidth(text, font_nr)) / 2,
251 gfx.sy + y, text, font_nr);
254 void DrawTextCentered(int y, int font_nr, char *text)
256 DrawText((gfx.sxsize - getTextWidth(text, font_nr)) / 2, y, text, font_nr);
259 void DrawTextSAligned(int x, int y, char *text, int font_nr, int align)
261 DrawText(gfx.sx + ALIGNED_XPOS(x, getTextWidth(text, font_nr), align),
262 gfx.sx + y, text, font_nr);
265 void DrawTextAligned(int x, int y, char *text, int font_nr, int align)
267 DrawText(ALIGNED_XPOS(x, getTextWidth(text, font_nr), align),
271 void DrawText(int x, int y, char *text, int font_nr)
273 int mask_mode = BLIT_OPAQUE;
275 if (DrawingOnBackground(x, y))
276 mask_mode = BLIT_ON_BACKGROUND;
278 DrawTextExt(drawto, x, y, text, font_nr, mask_mode);
281 redraw_mask |= REDRAW_FIELD;
282 else if (y < gfx.vy || gfx.vy == 0)
283 redraw_mask |= REDRAW_DOOR_1;
286 void DrawTextExt(DrawBuffer *dst_bitmap, int dst_x, int dst_y, char *text,
287 int font_nr, int mask_mode)
290 struct FontBitmapInfo *font = getFontBitmapInfo(font_nr);
292 int font_bitmap_id = gfx.select_font_function(font_nr);
293 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
295 int font_width = getFontWidth(font_nr);
296 int font_height = getFontHeight(font_nr);
298 int border_1 = gfx.sx + gfx.sxsize;
299 int border_2 = gfx.dx + gfx.dxsize;
300 int dst_x_start = dst_x;
304 char *text_ptr = text;
306 if (font->bitmap == NULL)
309 /* skip text to be printed outside the window (left/right will be clipped) */
310 if (dst_y < 0 || dst_y + font_height > video.height)
313 /* add offset for drawing font characters */
314 dst_x += font->draw_xoffset;
315 dst_y += font->draw_yoffset;
319 char c = *text_ptr++;
322 c = ' '; /* print space instead of newline */
324 getFontCharSource(font_nr, c, &src_bitmap, &src_x, &src_y);
326 /* clip text at the left side of the window */
334 /* clip text at the right side of the window */
336 if (dst_x + font_width > video.width)
339 /* (this does not work well when trying to print text to whole screen) */
340 if ((dst_x_start < border_1 && dst_x + font_width > border_1) ||
341 (dst_x_start < border_2 && dst_x + font_width > border_2))
345 if (mask_mode == BLIT_INVERSE) /* special mode for text gadgets */
347 /* first step: draw solid colored rectangle (use "cursor" character) */
348 if (strlen(text) == 1) /* only one char inverted => draw cursor */
350 Bitmap *cursor_bitmap;
351 int cursor_x, cursor_y;
353 getFontCharSource(font_nr, FONT_ASCII_CURSOR, &cursor_bitmap,
354 &cursor_x, &cursor_y);
356 BlitBitmap(cursor_bitmap, dst_bitmap, cursor_x, cursor_y,
357 font_width, font_height, dst_x, dst_y);
360 #if defined(TARGET_SDL)
361 /* second step: draw masked inverted character */
362 SDLCopyInverseMasked(src_bitmap, dst_bitmap, src_x, src_y,
363 font_width, font_height, dst_x, dst_y);
365 /* second step: draw masked black rectangle (use "space" character) */
366 SetClipOrigin(src_bitmap, src_bitmap->stored_clip_gc,
367 dst_x - src_x, dst_y - src_y);
368 BlitBitmapMasked(src_bitmap, dst_bitmap, 0, 0,
369 font_width, font_height, dst_x, dst_y);
372 else if (mask_mode == BLIT_MASKED || mask_mode == BLIT_ON_BACKGROUND)
374 if (mask_mode == BLIT_ON_BACKGROUND)
376 /* clear font character background */
377 ClearRectangleOnBackground(dst_bitmap, dst_x, dst_y,
378 font_width, font_height);
381 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
382 /* use special font tile clipmasks */
384 int font_pos = getFontCharPosition(font_nr, c);
386 SetClipMask(src_bitmap, font_clip_gc, font->clip_mask[font_pos]);
387 SetClipOrigin(src_bitmap, font_clip_gc, dst_x, dst_y);
390 SetClipOrigin(src_bitmap, src_bitmap->stored_clip_gc,
391 dst_x - src_x, dst_y - src_y);
394 BlitBitmapMasked(src_bitmap, dst_bitmap, src_x, src_y,
395 font_width, font_height, dst_x, dst_y);
397 else /* normal, non-masked font blitting */
399 BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y,
400 font_width, font_height, dst_x, dst_y);
408 /* ========================================================================= */
409 /* text buffer drawing functions */
410 /* ========================================================================= */
412 #define MAX_LINES_FROM_FILE 1024
414 char *GetTextBufferFromFile(char *filename, int max_lines)
420 if (filename == NULL)
423 if (!(file = fopen(filename, MODE_READ)))
426 buffer = checked_calloc(1); /* start with valid, but empty text buffer */
428 while (!feof(file) && num_lines < max_lines)
430 char line[MAX_LINE_LEN];
432 /* read next line of input file */
433 if (!fgets(line, MAX_LINE_LEN, file))
436 buffer = checked_realloc(buffer, strlen(buffer) + strlen(line) + 1);
438 strcat(buffer, line);
448 void DrawTextToTextArea_OLD(int x, int y, char *text, int font_nr, int line_length,
449 int area_xsize, int area_ysize, int mask_mode)
452 int font_height = getFontHeight(font_nr);
457 while (*text && area_line < area_ysize)
459 char buffer[MAX_OUTPUT_LINESIZE + 1];
462 for (i = 0; i < line_length && *text; i++)
463 if ((buffer[i] = *text++) == '\n')
465 buffer[MIN(i, area_xsize)] = '\0';
467 DrawTextExt(drawto, x, y + area_line * font_height, buffer, font_nr,
473 redraw_mask |= REDRAW_FIELD;
476 static boolean RenderLineToBuffer(char **src_buffer_ptr, char *dst_buffer,
477 int *dst_buffer_len, int line_length,
478 boolean last_line_was_empty)
480 char *text_ptr = *src_buffer_ptr;
481 char *buffer = dst_buffer;
482 int buffer_len = *dst_buffer_len;
483 boolean buffer_filled = FALSE;
490 /* skip leading whitespaces */
491 while (*text_ptr == ' ' || *text_ptr == '\t')
497 /* look for end of next word */
498 while (*word_ptr != ' ' && *word_ptr != '\t' && *word_ptr != '\0')
508 else if (*text_ptr == '\n') /* special case: force empty line */
513 /* prevent printing of multiple empty lines */
514 if (buffer_len > 0 || !last_line_was_empty)
515 buffer_filled = TRUE;
517 else if (word_len < line_length - buffer_len)
519 /* word fits into text buffer -- add word */
522 buffer[buffer_len++] = ' ';
524 strncpy(&buffer[buffer_len], text_ptr, word_len);
525 buffer_len += word_len;
526 buffer[buffer_len] = '\0';
527 text_ptr += word_len;
529 else if (buffer_len > 0)
531 /* not enough space left for word in text buffer -- print buffer */
533 buffer_filled = TRUE;
537 /* word does not fit at all into empty text buffer -- cut word */
539 strncpy(buffer, text_ptr, line_length);
540 buffer[line_length] = '\0';
541 text_ptr += line_length;
542 buffer_filled = TRUE;
549 *src_buffer_ptr = text_ptr;
550 *dst_buffer_len = buffer_len;
552 return buffer_filled;
556 void DrawTextWrapped_OLD(int x, int y, char *text, int font_nr, int line_length,
559 char *text_ptr = text;
560 int current_line = 0;
561 int font_height = getFontHeight(font_nr);
563 while (*text_ptr && current_line < max_lines)
565 char buffer[line_length + 1];
570 RenderLineToBuffer(&text_ptr, buffer, &buffer_len, line_length, TRUE);
572 DrawText(x, y + current_line * font_height, buffer, font_nr);
579 int DrawTextFromFile_OLD(int x, int y, char *filename, int font_nr,
580 int line_length, int max_lines, boolean wrap_text)
582 int font_height = getFontHeight(font_nr);
583 char line[MAX_LINE_LEN];
584 char buffer[line_length + 1];
586 int current_line = 0;
589 if (current_line >= max_lines)
592 if (filename == NULL)
595 if (!(file = fopen(filename, MODE_READ)))
601 while (!feof(file) && current_line < max_lines)
604 boolean last_line_was_empty = TRUE;
606 /* read next line of input file */
607 if (!fgets(line, MAX_LINE_LEN, file))
610 /* skip comments (lines directly beginning with '#') */
614 /* cut trailing newline from input line */
615 for (line_ptr = line; *line_ptr; line_ptr++)
617 if (*line_ptr == '\n' || *line_ptr == '\r')
624 if (strlen(line) == 0) /* special case: force empty line */
629 while (*line_ptr && current_line < max_lines)
632 boolean buffer_filled;
636 buffer_filled = RenderLineToBuffer(&line_ptr, buffer, &buffer_len,
637 line_length, last_line_was_empty);
641 if (strlen(line_ptr) <= line_length)
643 buffer_len = strlen(line_ptr);
644 strcpy(buffer, line_ptr);
648 buffer_len = line_length;
649 strncpy(buffer, line_ptr, line_length);
652 buffer[buffer_len] = '\0';
653 line_ptr += buffer_len;
655 buffer_filled = TRUE;
658 boolean buffer_filled = RenderLineToBuffer(&line_ptr, buffer, &buffer_len,
659 line_length, last_line_was_empty);
664 DrawText(x, y + current_line * font_height, buffer, font_nr);
667 last_line_was_empty = (buffer_len == 0);
677 if (buffer_len > 0 && current_line < max_lines)
679 DrawText(x, y + current_line * font_height, buffer, font_nr);
687 static boolean getCheckedTokenValueFromString(char *string, char **token,
692 if (!getTokenValueFromString(string, token, value))
695 if (**token != '.') /* token should begin with dot */
698 for (ptr = *token; *ptr; ptr++) /* token should contain no whitespace */
699 if (*ptr == ' ' || *ptr == '\t')
702 for (ptr = *value; *ptr; ptr++) /* value should contain no whitespace */
703 if (*ptr == ' ' || *ptr == '\t')
709 static void DrawTextBuffer_Flush(int x, int y, char *buffer, int font_nr,
710 int line_length, int cut_length, int mask_mode,
711 boolean centered, int current_line)
713 int buffer_len = strlen(buffer);
714 int font_width = getFontWidth(font_nr);
715 int font_height = getFontHeight(font_nr);
716 int offset_chars = (centered ? (line_length - buffer_len) / 2 : 0);
718 (centered ? font_width * (line_length - buffer_len) / 2 : 0);
719 int final_cut_length = MAX(0, cut_length - offset_chars);
720 int xx = x + offset_xsize;
721 int yy = y + current_line * font_height;
723 buffer[final_cut_length] = '\0';
726 DrawTextExt(drawto, xx, yy, buffer, font_nr, mask_mode);
728 DrawText(xx, yy, buffer, font_nr);
731 int DrawTextBuffer(int x, int y, char *text_buffer, int font_nr,
732 int line_length, int cut_length, int max_lines,
733 int mask_mode, boolean autowrap, boolean centered,
734 boolean skip_comments)
737 int font_width = getFontWidth(font_nr);
738 int font_height = getFontHeight(font_nr);
740 char buffer[line_length + 1];
742 int current_line = 0;
744 if (text_buffer == NULL || *text_buffer == '\0')
747 if (current_line >= max_lines)
750 if (cut_length == -1)
751 cut_length = line_length;
756 while (*text_buffer && current_line < max_lines)
758 char line[MAX_LINE_LEN + 1];
760 boolean last_line_was_empty = TRUE;
762 int num_line_chars = MAX_LINE_LEN;
764 int num_line_chars = (autowrap ? MAX_LINE_LEN : line_length);
768 /* copy next line from text buffer to line buffer (nearly fgets() style) */
769 for (i = 0; i < num_line_chars && *text_buffer; i++)
770 if ((line[i] = *text_buffer++) == '\n')
774 /* prevent 'num_line_chars' sized lines to cause additional empty line */
775 if (i == num_line_chars && *text_buffer == '\n')
778 /* skip comments (lines directly beginning with '#') */
779 if (line[0] == '#' && skip_comments)
783 /* try to read generic token/value pair definition after comment sign */
784 if (getCheckedTokenValueFromString(line + 1, &token, &value))
786 /* if found, flush the current buffer, if non-empty */
787 if (buffer_len > 0 && current_line < max_lines)
789 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
790 mask_mode, centered, current_line);
798 if (strEqual(token, ".autowrap"))
799 autowrap = get_boolean_from_string(value);
800 else if (strEqual(token, ".centered"))
801 centered = get_boolean_from_string(value);
802 else if (strEqual(token, ".skip_comments"))
803 skip_comments = get_boolean_from_string(value);
809 /* cut trailing newline and carriage return from input line */
810 for (line_ptr = line; *line_ptr; line_ptr++)
812 if (*line_ptr == '\n' || *line_ptr == '\r')
819 if (strlen(line) == 0) /* special case: force empty line */
824 while (*line_ptr && current_line < max_lines)
826 boolean buffer_filled;
830 buffer_filled = RenderLineToBuffer(&line_ptr, buffer, &buffer_len,
831 line_length, last_line_was_empty);
835 if (strlen(line_ptr) <= line_length)
837 buffer_len = strlen(line_ptr);
838 strcpy(buffer, line_ptr);
842 buffer_len = line_length;
843 strncpy(buffer, line_ptr, line_length);
846 buffer[buffer_len] = '\0';
847 line_ptr += buffer_len;
849 buffer_filled = TRUE;
855 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
856 mask_mode, centered, current_line);
858 int offset_chars = (centered ? (line_length - buffer_len) / 2 : 0);
860 (centered ? font_width * (line_length - buffer_len) / 2 : 0);
861 int final_cut_length = MAX(0, cut_length - offset_chars);
862 int xx = x + offset_xsize;
864 buffer[final_cut_length] = '\0';
867 DrawTextExt(drawto, xx, y + current_line * font_height, buffer,
870 DrawText(xx, y + current_line * font_height, buffer, font_nr);
875 last_line_was_empty = (buffer_len == 0);
883 if (buffer_len > 0 && current_line < max_lines)
886 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
887 mask_mode, centered, current_line);
889 int offset_chars = (centered ? (line_length - buffer_len) / 2 : 0);
891 (centered ? font_width * (line_length - buffer_len) / 2 : 0);
892 int final_cut_length = MAX(0, cut_length - offset_chars);
893 int xx = x + offset_xsize;
895 buffer[final_cut_length] = '\0';
898 DrawTextExt(drawto, xx, y + current_line * font_height, buffer,
901 DrawText(xx, y + current_line * font_height, buffer, font_nr);
910 int DrawTextFile(int x, int y, char *filename, int font_nr,
911 int line_length, int cut_length, int max_lines,
912 int mask_mode, boolean autowrap, boolean centered,
913 boolean skip_comments)
915 char *text_buffer = GetTextBufferFromFile(filename, MAX_LINES_FROM_FILE);
916 int num_lines_printed = DrawTextBuffer(x, y, text_buffer, font_nr,
917 line_length, cut_length, max_lines,
918 mask_mode, autowrap, centered,
920 checked_free(text_buffer);
922 return num_lines_printed;
926 void DrawTextWrapped(int x, int y, char *text, int font_nr, int line_length,
929 DrawTextBuffer(x, y, text, font_nr, line_length, -1, max_lines, -1, TRUE,
933 void DrawTextToTextArea(int x, int y, char *text, int font_nr, int line_length,
934 int cut_length, int max_lines, int mask_mode)
936 DrawTextBuffer(x, y, text, font_nr, line_length, cut_length, max_lines,
937 mask_mode, FALSE, FALSE, FALSE);