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 DrawInitText(char *text, int ypos, int font_nr)
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 DrawTextF(int x, int y, int font_nr, char *format, ...)
146 char buffer[MAX_OUTPUT_LINESIZE + 1];
149 va_start(ap, format);
150 vsprintf(buffer, format, ap);
153 if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
154 Error(ERR_EXIT, "string too long in DrawTextF() -- aborting");
156 DrawText(gfx.sx + x, gfx.sy + y, buffer, font_nr);
159 void DrawTextFCentered(int y, int font_nr, char *format, ...)
161 char buffer[MAX_OUTPUT_LINESIZE + 1];
164 va_start(ap, format);
165 vsprintf(buffer, format, ap);
168 if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
169 Error(ERR_EXIT, "string too long in DrawTextFCentered() -- aborting");
171 DrawText(gfx.sx + (gfx.sxsize - getTextWidth(buffer, font_nr)) / 2,
172 gfx.sy + y, buffer, font_nr);
175 void DrawTextS(int x, int y, int font_nr, char *text)
177 DrawText(gfx.sx + x, gfx.sy + y, text, font_nr);
180 void DrawTextSCentered(int y, int font_nr, char *text)
182 DrawText(gfx.sx + (gfx.sxsize - getTextWidth(text, font_nr)) / 2,
183 gfx.sy + y, text, font_nr);
186 void DrawTextSAligned(int x, int y, char *text, int font_nr, int align)
188 DrawText(gfx.sx + ALIGNED_XPOS(x, getTextWidth(text, font_nr), align),
189 gfx.sx + y, text, font_nr);
192 void DrawTextAligned(int x, int y, char *text, int font_nr, int align)
194 DrawText(ALIGNED_XPOS(x, getTextWidth(text, font_nr), align),
198 void DrawText(int x, int y, char *text, int font_nr)
200 int mask_mode = BLIT_OPAQUE;
202 if (DrawingOnBackground(x, y))
203 mask_mode = BLIT_ON_BACKGROUND;
205 DrawTextExt(drawto, x, y, text, font_nr, mask_mode);
207 if (IN_GFX_FIELD_FULL(x, y))
208 redraw_mask |= REDRAW_FIELD;
209 else if (IN_GFX_DOOR_1(x, y))
210 redraw_mask |= REDRAW_DOOR_1;
211 else if (IN_GFX_DOOR_2(x, y))
212 redraw_mask |= REDRAW_DOOR_2;
213 else if (IN_GFX_DOOR_3(x, y))
214 redraw_mask |= REDRAW_DOOR_3;
216 redraw_mask |= REDRAW_ALL;
219 void DrawTextExt(DrawBuffer *dst_bitmap, int dst_x, int dst_y, char *text,
220 int font_nr, int mask_mode)
222 struct FontBitmapInfo *font = getFontBitmapInfo(font_nr);
223 int font_width = getFontWidth(font_nr);
224 int font_height = getFontHeight(font_nr);
227 char *text_ptr = text;
229 if (font->bitmap == NULL)
232 /* skip text to be printed outside the window (left/right will be clipped) */
233 if (dst_y < 0 || dst_y + font_height > video.height)
236 /* add offset for drawing font characters */
237 dst_x += font->draw_xoffset;
238 dst_y += font->draw_yoffset;
242 char c = *text_ptr++;
245 c = ' '; /* print space instead of newline */
247 getFontCharSource(font_nr, c, &src_bitmap, &src_x, &src_y);
249 /* clip text at the left side of the window */
257 /* clip text at the right side of the window */
258 if (dst_x + font_width > video.width)
261 if (mask_mode == BLIT_INVERSE) /* special mode for text gadgets */
263 /* first step: draw solid colored rectangle (use "cursor" character) */
264 if (strlen(text) == 1) /* only one char inverted => draw cursor */
266 Bitmap *cursor_bitmap;
267 int cursor_x, cursor_y;
269 getFontCharSource(font_nr, FONT_ASCII_CURSOR, &cursor_bitmap,
270 &cursor_x, &cursor_y);
272 BlitBitmap(cursor_bitmap, dst_bitmap, cursor_x, cursor_y,
273 font_width, font_height, dst_x, dst_y);
276 /* second step: draw masked inverted character */
277 SDLCopyInverseMasked(src_bitmap, dst_bitmap, src_x, src_y,
278 font_width, font_height, dst_x, dst_y);
280 else if (mask_mode == BLIT_MASKED || mask_mode == BLIT_ON_BACKGROUND)
282 if (mask_mode == BLIT_ON_BACKGROUND)
284 /* clear font character background */
285 ClearRectangleOnBackground(dst_bitmap, dst_x, dst_y,
286 font_width, font_height);
289 BlitBitmapMasked(src_bitmap, dst_bitmap, src_x, src_y,
290 font_width, font_height, dst_x, dst_y);
292 else /* normal, non-masked font blitting */
294 BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y,
295 font_width, font_height, dst_x, dst_y);
303 /* ========================================================================= */
304 /* text buffer drawing functions */
305 /* ========================================================================= */
307 #define MAX_LINES_FROM_FILE 1024
309 char *GetTextBufferFromFile(char *filename, int max_lines)
315 if (filename == NULL)
318 if (!(file = openFile(filename, MODE_READ)))
321 buffer = checked_calloc(1); /* start with valid, but empty text buffer */
323 while (!checkEndOfFile(file) && num_lines < max_lines)
325 char line[MAX_LINE_LEN];
327 /* read next line of input file */
328 if (!getStringFromFile(file, line, MAX_LINE_LEN))
331 buffer = checked_realloc(buffer, strlen(buffer) + strlen(line) + 1);
333 strcat(buffer, line);
343 static boolean RenderLineToBuffer(char **src_buffer_ptr, char *dst_buffer,
344 int *dst_buffer_len, int line_length,
345 boolean last_line_was_empty)
347 char *text_ptr = *src_buffer_ptr;
348 char *buffer = dst_buffer;
349 int buffer_len = *dst_buffer_len;
350 boolean buffer_filled = FALSE;
357 /* skip leading whitespaces */
358 while (*text_ptr == ' ' || *text_ptr == '\t')
364 /* look for end of next word */
365 while (*word_ptr != ' ' && *word_ptr != '\t' && *word_ptr != '\0')
375 else if (*text_ptr == '\n') /* special case: force empty line */
380 /* prevent printing of multiple empty lines */
381 if (buffer_len > 0 || !last_line_was_empty)
382 buffer_filled = TRUE;
384 else if (word_len < line_length - buffer_len)
386 /* word fits into text buffer -- add word */
389 buffer[buffer_len++] = ' ';
391 strncpy(&buffer[buffer_len], text_ptr, word_len);
392 buffer_len += word_len;
393 buffer[buffer_len] = '\0';
394 text_ptr += word_len;
396 else if (buffer_len > 0)
398 /* not enough space left for word in text buffer -- print buffer */
400 buffer_filled = TRUE;
404 /* word does not fit at all into empty text buffer -- cut word */
406 strncpy(buffer, text_ptr, line_length);
407 buffer[line_length] = '\0';
408 text_ptr += line_length;
409 buffer_filled = TRUE;
416 *src_buffer_ptr = text_ptr;
417 *dst_buffer_len = buffer_len;
419 return buffer_filled;
422 static boolean getCheckedTokenValueFromString(char *string, char **token,
427 if (!getTokenValueFromString(string, token, value))
430 if (**token != '.') /* token should begin with dot */
433 for (ptr = *token; *ptr; ptr++) /* token should contain no whitespace */
434 if (*ptr == ' ' || *ptr == '\t')
437 for (ptr = *value; *ptr; ptr++) /* value should contain no whitespace */
438 if (*ptr == ' ' || *ptr == '\t')
444 static void DrawTextBuffer_Flush(int x, int y, char *buffer, int font_nr,
445 int line_length, int cut_length,
446 int line_spacing, int mask_mode,
447 boolean centered, int current_line)
449 int buffer_len = strlen(buffer);
450 int font_width = getFontWidth(font_nr);
451 int font_height = getFontHeight(font_nr);
452 int offset_chars = (centered ? (line_length - buffer_len) / 2 : 0);
454 (centered ? font_width * (line_length - buffer_len) / 2 : 0);
455 int final_cut_length = MAX(0, cut_length - offset_chars);
456 int xx = x + offset_xsize;
457 int yy = y + current_line * (font_height + line_spacing);
459 buffer[final_cut_length] = '\0';
462 DrawTextExt(drawto, xx, yy, buffer, font_nr, mask_mode);
464 DrawText(xx, yy, buffer, font_nr);
467 int DrawTextBuffer(int x, int y, char *text_buffer, int font_nr,
468 int line_length, int cut_length, int max_lines,
469 int line_spacing, int mask_mode, boolean autowrap,
470 boolean centered, boolean parse_comments)
472 char buffer[line_length + 1];
474 int current_line = 0;
476 if (text_buffer == NULL || *text_buffer == '\0')
479 if (current_line >= max_lines)
482 if (cut_length == -1)
483 cut_length = line_length;
488 while (*text_buffer && current_line < max_lines)
490 char line[MAX_LINE_LEN + 1];
492 boolean last_line_was_empty = TRUE;
493 int num_line_chars = MAX_LINE_LEN;
496 /* copy next line from text buffer to line buffer (nearly fgets() style) */
497 for (i = 0; i < num_line_chars && *text_buffer; i++)
498 if ((line[i] = *text_buffer++) == '\n')
502 /* prevent 'num_line_chars' sized lines to cause additional empty line */
503 if (i == num_line_chars && *text_buffer == '\n')
506 /* skip comments (lines directly beginning with '#') */
507 if (line[0] == '#' && parse_comments)
511 /* try to read generic token/value pair definition after comment sign */
512 if (getCheckedTokenValueFromString(line + 1, &token, &value))
514 /* if found, flush the current buffer, if non-empty */
515 if (buffer_len > 0 && current_line < max_lines)
517 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
518 line_spacing, mask_mode, centered, current_line);
526 if (strEqual(token, ".font"))
527 font_nr = gfx.get_font_from_token_function(value);
528 else if (strEqual(token, ".autowrap"))
529 autowrap = get_boolean_from_string(value);
530 else if (strEqual(token, ".centered"))
531 centered = get_boolean_from_string(value);
532 else if (strEqual(token, ".parse_comments"))
533 parse_comments = get_boolean_from_string(value);
539 /* cut trailing newline and carriage return from input line */
540 for (line_ptr = line; *line_ptr; line_ptr++)
542 if (*line_ptr == '\n' || *line_ptr == '\r')
549 if (strlen(line) == 0) /* special case: force empty line */
554 while (*line_ptr && current_line < max_lines)
556 boolean buffer_filled;
560 buffer_filled = RenderLineToBuffer(&line_ptr, buffer, &buffer_len,
561 line_length, last_line_was_empty);
565 if (strlen(line_ptr) <= line_length)
567 buffer_len = strlen(line_ptr);
568 strcpy(buffer, line_ptr);
572 buffer_len = line_length;
573 strncpy(buffer, line_ptr, line_length);
576 buffer[buffer_len] = '\0';
577 line_ptr += buffer_len;
579 buffer_filled = TRUE;
584 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
585 line_spacing, mask_mode, centered, current_line);
588 last_line_was_empty = (buffer_len == 0);
596 if (buffer_len > 0 && current_line < max_lines)
598 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
599 line_spacing, mask_mode, centered, current_line);
606 int DrawTextBufferVA(int x, int y, char *format, va_list ap, int font_nr,
607 int line_length, int cut_length, int max_lines,
608 int line_spacing, int mask_mode, boolean autowrap,
609 boolean centered, boolean parse_comments)
611 char text_buffer[MAX_OUTPUT_LINESIZE];
612 int text_length = vsnprintf(text_buffer, MAX_OUTPUT_LINESIZE, format, ap);
614 if (text_length >= MAX_OUTPUT_LINESIZE)
615 Error(ERR_WARN, "string too long in DrawTextBufferVA() -- truncated");
617 int num_lines_printed = DrawTextBuffer(x, y, text_buffer, font_nr,
618 line_length, cut_length, max_lines,
619 line_spacing, mask_mode, autowrap,
620 centered, parse_comments);
621 return num_lines_printed;
624 int DrawTextFile(int x, int y, char *filename, int font_nr,
625 int line_length, int cut_length, int max_lines,
626 int line_spacing, int mask_mode, boolean autowrap,
627 boolean centered, boolean parse_comments)
629 char *text_buffer = GetTextBufferFromFile(filename, MAX_LINES_FROM_FILE);
630 int num_lines_printed = DrawTextBuffer(x, y, text_buffer, font_nr,
631 line_length, cut_length, max_lines,
632 line_spacing, mask_mode, autowrap,
633 centered, parse_comments);
634 checked_free(text_buffer);
636 return num_lines_printed;