1 // ============================================================================
2 // Artsoft Retro-Game Library
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
7 // http://www.artsoft.org/
8 // ----------------------------------------------------------------------------
10 // ============================================================================
19 /* ========================================================================= */
21 /* ========================================================================= */
23 void InitFontInfo(struct FontBitmapInfo *font_bitmap_info, int num_fonts,
24 int (*select_font_function)(int),
25 int (*get_font_from_token_function)(char *))
27 gfx.num_fonts = num_fonts;
28 gfx.font_bitmap_info = font_bitmap_info;
29 gfx.select_font_function = select_font_function;
30 gfx.get_font_from_token_function = get_font_from_token_function;
33 void FreeFontInfo(struct FontBitmapInfo *font_bitmap_info)
35 if (font_bitmap_info == NULL)
38 free(font_bitmap_info);
41 struct FontBitmapInfo *getFontBitmapInfo(int font_nr)
43 int font_bitmap_id = gfx.select_font_function(font_nr);
45 return &gfx.font_bitmap_info[font_bitmap_id];
48 int getFontWidth(int font_nr)
50 int font_bitmap_id = gfx.select_font_function(font_nr);
52 return gfx.font_bitmap_info[font_bitmap_id].width;
55 int getFontHeight(int font_nr)
57 int font_bitmap_id = gfx.select_font_function(font_nr);
59 return gfx.font_bitmap_info[font_bitmap_id].height;
62 int getTextWidth(char *text, int font_nr)
64 return (text != NULL ? strlen(text) * getFontWidth(font_nr) : 0);
67 static int getFontCharPosition(int font_nr, char c)
69 int font_bitmap_id = gfx.select_font_function(font_nr);
70 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
71 boolean default_font = (font->num_chars == DEFAULT_NUM_CHARS_PER_FONT);
72 int font_pos = (unsigned char)c - 32;
74 /* map some special characters to their ascii values in default font */
76 font_pos = MAP_FONT_ASCII(c) - 32;
78 /* this allows dynamic special characters together with special font */
79 if (font_pos < 0 || font_pos >= font->num_chars)
85 void getFontCharSource(int font_nr, char c, Bitmap **bitmap, int *x, int *y)
87 int font_bitmap_id = gfx.select_font_function(font_nr);
88 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
89 int font_pos = getFontCharPosition(font_nr, c);
91 *bitmap = font->bitmap;
92 *x = font->src_x + (font_pos % font->num_chars_per_line) * font->width;
93 *y = font->src_y + (font_pos / font->num_chars_per_line) * font->height;
97 /* ========================================================================= */
98 /* text string helper functions */
99 /* ========================================================================= */
101 int maxWordLengthInString(char *text)
104 int word_len = 0, max_word_len = 0;
106 for (text_ptr = text; *text_ptr; text_ptr++)
108 word_len = (*text_ptr != ' ' ? word_len + 1 : 0);
110 max_word_len = MAX(word_len, max_word_len);
117 /* ========================================================================= */
118 /* simple text drawing functions */
119 /* ========================================================================= */
121 void DrawInitTextExt(char *text, int ypos, int font_nr, boolean force)
123 LimitScreenUpdates(TRUE);
127 if (window != NULL &&
128 gfx.draw_init_text &&
130 gfx.font_bitmap_info[font_nr].bitmap != NULL)
132 int x = (video.width - getTextWidth(text, font_nr)) / 2;
134 int width = video.width;
135 int height = getFontHeight(font_nr);
137 ClearRectangle(drawto, 0, y, width, height);
138 DrawTextExt(drawto, x, y, text, font_nr, BLIT_OPAQUE);
140 BlitBitmap(drawto, window, 0, 0, video.width, video.height, 0, 0);
144 void DrawInitText(char *text, int ypos, int font_nr)
146 // DrawInitTextExt(text, ypos, font_nr, TRUE);
147 DrawInitTextExt(text, ypos, font_nr, FALSE);
150 void DrawInitTextAlways(char *text, int ypos, int font_nr)
152 DrawInitTextExt(text, ypos, font_nr, TRUE);
155 void DrawInitTextIfNeeded(char *text, int ypos, int font_nr)
157 DrawInitTextExt(text, ypos, font_nr, FALSE);
160 void DrawTextF(int x, int y, int font_nr, char *format, ...)
162 char buffer[MAX_OUTPUT_LINESIZE + 1];
165 va_start(ap, format);
166 vsprintf(buffer, format, ap);
169 if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
170 Error(ERR_EXIT, "string too long in DrawTextF() -- aborting");
172 DrawText(gfx.sx + x, gfx.sy + y, buffer, font_nr);
175 void DrawTextFCentered(int y, int font_nr, char *format, ...)
177 char buffer[MAX_OUTPUT_LINESIZE + 1];
180 va_start(ap, format);
181 vsprintf(buffer, format, ap);
184 if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
185 Error(ERR_EXIT, "string too long in DrawTextFCentered() -- aborting");
187 DrawText(gfx.sx + (gfx.sxsize - getTextWidth(buffer, font_nr)) / 2,
188 gfx.sy + y, buffer, font_nr);
191 void DrawTextS(int x, int y, int font_nr, char *text)
193 DrawText(gfx.sx + x, gfx.sy + y, text, font_nr);
196 void DrawTextSCentered(int y, int font_nr, char *text)
198 DrawText(gfx.sx + (gfx.sxsize - getTextWidth(text, font_nr)) / 2,
199 gfx.sy + y, text, font_nr);
202 void DrawTextCentered(int y, int font_nr, char *text)
204 DrawText((gfx.sxsize - getTextWidth(text, font_nr)) / 2, y, text, font_nr);
207 void DrawTextSAligned(int x, int y, char *text, int font_nr, int align)
209 DrawText(gfx.sx + ALIGNED_XPOS(x, getTextWidth(text, font_nr), align),
210 gfx.sx + y, text, font_nr);
213 void DrawTextAligned(int x, int y, char *text, int font_nr, int align)
215 DrawText(ALIGNED_XPOS(x, getTextWidth(text, font_nr), align),
219 void DrawText(int x, int y, char *text, int font_nr)
221 int mask_mode = BLIT_OPAQUE;
223 if (DrawingOnBackground(x, y))
224 mask_mode = BLIT_ON_BACKGROUND;
226 DrawTextExt(drawto, x, y, text, font_nr, mask_mode);
228 if (IN_GFX_FIELD_FULL(x, y))
229 redraw_mask |= REDRAW_FIELD;
230 else if (IN_GFX_DOOR_1(x, y))
231 redraw_mask |= REDRAW_DOOR_1;
232 else if (IN_GFX_DOOR_2(x, y))
233 redraw_mask |= REDRAW_DOOR_2;
234 else if (IN_GFX_DOOR_3(x, y))
235 redraw_mask |= REDRAW_DOOR_3;
237 redraw_mask |= REDRAW_ALL;
240 void DrawTextExt(DrawBuffer *dst_bitmap, int dst_x, int dst_y, char *text,
241 int font_nr, int mask_mode)
243 struct FontBitmapInfo *font = getFontBitmapInfo(font_nr);
244 int font_width = getFontWidth(font_nr);
245 int font_height = getFontHeight(font_nr);
248 char *text_ptr = text;
250 if (font->bitmap == NULL)
253 /* skip text to be printed outside the window (left/right will be clipped) */
254 if (dst_y < 0 || dst_y + font_height > video.height)
257 /* add offset for drawing font characters */
258 dst_x += font->draw_xoffset;
259 dst_y += font->draw_yoffset;
263 char c = *text_ptr++;
266 c = ' '; /* print space instead of newline */
268 getFontCharSource(font_nr, c, &src_bitmap, &src_x, &src_y);
270 /* clip text at the left side of the window */
278 /* clip text at the right side of the window */
279 if (dst_x + font_width > video.width)
282 if (mask_mode == BLIT_INVERSE) /* special mode for text gadgets */
284 /* first step: draw solid colored rectangle (use "cursor" character) */
285 if (strlen(text) == 1) /* only one char inverted => draw cursor */
287 Bitmap *cursor_bitmap;
288 int cursor_x, cursor_y;
290 getFontCharSource(font_nr, FONT_ASCII_CURSOR, &cursor_bitmap,
291 &cursor_x, &cursor_y);
293 BlitBitmap(cursor_bitmap, dst_bitmap, cursor_x, cursor_y,
294 font_width, font_height, dst_x, dst_y);
297 /* second step: draw masked inverted character */
298 SDLCopyInverseMasked(src_bitmap, dst_bitmap, src_x, src_y,
299 font_width, font_height, dst_x, dst_y);
301 else if (mask_mode == BLIT_MASKED || mask_mode == BLIT_ON_BACKGROUND)
303 if (mask_mode == BLIT_ON_BACKGROUND)
305 /* clear font character background */
306 ClearRectangleOnBackground(dst_bitmap, dst_x, dst_y,
307 font_width, font_height);
310 BlitBitmapMasked(src_bitmap, dst_bitmap, src_x, src_y,
311 font_width, font_height, dst_x, dst_y);
313 else /* normal, non-masked font blitting */
315 BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y,
316 font_width, font_height, dst_x, dst_y);
324 /* ========================================================================= */
325 /* text buffer drawing functions */
326 /* ========================================================================= */
328 #define MAX_LINES_FROM_FILE 1024
330 char *GetTextBufferFromFile(char *filename, int max_lines)
336 if (filename == NULL)
339 if (!(file = openFile(filename, MODE_READ)))
342 buffer = checked_calloc(1); /* start with valid, but empty text buffer */
344 while (!checkEndOfFile(file) && num_lines < max_lines)
346 char line[MAX_LINE_LEN];
348 /* read next line of input file */
349 if (!getStringFromFile(file, line, MAX_LINE_LEN))
352 buffer = checked_realloc(buffer, strlen(buffer) + strlen(line) + 1);
354 strcat(buffer, line);
364 static boolean RenderLineToBuffer(char **src_buffer_ptr, char *dst_buffer,
365 int *dst_buffer_len, int line_length,
366 boolean last_line_was_empty)
368 char *text_ptr = *src_buffer_ptr;
369 char *buffer = dst_buffer;
370 int buffer_len = *dst_buffer_len;
371 boolean buffer_filled = FALSE;
378 /* skip leading whitespaces */
379 while (*text_ptr == ' ' || *text_ptr == '\t')
385 /* look for end of next word */
386 while (*word_ptr != ' ' && *word_ptr != '\t' && *word_ptr != '\0')
396 else if (*text_ptr == '\n') /* special case: force empty line */
401 /* prevent printing of multiple empty lines */
402 if (buffer_len > 0 || !last_line_was_empty)
403 buffer_filled = TRUE;
405 else if (word_len < line_length - buffer_len)
407 /* word fits into text buffer -- add word */
410 buffer[buffer_len++] = ' ';
412 strncpy(&buffer[buffer_len], text_ptr, word_len);
413 buffer_len += word_len;
414 buffer[buffer_len] = '\0';
415 text_ptr += word_len;
417 else if (buffer_len > 0)
419 /* not enough space left for word in text buffer -- print buffer */
421 buffer_filled = TRUE;
425 /* word does not fit at all into empty text buffer -- cut word */
427 strncpy(buffer, text_ptr, line_length);
428 buffer[line_length] = '\0';
429 text_ptr += line_length;
430 buffer_filled = TRUE;
437 *src_buffer_ptr = text_ptr;
438 *dst_buffer_len = buffer_len;
440 return buffer_filled;
443 static boolean getCheckedTokenValueFromString(char *string, char **token,
448 if (!getTokenValueFromString(string, token, value))
451 if (**token != '.') /* token should begin with dot */
454 for (ptr = *token; *ptr; ptr++) /* token should contain no whitespace */
455 if (*ptr == ' ' || *ptr == '\t')
458 for (ptr = *value; *ptr; ptr++) /* value should contain no whitespace */
459 if (*ptr == ' ' || *ptr == '\t')
465 static void DrawTextBuffer_Flush(int x, int y, char *buffer, int font_nr,
466 int line_length, int cut_length,
467 int line_spacing, int mask_mode,
468 boolean centered, int current_line)
470 int buffer_len = strlen(buffer);
471 int font_width = getFontWidth(font_nr);
472 int font_height = getFontHeight(font_nr);
473 int offset_chars = (centered ? (line_length - buffer_len) / 2 : 0);
475 (centered ? font_width * (line_length - buffer_len) / 2 : 0);
476 int final_cut_length = MAX(0, cut_length - offset_chars);
477 int xx = x + offset_xsize;
478 int yy = y + current_line * (font_height + line_spacing);
480 buffer[final_cut_length] = '\0';
483 DrawTextExt(drawto, xx, yy, buffer, font_nr, mask_mode);
485 DrawText(xx, yy, buffer, font_nr);
488 int DrawTextBuffer(int x, int y, char *text_buffer, int font_nr,
489 int line_length, int cut_length, int max_lines,
490 int line_spacing, int mask_mode, boolean autowrap,
491 boolean centered, boolean parse_comments)
493 char buffer[line_length + 1];
495 int current_line = 0;
497 if (text_buffer == NULL || *text_buffer == '\0')
500 if (current_line >= max_lines)
503 if (cut_length == -1)
504 cut_length = line_length;
509 while (*text_buffer && current_line < max_lines)
511 char line[MAX_LINE_LEN + 1];
513 boolean last_line_was_empty = TRUE;
514 int num_line_chars = MAX_LINE_LEN;
517 /* copy next line from text buffer to line buffer (nearly fgets() style) */
518 for (i = 0; i < num_line_chars && *text_buffer; i++)
519 if ((line[i] = *text_buffer++) == '\n')
523 /* prevent 'num_line_chars' sized lines to cause additional empty line */
524 if (i == num_line_chars && *text_buffer == '\n')
527 /* skip comments (lines directly beginning with '#') */
528 if (line[0] == '#' && parse_comments)
532 /* try to read generic token/value pair definition after comment sign */
533 if (getCheckedTokenValueFromString(line + 1, &token, &value))
535 /* if found, flush the current buffer, if non-empty */
536 if (buffer_len > 0 && current_line < max_lines)
538 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
539 line_spacing, mask_mode, centered, current_line);
547 if (strEqual(token, ".font"))
548 font_nr = gfx.get_font_from_token_function(value);
549 else if (strEqual(token, ".autowrap"))
550 autowrap = get_boolean_from_string(value);
551 else if (strEqual(token, ".centered"))
552 centered = get_boolean_from_string(value);
553 else if (strEqual(token, ".parse_comments"))
554 parse_comments = get_boolean_from_string(value);
560 /* cut trailing newline and carriage return from input line */
561 for (line_ptr = line; *line_ptr; line_ptr++)
563 if (*line_ptr == '\n' || *line_ptr == '\r')
570 if (strlen(line) == 0) /* special case: force empty line */
575 while (*line_ptr && current_line < max_lines)
577 boolean buffer_filled;
581 buffer_filled = RenderLineToBuffer(&line_ptr, buffer, &buffer_len,
582 line_length, last_line_was_empty);
586 if (strlen(line_ptr) <= line_length)
588 buffer_len = strlen(line_ptr);
589 strcpy(buffer, line_ptr);
593 buffer_len = line_length;
594 strncpy(buffer, line_ptr, line_length);
597 buffer[buffer_len] = '\0';
598 line_ptr += buffer_len;
600 buffer_filled = TRUE;
605 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
606 line_spacing, mask_mode, centered, current_line);
609 last_line_was_empty = (buffer_len == 0);
617 if (buffer_len > 0 && current_line < max_lines)
619 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
620 line_spacing, mask_mode, centered, current_line);
627 int DrawTextBufferVA(int x, int y, char *format, va_list ap, int font_nr,
628 int line_length, int cut_length, int max_lines,
629 int line_spacing, int mask_mode, boolean autowrap,
630 boolean centered, boolean parse_comments)
632 char text_buffer[MAX_OUTPUT_LINESIZE];
633 int text_length = vsnprintf(text_buffer, MAX_OUTPUT_LINESIZE, format, ap);
635 if (text_length >= MAX_OUTPUT_LINESIZE)
636 Error(ERR_WARN, "string too long in DrawTextBufferVA() -- truncated");
638 int num_lines_printed = DrawTextBuffer(x, y, text_buffer, font_nr,
639 line_length, cut_length, max_lines,
640 line_spacing, mask_mode, autowrap,
641 centered, parse_comments);
642 return num_lines_printed;
645 int DrawTextFile(int x, int y, char *filename, int font_nr,
646 int line_length, int cut_length, int max_lines,
647 int line_spacing, int mask_mode, boolean autowrap,
648 boolean centered, boolean parse_comments)
650 char *text_buffer = GetTextBufferFromFile(filename, MAX_LINES_FROM_FILE);
651 int num_lines_printed = DrawTextBuffer(x, y, text_buffer, font_nr,
652 line_length, cut_length, max_lines,
653 line_spacing, mask_mode, autowrap,
654 centered, parse_comments);
655 checked_free(text_buffer);
657 return num_lines_printed;