rnd-20001203-3-src
[rocksndiamonds.git] / src / libgame / text.c
1 /***********************************************************
2 *  Rocks'n'Diamonds -- McDuffin Strikes Back!              *
3 *----------------------------------------------------------*
4 *  (c) 1995-98 Artsoft Entertainment                       *
5 *              Holger Schemel                              *
6 *              Oststrasse 11a                              *
7 *              33604 Bielefeld                             *
8 *              phone: ++49 +521 290471                     *
9 *              email: aeglos@valinor.owl.de                *
10 *----------------------------------------------------------*
11 *  text.c                                                  *
12 ***********************************************************/
13
14 #include <stdarg.h>
15
16 #include "libgame.h"
17
18
19 /* ========================================================================= */
20 /* exported variables                                                        */
21 /* ========================================================================= */
22
23 struct FontInfo         font;
24
25
26 /* ========================================================================= */
27 /* font functions                                                            */
28 /* ========================================================================= */
29
30 void InitFontInfo(Bitmap bitmap_big, Bitmap bitmap_medium,
31                   Bitmap bitmap_small)
32 {
33   font.bitmap_big = bitmap_big;
34   font.bitmap_medium = bitmap_medium;
35   font.bitmap_small = bitmap_small;
36 }
37
38 int getFontWidth(int font_size, int font_type)
39 {
40   return (font_size == FS_BIG ? FONT1_XSIZE :
41           font_size == FS_MEDIUM ? FONT6_XSIZE :
42           font_type == FC_SPECIAL1 ? FONT3_XSIZE :
43           font_type == FC_SPECIAL2 ? FONT4_XSIZE :
44           font_type == FC_SPECIAL3 ? FONT5_XSIZE :
45           FONT2_XSIZE);
46 }
47
48 int getFontHeight(int font_size, int font_type)
49 {
50   return (font_size == FS_BIG ? FONT1_YSIZE :
51           font_size == FS_MEDIUM ? FONT6_YSIZE :
52           font_type == FC_SPECIAL1 ? FONT3_YSIZE :
53           font_type == FC_SPECIAL2 ? FONT4_YSIZE :
54           font_type == FC_SPECIAL3 ? FONT5_YSIZE :
55           FONT2_YSIZE);
56 }
57
58 void DrawInitText(char *text, int ypos, int color)
59 {
60   if (window && font.bitmap_small)
61   {
62     ClearRectangle(window, 0, ypos, video.width, FONT2_YSIZE);
63     DrawTextExt(window, (video.width - strlen(text) * FONT2_XSIZE)/2,
64                 ypos, text, FS_SMALL, color);
65     FlushDisplay();
66   }
67 }
68
69 void DrawTextFCentered(int y, int font_type, char *format, ...)
70 {
71   char buffer[MAX_OUTPUT_LINESIZE + 1];
72   int font_width = getFontWidth(FS_SMALL, font_type);
73   va_list ap;
74
75   va_start(ap, format);
76   vsprintf(buffer, format, ap);
77   va_end(ap);
78
79   DrawText(gfx.sx + (gfx.sxsize - strlen(buffer) * font_width) / 2,
80            gfx.sy + y, buffer, FS_SMALL, font_type);
81 }
82
83 void DrawTextF(int x, int y, int font_type, char *format, ...)
84 {
85   char buffer[MAX_OUTPUT_LINESIZE + 1];
86   va_list ap;
87
88   va_start(ap, format);
89   vsprintf(buffer, format, ap);
90   va_end(ap);
91
92   DrawText(gfx.sx + x, gfx.sy + y, buffer, FS_SMALL, font_type);
93 }
94
95 void DrawText(int x, int y, char *text, int font_size, int font_type)
96 {
97   DrawTextExt(drawto, x, y, text, font_size, font_type);
98
99   if (x < gfx.dx)
100     redraw_mask |= REDRAW_FIELD;
101   else if (y < gfx.vy)
102     redraw_mask |= REDRAW_DOOR_1;
103 }
104
105 void DrawTextExt(DrawBuffer bitmap, int x, int y,
106                  char *text, int font_size, int font_type)
107 {
108   Bitmap font_bitmap;
109   int font_width, font_height, font_start;
110   boolean print_inverse = FALSE;
111
112   if (font_size != FS_SMALL && font_size != FS_BIG && font_size != FS_MEDIUM)
113     font_size = FS_SMALL;
114   if (font_type < FC_RED || font_type > FC_SPECIAL3)
115     font_type = FC_RED;
116
117   font_width = getFontWidth(font_size, font_type);
118   font_height = getFontHeight(font_size, font_type);
119
120   font_bitmap = (font_size == FS_BIG ? font.bitmap_big :
121                  font_size == FS_MEDIUM ? font.bitmap_medium :
122                  font.bitmap_small);
123   font_start = (font_type * (font_size == FS_BIG ? FONT1_YSIZE :
124                              font_size == FS_MEDIUM ? FONT6_YSIZE :
125                              FONT2_YSIZE) *
126                 FONT_LINES_PER_FONT);
127
128   if (font_type == FC_SPECIAL3)
129     font_start += (FONT4_YSIZE - FONT2_YSIZE) * FONT_LINES_PER_FONT;
130
131   while (*text)
132   {
133     char c = *text++;
134
135     if (c == '~' && font_size == FS_SMALL)
136     {
137       print_inverse = TRUE;
138       continue;
139     }
140
141     if (c >= 'a' && c <= 'z')
142       c = 'A' + (c - 'a');
143     else if (c == 'ä' || c == 'Ä')
144       c = 91;
145     else if (c == 'ö' || c == 'Ö')
146       c = 92;
147     else if (c == 'ü' || c == 'Ü')
148       c = 93;
149
150     if (c >= 32 && c <= 95)
151     {
152       int src_x = ((c - 32) % FONT_CHARS_PER_LINE) * font_width;
153       int src_y = ((c - 32) / FONT_CHARS_PER_LINE) * font_height + font_start;
154       int dest_x = x, dest_y = y;
155
156       if (print_inverse)
157       {
158         BlitBitmap(font_bitmap, bitmap,
159                    FONT_CHARS_PER_LINE * font_width,
160                    3 * font_height + font_start,
161                    font_width, font_height, x, y);
162
163         SetClipOrigin(font_bitmap, font_bitmap->stored_clip_gc,
164                       dest_x - src_x, dest_y - src_y);
165         BlitBitmapMasked(font_bitmap, bitmap,
166                          0, 0, font_width, font_height, dest_x, dest_y);
167       }
168       else
169         BlitBitmap(font_bitmap, bitmap,
170                    src_x, src_y, font_width, font_height, dest_x, dest_y);
171     }
172
173     x += font_width;
174   }
175 }