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