rnd-20001201-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 #include "main_TMP.h"
19
20 int getFontWidth(int font_size, int font_type)
21 {
22   return (font_size == FS_BIG ? FONT1_XSIZE :
23           font_size == FS_MEDIUM ? FONT6_XSIZE :
24           font_type == FC_SPECIAL1 ? FONT3_XSIZE :
25           font_type == FC_SPECIAL2 ? FONT4_XSIZE :
26           font_type == FC_SPECIAL3 ? FONT5_XSIZE :
27           FONT2_XSIZE);
28 }
29
30 int getFontHeight(int font_size, int font_type)
31 {
32   return (font_size == FS_BIG ? FONT1_YSIZE :
33           font_size == FS_MEDIUM ? FONT6_YSIZE :
34           font_type == FC_SPECIAL1 ? FONT3_YSIZE :
35           font_type == FC_SPECIAL2 ? FONT4_YSIZE :
36           font_type == FC_SPECIAL3 ? FONT5_YSIZE :
37           FONT2_YSIZE);
38 }
39
40 void DrawInitText(char *text, int ypos, int color)
41 {
42   if (window && pix[PIX_SMALLFONT])
43   {
44     ClearRectangle(window, 0, ypos, WIN_XSIZE, FONT2_YSIZE);
45     DrawTextExt(window, gc, (WIN_XSIZE - strlen(text) * FONT2_XSIZE)/2,
46                 ypos, text, FS_SMALL, color);
47     FlushDisplay();
48   }
49 }
50
51 void DrawTextFCentered(int y, int font_type, char *format, ...)
52 {
53   char buffer[FULL_SXSIZE / FONT5_XSIZE + 10];
54   int font_width = getFontWidth(FS_SMALL, font_type);
55   va_list ap;
56
57   va_start(ap, format);
58   vsprintf(buffer, format, ap);
59   va_end(ap);
60
61   DrawText(SX + (SXSIZE - strlen(buffer) * font_width) / 2, SY + y,
62            buffer, FS_SMALL, font_type);
63 }
64
65 void DrawTextF(int x, int y, int font_type, char *format, ...)
66 {
67   char buffer[FULL_SXSIZE / FONT5_XSIZE + 10];
68   va_list ap;
69
70   va_start(ap, format);
71   vsprintf(buffer, format, ap);
72   va_end(ap);
73
74   DrawText(SX + x, SY + y, buffer, FS_SMALL, font_type);
75 }
76
77 void DrawText(int x, int y, char *text, int font_size, int font_type)
78 {
79   DrawTextExt(drawto, gc, x, y, text, font_size, font_type);
80
81   if (x < DX)
82     redraw_mask |= REDRAW_FIELD;
83   else if (y < VY)
84     redraw_mask |= REDRAW_DOOR_1;
85 }
86
87 void DrawTextExt(DrawBuffer d, GC gc, int x, int y,
88                  char *text, int font_size, int font_type)
89 {
90   int font_width, font_height, font_start;
91   int font_bitmap;
92   boolean print_inverse = FALSE;
93
94   if (font_size != FS_SMALL && font_size != FS_BIG && font_size != FS_MEDIUM)
95     font_size = FS_SMALL;
96   if (font_type < FC_RED || font_type > FC_SPECIAL3)
97     font_type = FC_RED;
98
99   font_width = getFontWidth(font_size, font_type);
100   font_height = getFontHeight(font_size, font_type);
101
102   font_bitmap = (font_size == FS_BIG ? PIX_BIGFONT :
103                  font_size == FS_MEDIUM ? PIX_MEDIUMFONT :
104                  PIX_SMALLFONT);
105   font_start = (font_type * (font_size == FS_BIG ? FONT1_YSIZE :
106                              font_size == FS_MEDIUM ? FONT6_YSIZE :
107                              FONT2_YSIZE) *
108                 FONT_LINES_PER_FONT);
109
110   if (font_type == FC_SPECIAL3)
111     font_start += (FONT4_YSIZE - FONT2_YSIZE) * FONT_LINES_PER_FONT;
112
113   while (*text)
114   {
115     char c = *text++;
116
117     if (c == '~' && font_size == FS_SMALL)
118     {
119       print_inverse = TRUE;
120       continue;
121     }
122
123     if (c >= 'a' && c <= 'z')
124       c = 'A' + (c - 'a');
125     else if (c == 'ä' || c == 'Ä')
126       c = 91;
127     else if (c == 'ö' || c == 'Ö')
128       c = 92;
129     else if (c == 'ü' || c == 'Ü')
130       c = 93;
131
132     if (c >= 32 && c <= 95)
133     {
134       int src_x = ((c - 32) % FONT_CHARS_PER_LINE) * font_width;
135       int src_y = ((c - 32) / FONT_CHARS_PER_LINE) * font_height + font_start;
136       int dest_x = x, dest_y = y;
137
138       if (print_inverse)
139       {
140         BlitBitmap(pix[font_bitmap], d,
141                    FONT_CHARS_PER_LINE * font_width,
142                    3 * font_height + font_start,
143                    font_width, font_height, x, y);
144
145         SetClipOrigin(clip_gc[font_bitmap], dest_x - src_x, dest_y - src_y);
146         BlitBitmapMasked(pix_masked[font_bitmap], d,
147                          0, 0, font_width, font_height, dest_x, dest_y);
148       }
149       else
150         BlitBitmap(pix[font_bitmap], d,
151                    src_x, src_y, font_width, font_height, dest_x, dest_y);
152     }
153
154     x += font_width;
155   }
156 }