1 // ============================================================================
2 // Artsoft Retro-Game Library
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
7 // https://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 getFontDrawOffsetX(int font_nr)
64 int font_bitmap_id = gfx.select_font_function(font_nr);
66 return gfx.font_bitmap_info[font_bitmap_id].draw_xoffset;
69 int getFontDrawOffsetY(int font_nr)
71 int font_bitmap_id = gfx.select_font_function(font_nr);
73 return gfx.font_bitmap_info[font_bitmap_id].draw_yoffset;
76 int getTextWidth(char *text, int font_nr)
78 return (text != NULL ? strlen(text) * getFontWidth(font_nr) : 0);
81 static int getFontCharPosition(int font_nr, char c)
83 int font_bitmap_id = gfx.select_font_function(font_nr);
84 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
85 boolean default_font = (font->num_chars == DEFAULT_NUM_CHARS_PER_FONT);
86 int font_pos = (unsigned char)c - 32;
88 // map some special characters to their ascii values in default font
90 font_pos = MAP_FONT_ASCII(c) - 32;
92 // this allows dynamic special characters together with special font
93 if (font_pos < 0 || font_pos >= font->num_chars)
99 void getFontCharSource(int font_nr, char c, Bitmap **bitmap, int *x, int *y)
101 int font_bitmap_id = gfx.select_font_function(font_nr);
102 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
103 int font_pos = getFontCharPosition(font_nr, c);
104 int offset_x = (font->offset_x != 0 ? font->offset_x : font->width);
105 int offset_y = (font->offset_y != 0 ? font->offset_y : font->height);
107 *bitmap = font->bitmap;
108 *x = font->src_x + (font_pos % font->num_chars_per_line) * offset_x;
109 *y = font->src_y + (font_pos / font->num_chars_per_line) * offset_y;
113 // ============================================================================
114 // text string helper functions
115 // ============================================================================
117 int maxWordLengthInRequestString(char *text)
120 int word_len = 0, max_word_len = 0;
122 for (text_ptr = text; *text_ptr; text_ptr++)
124 word_len = (*text_ptr != ' ' &&
126 *text_ptr != '!' ? word_len + 1 : 0);
128 max_word_len = MAX(word_len, max_word_len);
135 // ============================================================================
136 // simple text drawing functions
137 // ============================================================================
139 void DrawInitText(char *text, int ypos, int font_nr)
141 if (window != NULL &&
142 gfx.draw_init_text &&
144 gfx.font_bitmap_info[font_nr].bitmap != NULL)
146 int x = (video.width - getTextWidth(text, font_nr)) / 2;
148 int width = video.width;
149 int height = getFontHeight(font_nr);
151 ClearRectangle(drawto, 0, y, width, height);
152 DrawTextExt(drawto, x, y, text, font_nr, BLIT_OPAQUE);
154 BlitBitmap(drawto, window, 0, 0, video.width, video.height, 0, 0);
158 void DrawInitTextHead(char *text)
160 // always draw headlines when loading initial stuff
161 LimitScreenUpdates(FALSE);
165 DrawInitText(text, 120, FC_GREEN);
168 void DrawInitTextItem(char *text)
170 // limit drawing (potentially many) loading items
171 LimitScreenUpdates(TRUE);
175 DrawInitText(text, 150, FC_YELLOW);
178 void DrawTextF(int x, 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 Fail("string too long in DrawTextF() -- aborting");
190 DrawText(gfx.sx + x, gfx.sy + y, buffer, font_nr);
193 void DrawTextFCentered(int y, int font_nr, char *format, ...)
195 char buffer[MAX_OUTPUT_LINESIZE + 1];
198 va_start(ap, format);
199 vsprintf(buffer, format, ap);
202 if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
203 Fail("string too long in DrawTextFCentered() -- aborting");
205 DrawText(gfx.sx + (gfx.sxsize - getTextWidth(buffer, font_nr)) / 2,
206 gfx.sy + y, buffer, font_nr);
209 void DrawTextS(int x, int y, int font_nr, char *text)
211 DrawText(gfx.sx + x, gfx.sy + y, text, font_nr);
214 void DrawTextSCentered(int y, int font_nr, char *text)
216 DrawText(gfx.sx + (gfx.sxsize - getTextWidth(text, font_nr)) / 2,
217 gfx.sy + y, text, font_nr);
220 void DrawTextSAligned(int x, int y, char *text, int font_nr, int align)
222 DrawText(gfx.sx + ALIGNED_XPOS(x, getTextWidth(text, font_nr), align),
223 gfx.sy + y, text, font_nr);
226 void DrawTextAligned(int x, int y, char *text, int font_nr, int align)
228 DrawText(ALIGNED_XPOS(x, getTextWidth(text, font_nr), align),
232 void DrawText(int x, int y, char *text, int font_nr)
234 int mask_mode = BLIT_OPAQUE;
236 if (DrawingOnBackground(x, y))
237 mask_mode = BLIT_ON_BACKGROUND;
239 DrawTextExt(drawto, x, y, text, font_nr, mask_mode);
241 if (IN_GFX_FIELD_FULL(x, y))
242 redraw_mask |= REDRAW_FIELD;
243 else if (IN_GFX_DOOR_1(x, y))
244 redraw_mask |= REDRAW_DOOR_1;
245 else if (IN_GFX_DOOR_2(x, y))
246 redraw_mask |= REDRAW_DOOR_2;
247 else if (IN_GFX_DOOR_3(x, y))
248 redraw_mask |= REDRAW_DOOR_3;
250 redraw_mask |= REDRAW_ALL;
253 void DrawTextExt(DrawBuffer *dst_bitmap, int dst_x, int dst_y, char *text,
254 int font_nr, int mask_mode)
256 struct FontBitmapInfo *font = getFontBitmapInfo(font_nr);
257 int font_width = getFontWidth(font_nr);
258 int font_height = getFontHeight(font_nr);
261 char *text_ptr = text;
263 if (font->bitmap == NULL)
266 // skip text to be printed outside the window (left/right will be clipped)
267 if (dst_y < 0 || dst_y + font_height > video.height)
270 // add offset for drawing font characters
271 dst_x += font->draw_xoffset;
272 dst_y += font->draw_yoffset;
276 char c = *text_ptr++;
279 c = ' '; // print space instead of newline
281 getFontCharSource(font_nr, c, &src_bitmap, &src_x, &src_y);
283 // clip text at the left side of the window
291 // clip text at the right side of the window
292 if (dst_x + font_width > video.width)
295 if (mask_mode == BLIT_INVERSE) // special mode for text gadgets
297 // first step: draw solid colored rectangle (use "cursor" character)
298 if (strlen(text) == 1) // only one char inverted => draw cursor
300 Bitmap *cursor_bitmap;
301 int cursor_x, cursor_y;
303 getFontCharSource(font_nr, FONT_ASCII_CURSOR, &cursor_bitmap,
304 &cursor_x, &cursor_y);
306 BlitBitmap(cursor_bitmap, dst_bitmap, cursor_x, cursor_y,
307 font_width, font_height, dst_x, dst_y);
310 // second step: draw masked inverted character
311 SDLCopyInverseMasked(src_bitmap, dst_bitmap, src_x, src_y,
312 font_width, font_height, dst_x, dst_y);
314 else if (mask_mode == BLIT_MASKED || mask_mode == BLIT_ON_BACKGROUND)
316 if (mask_mode == BLIT_ON_BACKGROUND)
318 // clear font character background
319 ClearRectangleOnBackground(dst_bitmap, dst_x, dst_y,
320 font_width, font_height);
323 BlitBitmapMasked(src_bitmap, dst_bitmap, src_x, src_y,
324 font_width, font_height, dst_x, dst_y);
326 else // normal, non-masked font blitting
328 BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y,
329 font_width, font_height, dst_x, dst_y);
337 // ============================================================================
338 // text buffer drawing functions
339 // ============================================================================
341 #define MAX_LINES_FROM_FILE 1024
343 char *GetTextBufferFromFile(char *filename, int max_lines)
349 if (filename == NULL)
352 if (!(file = openFile(filename, MODE_READ)))
355 buffer = checked_calloc(1); // start with valid, but empty text buffer
357 while (!checkEndOfFile(file) && num_lines < max_lines)
359 char line[MAX_LINE_LEN];
361 // read next line of input file
362 if (!getStringFromFile(file, line, MAX_LINE_LEN))
365 buffer = checked_realloc(buffer, strlen(buffer) + strlen(line) + 1);
367 strcat(buffer, line);
377 static boolean RenderLineToBuffer(char **src_buffer_ptr, char *dst_buffer,
378 int *dst_buffer_len, int line_length,
379 boolean last_line_was_empty)
381 char *text_ptr = *src_buffer_ptr;
382 char *buffer = dst_buffer;
383 int buffer_len = *dst_buffer_len;
384 boolean buffer_filled = FALSE;
391 // skip leading whitespaces
392 while (*text_ptr == ' ' || *text_ptr == '\t')
398 // look for end of next word
399 while (*word_ptr != ' ' && *word_ptr != '\t' && *word_ptr != '\0')
409 else if (*text_ptr == '\n') // special case: force empty line
414 // prevent printing of multiple empty lines
415 if (buffer_len > 0 || !last_line_was_empty)
416 buffer_filled = TRUE;
418 else if (word_len < line_length - buffer_len)
420 // word fits into text buffer -- add word
423 buffer[buffer_len++] = ' ';
425 strncpy(&buffer[buffer_len], text_ptr, word_len);
426 buffer_len += word_len;
427 buffer[buffer_len] = '\0';
428 text_ptr += word_len;
430 else if (buffer_len > 0)
432 // not enough space left for word in text buffer -- print buffer
434 buffer_filled = TRUE;
438 // word does not fit at all into empty text buffer -- cut word
440 strncpy(buffer, text_ptr, line_length);
441 buffer[line_length] = '\0';
442 text_ptr += line_length;
443 buffer_filled = TRUE;
450 *src_buffer_ptr = text_ptr;
451 *dst_buffer_len = buffer_len;
453 return buffer_filled;
456 static boolean getCheckedTokenValueFromString(char *string, char **token,
461 if (!getTokenValueFromString(string, token, value))
464 if (**token != '.') // token should begin with dot
467 for (ptr = *token; *ptr; ptr++) // token should contain no whitespace
468 if (*ptr == ' ' || *ptr == '\t')
471 for (ptr = *value; *ptr; ptr++) // value should contain no whitespace
472 if (*ptr == ' ' || *ptr == '\t')
478 static void DrawTextBuffer_Flush(int x, int y, char *buffer, int font_nr,
479 int line_length, int cut_length,
480 int mask_mode, boolean centered,
483 int buffer_len = strlen(buffer);
484 int font_width = getFontWidth(font_nr);
485 int offset_chars = (centered ? (line_length - buffer_len) / 2 : 0);
487 (centered ? font_width * (line_length - buffer_len) / 2 : 0);
488 int final_cut_length = MAX(0, cut_length - offset_chars);
489 int xx = x + offset_xsize;
490 int yy = y + current_ypos;
492 buffer[final_cut_length] = '\0';
495 DrawTextExt(drawto, xx, yy, buffer, font_nr, mask_mode);
497 DrawText(xx, yy, buffer, font_nr);
500 int DrawTextBuffer(int x, int y, char *text_buffer, int font_nr,
501 int line_length, int cut_length, int max_lines,
502 int line_spacing, int mask_mode, boolean autowrap,
503 boolean centered, boolean parse_comments)
505 char buffer[line_length + 1];
507 int font_height = getFontHeight(font_nr);
508 int line_height = font_height + line_spacing;
509 int current_line = 0;
510 int current_ypos = 0;
511 int max_ysize = max_lines * line_height;
513 if (text_buffer == NULL || *text_buffer == '\0')
516 if (current_line >= max_lines)
519 if (cut_length == -1)
520 cut_length = line_length;
525 while (*text_buffer && current_ypos < max_ysize)
527 char line[MAX_LINE_LEN + 1];
529 boolean last_line_was_empty = TRUE;
530 int num_line_chars = MAX_LINE_LEN;
533 // copy next line from text buffer to line buffer (nearly fgets() style)
534 for (i = 0; i < num_line_chars && *text_buffer; i++)
535 if ((line[i] = *text_buffer++) == '\n')
539 // prevent 'num_line_chars' sized lines to cause additional empty line
540 if (i == num_line_chars && *text_buffer == '\n')
543 // skip comments (lines directly beginning with '#')
544 if (line[0] == '#' && parse_comments)
548 // try to read generic token/value pair definition after comment sign
549 if (getCheckedTokenValueFromString(line + 1, &token, &value))
551 // if found, flush the current buffer, if non-empty
552 if (buffer_len > 0 && current_ypos < max_ysize)
554 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
555 mask_mode, centered, current_ypos);
556 current_ypos += line_height;
563 if (strEqual(token, ".font"))
564 font_nr = gfx.get_font_from_token_function(value);
565 else if (strEqual(token, ".autowrap"))
566 autowrap = get_boolean_from_string(value);
567 else if (strEqual(token, ".centered"))
568 centered = get_boolean_from_string(value);
569 else if (strEqual(token, ".parse_comments"))
570 parse_comments = get_boolean_from_string(value);
572 // if font has changed, depending values need to be updated as well
573 font_height = getFontHeight(font_nr);
574 line_height = font_height + line_spacing;
580 // cut trailing newline and carriage return from input line
581 for (line_ptr = line; *line_ptr; line_ptr++)
583 if (*line_ptr == '\n' || *line_ptr == '\r')
590 if (strlen(line) == 0) // special case: force empty line
595 while (*line_ptr && current_ypos < max_ysize)
597 boolean buffer_filled;
601 buffer_filled = RenderLineToBuffer(&line_ptr, buffer, &buffer_len,
602 line_length, last_line_was_empty);
606 if (strlen(line_ptr) <= line_length)
608 buffer_len = strlen(line_ptr);
609 strcpy(buffer, line_ptr);
613 buffer_len = line_length;
614 strncpy(buffer, line_ptr, line_length);
617 buffer[buffer_len] = '\0';
618 line_ptr += buffer_len;
620 buffer_filled = TRUE;
625 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
626 mask_mode, centered, current_ypos);
627 current_ypos += line_height;
630 last_line_was_empty = (buffer_len == 0);
638 if (buffer_len > 0 && current_ypos < max_ysize)
640 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
641 mask_mode, centered, current_ypos);
642 current_ypos += line_height;
649 int DrawTextBufferVA(int x, int y, char *format, va_list ap, int font_nr,
650 int line_length, int cut_length, int max_lines,
651 int line_spacing, int mask_mode, boolean autowrap,
652 boolean centered, boolean parse_comments)
654 char text_buffer[MAX_OUTPUT_LINESIZE];
655 int text_length = vsnprintf(text_buffer, MAX_OUTPUT_LINESIZE, format, ap);
657 if (text_length >= MAX_OUTPUT_LINESIZE)
658 Warn("string too long in DrawTextBufferVA() -- truncated");
660 int num_lines_printed = DrawTextBuffer(x, y, text_buffer, font_nr,
661 line_length, cut_length, max_lines,
662 line_spacing, mask_mode, autowrap,
663 centered, parse_comments);
664 return num_lines_printed;
667 int DrawTextFile(int x, int y, char *filename, int font_nr,
668 int line_length, int cut_length, int max_lines,
669 int line_spacing, int mask_mode, boolean autowrap,
670 boolean centered, boolean parse_comments)
672 char *text_buffer = GetTextBufferFromFile(filename, MAX_LINES_FROM_FILE);
673 int num_lines_printed = DrawTextBuffer(x, y, text_buffer, font_nr,
674 line_length, cut_length, max_lines,
675 line_spacing, mask_mode, autowrap,
676 centered, parse_comments);
677 checked_free(text_buffer);
679 return num_lines_printed;