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),
111 int (*get_font_from_token_function)(char *))
113 gfx.num_fonts = num_fonts;
114 gfx.font_bitmap_info = font_bitmap_info;
115 gfx.select_font_function = select_font_function;
116 gfx.get_font_from_token_function = get_font_from_token_function;
118 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
123 void FreeFontInfo(struct FontBitmapInfo *font_bitmap_info)
125 if (font_bitmap_info == NULL)
128 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
132 free(font_bitmap_info);
135 struct FontBitmapInfo *getFontBitmapInfo(int font_nr)
137 int font_bitmap_id = gfx.select_font_function(font_nr);
139 return &gfx.font_bitmap_info[font_bitmap_id];
142 int getFontWidth(int font_nr)
144 int font_bitmap_id = gfx.select_font_function(font_nr);
146 return gfx.font_bitmap_info[font_bitmap_id].width;
149 int getFontHeight(int font_nr)
151 int font_bitmap_id = gfx.select_font_function(font_nr);
153 return gfx.font_bitmap_info[font_bitmap_id].height;
156 int getTextWidth(char *text, int font_nr)
158 return (text != NULL ? strlen(text) * getFontWidth(font_nr) : 0);
161 static int getFontCharPosition(int font_nr, char c)
163 int font_bitmap_id = gfx.select_font_function(font_nr);
164 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
165 boolean default_font = (font->num_chars == DEFAULT_NUM_CHARS_PER_FONT);
166 int font_pos = (unsigned char)c - 32;
168 /* map some special characters to their ascii values in default font */
170 font_pos = MAP_FONT_ASCII(c) - 32;
172 /* this allows dynamic special characters together with special font */
173 if (font_pos < 0 || font_pos >= font->num_chars)
179 void getFontCharSource(int font_nr, char c, Bitmap **bitmap, int *x, int *y)
181 int font_bitmap_id = gfx.select_font_function(font_nr);
182 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
183 int font_pos = getFontCharPosition(font_nr, c);
185 *bitmap = font->bitmap;
186 *x = font->src_x + (font_pos % font->num_chars_per_line) * font->width;
187 *y = font->src_y + (font_pos / font->num_chars_per_line) * font->height;
191 /* ========================================================================= */
192 /* simple text drawing functions */
193 /* ========================================================================= */
195 void DrawInitText(char *text, int ypos, int font_nr)
197 if (window != NULL &&
199 gfx.font_bitmap_info[font_nr].bitmap != NULL)
201 int x = (video.width - getTextWidth(text, font_nr)) / 2;
203 int width = video.width;
204 int height = getFontHeight(font_nr);
206 ClearRectangle(drawto, 0, y, width, height);
207 DrawTextExt(drawto, x, y, text, font_nr, BLIT_OPAQUE);
209 /* this makes things significantly faster than directly drawing to window */
210 BlitBitmap(drawto, window, 0, y, width, height, 0, y);
214 void DrawTextF(int x, int y, int font_nr, char *format, ...)
216 char buffer[MAX_OUTPUT_LINESIZE + 1];
219 va_start(ap, format);
220 vsprintf(buffer, format, ap);
223 if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
224 Error(ERR_EXIT, "string too long in DrawTextF() -- aborting");
226 DrawText(gfx.sx + x, gfx.sy + y, buffer, font_nr);
229 void DrawTextFCentered(int y, int font_nr, char *format, ...)
231 char buffer[MAX_OUTPUT_LINESIZE + 1];
234 va_start(ap, format);
235 vsprintf(buffer, format, ap);
238 if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
239 Error(ERR_EXIT, "string too long in DrawTextFCentered() -- aborting");
241 DrawText(gfx.sx + (gfx.sxsize - getTextWidth(buffer, font_nr)) / 2,
242 gfx.sy + y, buffer, font_nr);
245 void DrawTextS(int x, int y, int font_nr, char *text)
247 DrawText(gfx.sx + x, gfx.sy + y, text, font_nr);
250 void DrawTextSCentered(int y, int font_nr, char *text)
252 DrawText(gfx.sx + (gfx.sxsize - getTextWidth(text, font_nr)) / 2,
253 gfx.sy + y, text, font_nr);
256 void DrawTextCentered(int y, int font_nr, char *text)
258 DrawText((gfx.sxsize - getTextWidth(text, font_nr)) / 2, y, text, font_nr);
261 void DrawTextSAligned(int x, int y, char *text, int font_nr, int align)
263 DrawText(gfx.sx + ALIGNED_XPOS(x, getTextWidth(text, font_nr), align),
264 gfx.sx + y, text, font_nr);
267 void DrawTextAligned(int x, int y, char *text, int font_nr, int align)
269 DrawText(ALIGNED_XPOS(x, getTextWidth(text, font_nr), align),
273 void DrawText(int x, int y, char *text, int font_nr)
275 int mask_mode = BLIT_OPAQUE;
277 if (DrawingOnBackground(x, y))
278 mask_mode = BLIT_ON_BACKGROUND;
280 DrawTextExt(drawto, x, y, text, font_nr, mask_mode);
283 redraw_mask |= REDRAW_FIELD;
284 else if (y < gfx.vy || gfx.vy == 0)
285 redraw_mask |= REDRAW_DOOR_1;
288 void DrawTextExt(DrawBuffer *dst_bitmap, int dst_x, int dst_y, char *text,
289 int font_nr, int mask_mode)
292 struct FontBitmapInfo *font = getFontBitmapInfo(font_nr);
294 int font_bitmap_id = gfx.select_font_function(font_nr);
295 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
297 int font_width = getFontWidth(font_nr);
298 int font_height = getFontHeight(font_nr);
300 int border_1 = gfx.sx + gfx.sxsize;
301 int border_2 = gfx.dx + gfx.dxsize;
302 int dst_x_start = dst_x;
306 char *text_ptr = text;
308 if (font->bitmap == NULL)
311 /* skip text to be printed outside the window (left/right will be clipped) */
312 if (dst_y < 0 || dst_y + font_height > video.height)
315 /* add offset for drawing font characters */
316 dst_x += font->draw_xoffset;
317 dst_y += font->draw_yoffset;
321 char c = *text_ptr++;
324 c = ' '; /* print space instead of newline */
326 getFontCharSource(font_nr, c, &src_bitmap, &src_x, &src_y);
328 /* clip text at the left side of the window */
336 /* clip text at the right side of the window */
338 if (dst_x + font_width > video.width)
341 /* (this does not work well when trying to print text to whole screen) */
342 if ((dst_x_start < border_1 && dst_x + font_width > border_1) ||
343 (dst_x_start < border_2 && dst_x + font_width > border_2))
347 if (mask_mode == BLIT_INVERSE) /* special mode for text gadgets */
349 /* first step: draw solid colored rectangle (use "cursor" character) */
350 if (strlen(text) == 1) /* only one char inverted => draw cursor */
352 Bitmap *cursor_bitmap;
353 int cursor_x, cursor_y;
355 getFontCharSource(font_nr, FONT_ASCII_CURSOR, &cursor_bitmap,
356 &cursor_x, &cursor_y);
358 BlitBitmap(cursor_bitmap, dst_bitmap, cursor_x, cursor_y,
359 font_width, font_height, dst_x, dst_y);
362 #if defined(TARGET_SDL)
363 /* second step: draw masked inverted character */
364 SDLCopyInverseMasked(src_bitmap, dst_bitmap, src_x, src_y,
365 font_width, font_height, dst_x, dst_y);
367 /* second step: draw masked black rectangle (use "space" character) */
368 SetClipOrigin(src_bitmap, src_bitmap->stored_clip_gc,
369 dst_x - src_x, dst_y - src_y);
370 BlitBitmapMasked(src_bitmap, dst_bitmap, 0, 0,
371 font_width, font_height, dst_x, dst_y);
374 else if (mask_mode == BLIT_MASKED || mask_mode == BLIT_ON_BACKGROUND)
376 if (mask_mode == BLIT_ON_BACKGROUND)
378 /* clear font character background */
379 ClearRectangleOnBackground(dst_bitmap, dst_x, dst_y,
380 font_width, font_height);
383 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
384 /* use special font tile clipmasks */
386 int font_pos = getFontCharPosition(font_nr, c);
388 SetClipMask(src_bitmap, font_clip_gc, font->clip_mask[font_pos]);
389 SetClipOrigin(src_bitmap, font_clip_gc, dst_x, dst_y);
392 SetClipOrigin(src_bitmap, src_bitmap->stored_clip_gc,
393 dst_x - src_x, dst_y - src_y);
396 BlitBitmapMasked(src_bitmap, dst_bitmap, src_x, src_y,
397 font_width, font_height, dst_x, dst_y);
399 else /* normal, non-masked font blitting */
401 BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y,
402 font_width, font_height, dst_x, dst_y);
410 /* ========================================================================= */
411 /* text buffer drawing functions */
412 /* ========================================================================= */
414 #define MAX_LINES_FROM_FILE 1024
416 char *GetTextBufferFromFile(char *filename, int max_lines)
422 if (filename == NULL)
425 if (!(file = fopen(filename, MODE_READ)))
428 buffer = checked_calloc(1); /* start with valid, but empty text buffer */
430 while (!feof(file) && num_lines < max_lines)
432 char line[MAX_LINE_LEN];
434 /* read next line of input file */
435 if (!fgets(line, MAX_LINE_LEN, file))
438 buffer = checked_realloc(buffer, strlen(buffer) + strlen(line) + 1);
440 strcat(buffer, line);
450 void DrawTextToTextArea_OLD(int x, int y, char *text, int font_nr, int line_length,
451 int area_xsize, int area_ysize, int mask_mode)
454 int font_height = getFontHeight(font_nr);
459 while (*text && area_line < area_ysize)
461 char buffer[MAX_OUTPUT_LINESIZE + 1];
464 for (i = 0; i < line_length && *text; i++)
465 if ((buffer[i] = *text++) == '\n')
467 buffer[MIN(i, area_xsize)] = '\0';
469 DrawTextExt(drawto, x, y + area_line * font_height, buffer, font_nr,
475 redraw_mask |= REDRAW_FIELD;
478 static boolean RenderLineToBuffer(char **src_buffer_ptr, char *dst_buffer,
479 int *dst_buffer_len, int line_length,
480 boolean last_line_was_empty)
482 char *text_ptr = *src_buffer_ptr;
483 char *buffer = dst_buffer;
484 int buffer_len = *dst_buffer_len;
485 boolean buffer_filled = FALSE;
492 /* skip leading whitespaces */
493 while (*text_ptr == ' ' || *text_ptr == '\t')
499 /* look for end of next word */
500 while (*word_ptr != ' ' && *word_ptr != '\t' && *word_ptr != '\0')
510 else if (*text_ptr == '\n') /* special case: force empty line */
515 /* prevent printing of multiple empty lines */
516 if (buffer_len > 0 || !last_line_was_empty)
517 buffer_filled = TRUE;
519 else if (word_len < line_length - buffer_len)
521 /* word fits into text buffer -- add word */
524 buffer[buffer_len++] = ' ';
526 strncpy(&buffer[buffer_len], text_ptr, word_len);
527 buffer_len += word_len;
528 buffer[buffer_len] = '\0';
529 text_ptr += word_len;
531 else if (buffer_len > 0)
533 /* not enough space left for word in text buffer -- print buffer */
535 buffer_filled = TRUE;
539 /* word does not fit at all into empty text buffer -- cut word */
541 strncpy(buffer, text_ptr, line_length);
542 buffer[line_length] = '\0';
543 text_ptr += line_length;
544 buffer_filled = TRUE;
551 *src_buffer_ptr = text_ptr;
552 *dst_buffer_len = buffer_len;
554 return buffer_filled;
558 void DrawTextWrapped_OLD(int x, int y, char *text, int font_nr, int line_length,
561 char *text_ptr = text;
562 int current_line = 0;
563 int font_height = getFontHeight(font_nr);
565 while (*text_ptr && current_line < max_lines)
567 char buffer[line_length + 1];
572 RenderLineToBuffer(&text_ptr, buffer, &buffer_len, line_length, TRUE);
574 DrawText(x, y + current_line * font_height, buffer, font_nr);
581 int DrawTextFromFile_OLD(int x, int y, char *filename, int font_nr,
582 int line_length, int max_lines, boolean wrap_text)
584 int font_height = getFontHeight(font_nr);
585 char line[MAX_LINE_LEN];
586 char buffer[line_length + 1];
588 int current_line = 0;
591 if (current_line >= max_lines)
594 if (filename == NULL)
597 if (!(file = fopen(filename, MODE_READ)))
603 while (!feof(file) && current_line < max_lines)
606 boolean last_line_was_empty = TRUE;
608 /* read next line of input file */
609 if (!fgets(line, MAX_LINE_LEN, file))
612 /* skip comments (lines directly beginning with '#') */
616 /* cut trailing newline from input line */
617 for (line_ptr = line; *line_ptr; line_ptr++)
619 if (*line_ptr == '\n' || *line_ptr == '\r')
626 if (strlen(line) == 0) /* special case: force empty line */
631 while (*line_ptr && current_line < max_lines)
634 boolean buffer_filled;
638 buffer_filled = RenderLineToBuffer(&line_ptr, buffer, &buffer_len,
639 line_length, last_line_was_empty);
643 if (strlen(line_ptr) <= line_length)
645 buffer_len = strlen(line_ptr);
646 strcpy(buffer, line_ptr);
650 buffer_len = line_length;
651 strncpy(buffer, line_ptr, line_length);
654 buffer[buffer_len] = '\0';
655 line_ptr += buffer_len;
657 buffer_filled = TRUE;
660 boolean buffer_filled = RenderLineToBuffer(&line_ptr, buffer, &buffer_len,
661 line_length, last_line_was_empty);
666 DrawText(x, y + current_line * font_height, buffer, font_nr);
669 last_line_was_empty = (buffer_len == 0);
679 if (buffer_len > 0 && current_line < max_lines)
681 DrawText(x, y + current_line * font_height, buffer, font_nr);
689 static boolean getCheckedTokenValueFromString(char *string, char **token,
694 if (!getTokenValueFromString(string, token, value))
697 if (**token != '.') /* token should begin with dot */
700 for (ptr = *token; *ptr; ptr++) /* token should contain no whitespace */
701 if (*ptr == ' ' || *ptr == '\t')
704 for (ptr = *value; *ptr; ptr++) /* value should contain no whitespace */
705 if (*ptr == ' ' || *ptr == '\t')
711 static void DrawTextBuffer_Flush(int x, int y, char *buffer, int font_nr,
712 int line_length, int cut_length, int mask_mode,
713 boolean centered, int current_line)
715 int buffer_len = strlen(buffer);
716 int font_width = getFontWidth(font_nr);
717 int font_height = getFontHeight(font_nr);
718 int offset_chars = (centered ? (line_length - buffer_len) / 2 : 0);
720 (centered ? font_width * (line_length - buffer_len) / 2 : 0);
721 int final_cut_length = MAX(0, cut_length - offset_chars);
722 int xx = x + offset_xsize;
723 int yy = y + current_line * font_height;
725 buffer[final_cut_length] = '\0';
728 DrawTextExt(drawto, xx, yy, buffer, font_nr, mask_mode);
730 DrawText(xx, yy, buffer, font_nr);
733 int DrawTextBuffer(int x, int y, char *text_buffer, int font_nr,
734 int line_length, int cut_length, int max_lines,
735 int mask_mode, boolean autowrap, boolean centered,
736 boolean parse_comments)
739 int font_width = getFontWidth(font_nr);
740 int font_height = getFontHeight(font_nr);
742 char buffer[line_length + 1];
744 int current_line = 0;
746 if (text_buffer == NULL || *text_buffer == '\0')
749 if (current_line >= max_lines)
752 if (cut_length == -1)
753 cut_length = line_length;
758 while (*text_buffer && current_line < max_lines)
760 char line[MAX_LINE_LEN + 1];
762 boolean last_line_was_empty = TRUE;
764 int num_line_chars = MAX_LINE_LEN;
766 int num_line_chars = (autowrap ? MAX_LINE_LEN : line_length);
770 /* copy next line from text buffer to line buffer (nearly fgets() style) */
771 for (i = 0; i < num_line_chars && *text_buffer; i++)
772 if ((line[i] = *text_buffer++) == '\n')
776 /* prevent 'num_line_chars' sized lines to cause additional empty line */
777 if (i == num_line_chars && *text_buffer == '\n')
780 /* skip comments (lines directly beginning with '#') */
781 if (line[0] == '#' && parse_comments)
785 /* try to read generic token/value pair definition after comment sign */
786 if (getCheckedTokenValueFromString(line + 1, &token, &value))
788 /* if found, flush the current buffer, if non-empty */
789 if (buffer_len > 0 && current_line < max_lines)
791 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
792 mask_mode, centered, current_line);
800 if (strEqual(token, ".font"))
801 font_nr = gfx.get_font_from_token_function(value);
802 else if (strEqual(token, ".autowrap"))
803 autowrap = get_boolean_from_string(value);
804 else if (strEqual(token, ".centered"))
805 centered = get_boolean_from_string(value);
806 else if (strEqual(token, ".parse_comments"))
807 parse_comments = get_boolean_from_string(value);
813 /* cut trailing newline and carriage return from input line */
814 for (line_ptr = line; *line_ptr; line_ptr++)
816 if (*line_ptr == '\n' || *line_ptr == '\r')
823 if (strlen(line) == 0) /* special case: force empty line */
828 while (*line_ptr && current_line < max_lines)
830 boolean buffer_filled;
834 buffer_filled = RenderLineToBuffer(&line_ptr, buffer, &buffer_len,
835 line_length, last_line_was_empty);
839 if (strlen(line_ptr) <= line_length)
841 buffer_len = strlen(line_ptr);
842 strcpy(buffer, line_ptr);
846 buffer_len = line_length;
847 strncpy(buffer, line_ptr, line_length);
850 buffer[buffer_len] = '\0';
851 line_ptr += buffer_len;
853 buffer_filled = TRUE;
859 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
860 mask_mode, centered, current_line);
862 int offset_chars = (centered ? (line_length - buffer_len) / 2 : 0);
864 (centered ? font_width * (line_length - buffer_len) / 2 : 0);
865 int final_cut_length = MAX(0, cut_length - offset_chars);
866 int xx = x + offset_xsize;
868 buffer[final_cut_length] = '\0';
871 DrawTextExt(drawto, xx, y + current_line * font_height, buffer,
874 DrawText(xx, y + current_line * font_height, buffer, font_nr);
879 last_line_was_empty = (buffer_len == 0);
887 if (buffer_len > 0 && current_line < max_lines)
890 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
891 mask_mode, centered, current_line);
893 int offset_chars = (centered ? (line_length - buffer_len) / 2 : 0);
895 (centered ? font_width * (line_length - buffer_len) / 2 : 0);
896 int final_cut_length = MAX(0, cut_length - offset_chars);
897 int xx = x + offset_xsize;
899 buffer[final_cut_length] = '\0';
902 DrawTextExt(drawto, xx, y + current_line * font_height, buffer,
905 DrawText(xx, y + current_line * font_height, buffer, font_nr);
914 int DrawTextFile(int x, int y, char *filename, int font_nr,
915 int line_length, int cut_length, int max_lines,
916 int mask_mode, boolean autowrap, boolean centered,
917 boolean parse_comments)
919 char *text_buffer = GetTextBufferFromFile(filename, MAX_LINES_FROM_FILE);
920 int num_lines_printed = DrawTextBuffer(x, y, text_buffer, font_nr,
921 line_length, cut_length, max_lines,
922 mask_mode, autowrap, centered,
924 checked_free(text_buffer);
926 return num_lines_printed;
930 void DrawTextWrapped(int x, int y, char *text, int font_nr, int line_length,
933 DrawTextBuffer(x, y, text, font_nr, line_length, -1, max_lines, -1, TRUE,
937 void DrawTextToTextArea(int x, int y, char *text, int font_nr, int line_length,
938 int cut_length, int max_lines, int mask_mode)
940 DrawTextBuffer(x, y, text, font_nr, line_length, cut_length, max_lines,
941 mask_mode, FALSE, FALSE, FALSE);