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 DrawTextCentered(int y, int font_nr, char *text)
188 DrawText((gfx.sxsize - getTextWidth(text, font_nr)) / 2, y, text, font_nr);
191 void DrawTextSAligned(int x, int y, char *text, int font_nr, int align)
193 DrawText(gfx.sx + ALIGNED_XPOS(x, getTextWidth(text, font_nr), align),
194 gfx.sx + y, text, font_nr);
197 void DrawTextAligned(int x, int y, char *text, int font_nr, int align)
199 DrawText(ALIGNED_XPOS(x, getTextWidth(text, font_nr), align),
203 void DrawText(int x, int y, char *text, int font_nr)
205 int mask_mode = BLIT_OPAQUE;
207 if (DrawingOnBackground(x, y))
208 mask_mode = BLIT_ON_BACKGROUND;
210 DrawTextExt(drawto, x, y, text, font_nr, mask_mode);
212 if (IN_GFX_FIELD_FULL(x, y))
213 redraw_mask |= REDRAW_FIELD;
214 else if (IN_GFX_DOOR_1(x, y))
215 redraw_mask |= REDRAW_DOOR_1;
216 else if (IN_GFX_DOOR_2(x, y))
217 redraw_mask |= REDRAW_DOOR_2;
218 else if (IN_GFX_DOOR_3(x, y))
219 redraw_mask |= REDRAW_DOOR_3;
221 redraw_mask |= REDRAW_ALL;
224 void DrawTextExt(DrawBuffer *dst_bitmap, int dst_x, int dst_y, char *text,
225 int font_nr, int mask_mode)
227 struct FontBitmapInfo *font = getFontBitmapInfo(font_nr);
228 int font_width = getFontWidth(font_nr);
229 int font_height = getFontHeight(font_nr);
232 char *text_ptr = text;
234 if (font->bitmap == NULL)
237 /* skip text to be printed outside the window (left/right will be clipped) */
238 if (dst_y < 0 || dst_y + font_height > video.height)
241 /* add offset for drawing font characters */
242 dst_x += font->draw_xoffset;
243 dst_y += font->draw_yoffset;
247 char c = *text_ptr++;
250 c = ' '; /* print space instead of newline */
252 getFontCharSource(font_nr, c, &src_bitmap, &src_x, &src_y);
254 /* clip text at the left side of the window */
262 /* clip text at the right side of the window */
263 if (dst_x + font_width > video.width)
266 if (mask_mode == BLIT_INVERSE) /* special mode for text gadgets */
268 /* first step: draw solid colored rectangle (use "cursor" character) */
269 if (strlen(text) == 1) /* only one char inverted => draw cursor */
271 Bitmap *cursor_bitmap;
272 int cursor_x, cursor_y;
274 getFontCharSource(font_nr, FONT_ASCII_CURSOR, &cursor_bitmap,
275 &cursor_x, &cursor_y);
277 BlitBitmap(cursor_bitmap, dst_bitmap, cursor_x, cursor_y,
278 font_width, font_height, dst_x, dst_y);
281 /* second step: draw masked inverted character */
282 SDLCopyInverseMasked(src_bitmap, dst_bitmap, src_x, src_y,
283 font_width, font_height, dst_x, dst_y);
285 else if (mask_mode == BLIT_MASKED || mask_mode == BLIT_ON_BACKGROUND)
287 if (mask_mode == BLIT_ON_BACKGROUND)
289 /* clear font character background */
290 ClearRectangleOnBackground(dst_bitmap, dst_x, dst_y,
291 font_width, font_height);
294 BlitBitmapMasked(src_bitmap, dst_bitmap, src_x, src_y,
295 font_width, font_height, dst_x, dst_y);
297 else /* normal, non-masked font blitting */
299 BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y,
300 font_width, font_height, dst_x, dst_y);
308 /* ========================================================================= */
309 /* text buffer drawing functions */
310 /* ========================================================================= */
312 #define MAX_LINES_FROM_FILE 1024
314 char *GetTextBufferFromFile(char *filename, int max_lines)
320 if (filename == NULL)
323 if (!(file = openFile(filename, MODE_READ)))
326 buffer = checked_calloc(1); /* start with valid, but empty text buffer */
328 while (!checkEndOfFile(file) && num_lines < max_lines)
330 char line[MAX_LINE_LEN];
332 /* read next line of input file */
333 if (!getStringFromFile(file, line, MAX_LINE_LEN))
336 buffer = checked_realloc(buffer, strlen(buffer) + strlen(line) + 1);
338 strcat(buffer, line);
348 static boolean RenderLineToBuffer(char **src_buffer_ptr, char *dst_buffer,
349 int *dst_buffer_len, int line_length,
350 boolean last_line_was_empty)
352 char *text_ptr = *src_buffer_ptr;
353 char *buffer = dst_buffer;
354 int buffer_len = *dst_buffer_len;
355 boolean buffer_filled = FALSE;
362 /* skip leading whitespaces */
363 while (*text_ptr == ' ' || *text_ptr == '\t')
369 /* look for end of next word */
370 while (*word_ptr != ' ' && *word_ptr != '\t' && *word_ptr != '\0')
380 else if (*text_ptr == '\n') /* special case: force empty line */
385 /* prevent printing of multiple empty lines */
386 if (buffer_len > 0 || !last_line_was_empty)
387 buffer_filled = TRUE;
389 else if (word_len < line_length - buffer_len)
391 /* word fits into text buffer -- add word */
394 buffer[buffer_len++] = ' ';
396 strncpy(&buffer[buffer_len], text_ptr, word_len);
397 buffer_len += word_len;
398 buffer[buffer_len] = '\0';
399 text_ptr += word_len;
401 else if (buffer_len > 0)
403 /* not enough space left for word in text buffer -- print buffer */
405 buffer_filled = TRUE;
409 /* word does not fit at all into empty text buffer -- cut word */
411 strncpy(buffer, text_ptr, line_length);
412 buffer[line_length] = '\0';
413 text_ptr += line_length;
414 buffer_filled = TRUE;
421 *src_buffer_ptr = text_ptr;
422 *dst_buffer_len = buffer_len;
424 return buffer_filled;
427 static boolean getCheckedTokenValueFromString(char *string, char **token,
432 if (!getTokenValueFromString(string, token, value))
435 if (**token != '.') /* token should begin with dot */
438 for (ptr = *token; *ptr; ptr++) /* token should contain no whitespace */
439 if (*ptr == ' ' || *ptr == '\t')
442 for (ptr = *value; *ptr; ptr++) /* value should contain no whitespace */
443 if (*ptr == ' ' || *ptr == '\t')
449 static void DrawTextBuffer_Flush(int x, int y, char *buffer, int font_nr,
450 int line_length, int cut_length,
451 int line_spacing, int mask_mode,
452 boolean centered, int current_line)
454 int buffer_len = strlen(buffer);
455 int font_width = getFontWidth(font_nr);
456 int font_height = getFontHeight(font_nr);
457 int offset_chars = (centered ? (line_length - buffer_len) / 2 : 0);
459 (centered ? font_width * (line_length - buffer_len) / 2 : 0);
460 int final_cut_length = MAX(0, cut_length - offset_chars);
461 int xx = x + offset_xsize;
462 int yy = y + current_line * (font_height + line_spacing);
464 buffer[final_cut_length] = '\0';
467 DrawTextExt(drawto, xx, yy, buffer, font_nr, mask_mode);
469 DrawText(xx, yy, buffer, font_nr);
472 int DrawTextBuffer(int x, int y, char *text_buffer, int font_nr,
473 int line_length, int cut_length, int max_lines,
474 int line_spacing, int mask_mode, boolean autowrap,
475 boolean centered, boolean parse_comments)
477 char buffer[line_length + 1];
479 int current_line = 0;
481 if (text_buffer == NULL || *text_buffer == '\0')
484 if (current_line >= max_lines)
487 if (cut_length == -1)
488 cut_length = line_length;
493 while (*text_buffer && current_line < max_lines)
495 char line[MAX_LINE_LEN + 1];
497 boolean last_line_was_empty = TRUE;
498 int num_line_chars = MAX_LINE_LEN;
501 /* copy next line from text buffer to line buffer (nearly fgets() style) */
502 for (i = 0; i < num_line_chars && *text_buffer; i++)
503 if ((line[i] = *text_buffer++) == '\n')
507 /* prevent 'num_line_chars' sized lines to cause additional empty line */
508 if (i == num_line_chars && *text_buffer == '\n')
511 /* skip comments (lines directly beginning with '#') */
512 if (line[0] == '#' && parse_comments)
516 /* try to read generic token/value pair definition after comment sign */
517 if (getCheckedTokenValueFromString(line + 1, &token, &value))
519 /* if found, flush the current buffer, if non-empty */
520 if (buffer_len > 0 && current_line < max_lines)
522 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
523 line_spacing, mask_mode, centered, current_line);
531 if (strEqual(token, ".font"))
532 font_nr = gfx.get_font_from_token_function(value);
533 else if (strEqual(token, ".autowrap"))
534 autowrap = get_boolean_from_string(value);
535 else if (strEqual(token, ".centered"))
536 centered = get_boolean_from_string(value);
537 else if (strEqual(token, ".parse_comments"))
538 parse_comments = get_boolean_from_string(value);
544 /* cut trailing newline and carriage return from input line */
545 for (line_ptr = line; *line_ptr; line_ptr++)
547 if (*line_ptr == '\n' || *line_ptr == '\r')
554 if (strlen(line) == 0) /* special case: force empty line */
559 while (*line_ptr && current_line < max_lines)
561 boolean buffer_filled;
565 buffer_filled = RenderLineToBuffer(&line_ptr, buffer, &buffer_len,
566 line_length, last_line_was_empty);
570 if (strlen(line_ptr) <= line_length)
572 buffer_len = strlen(line_ptr);
573 strcpy(buffer, line_ptr);
577 buffer_len = line_length;
578 strncpy(buffer, line_ptr, line_length);
581 buffer[buffer_len] = '\0';
582 line_ptr += buffer_len;
584 buffer_filled = TRUE;
589 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
590 line_spacing, mask_mode, centered, current_line);
593 last_line_was_empty = (buffer_len == 0);
601 if (buffer_len > 0 && current_line < max_lines)
603 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
604 line_spacing, mask_mode, centered, current_line);
611 int DrawTextBufferVA(int x, int y, char *format, va_list ap, int font_nr,
612 int line_length, int cut_length, int max_lines,
613 int line_spacing, int mask_mode, boolean autowrap,
614 boolean centered, boolean parse_comments)
616 char text_buffer[MAX_OUTPUT_LINESIZE];
617 int text_length = vsnprintf(text_buffer, MAX_OUTPUT_LINESIZE, format, ap);
619 if (text_length >= MAX_OUTPUT_LINESIZE)
620 Error(ERR_WARN, "string too long in DrawTextBufferVA() -- truncated");
622 int num_lines_printed = DrawTextBuffer(x, y, text_buffer, font_nr,
623 line_length, cut_length, max_lines,
624 line_spacing, mask_mode, autowrap,
625 centered, parse_comments);
626 return num_lines_printed;
629 int DrawTextFile(int x, int y, char *filename, int font_nr,
630 int line_length, int cut_length, int max_lines,
631 int line_spacing, int mask_mode, boolean autowrap,
632 boolean centered, boolean parse_comments)
634 char *text_buffer = GetTextBufferFromFile(filename, MAX_LINES_FROM_FILE);
635 int num_lines_printed = DrawTextBuffer(x, y, text_buffer, font_nr,
636 line_length, cut_length, max_lines,
637 line_spacing, mask_mode, autowrap,
638 centered, parse_comments);
639 checked_free(text_buffer);
641 return num_lines_printed;