]> icculus.org git repositories - btb/d2x.git/blob - main/text.c
use the orientation parameter of g3_draw_bitmap
[btb/d2x.git] / main / text.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 /*
15  *
16  * Code for localizable text
17  *
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <conf.h>
22 #endif
23
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "pstypes.h"
28 #include "cfile.h"
29 #include "u_mem.h"
30 #include "dxxerror.h"
31 #include "inferno.h"
32 #include "args.h"
33
34
35 #define SHAREWARE_TEXTSIZE  14677
36
37 char *text;
38
39 char *Text_string[N_TEXT_STRINGS];
40
41 void free_text()
42 {
43         d_free(text);
44 }
45
46 // rotates a byte left one bit, preserving the bit falling off the right
47 void
48 encode_rotate_left(char *c)
49 {
50         int found;
51
52         found = 0;
53         if (*c & 0x80)
54                 found = 1;
55         *c = *c << 1;
56         if (found)
57                 *c |= 0x01;
58 }
59
60 #define BITMAP_TBL_XOR 0xD3
61
62 //decode an encoded line of text of bitmaps.tbl
63 void decode_text_line(char *p)
64 {
65         for (;*p;p++) {
66                 encode_rotate_left(p);
67                 *p = *p ^ BITMAP_TBL_XOR;
68                 encode_rotate_left(p);
69         }
70 }
71
72 // decode buffer of text, preserves newlines
73 void decode_text(char *buf, int len)
74 {
75         char *ptr;
76         int i;
77
78         for (i = 0, ptr = buf; i < len; i++, ptr++)
79         {
80                 if (*ptr != '\n')
81                 {
82                         encode_rotate_left(ptr);
83                         *ptr = *ptr  ^ BITMAP_TBL_XOR;
84                         encode_rotate_left(ptr);
85                 }
86         }
87 }
88
89
90 #if !defined(_MSC_VER) && !defined(macintosh)
91 #include <unistd.h>
92 #endif
93 //load all the text strings for Descent
94 void load_text()
95 {
96         CFILE  *tfile;
97         CFILE *ifile;
98         int len,i, have_binary = 0;
99         char *tptr;
100         char *filename="descent.tex";
101
102         if ((i=FindArg("-text"))!=0)
103                 filename = Args[i+1];
104
105         if ((tfile = cfopen(filename,"rb")) == NULL) {
106                 filename="descent.txb";
107                 if ((ifile = cfopen(filename, "rb")) == NULL) {
108                         Error("Cannot open file DESCENT.TEX or DESCENT.TXB");
109                         return;
110                 }
111                 have_binary = 1;
112
113                 len = cfilelength(ifile);
114
115                 MALLOC(text,char,len);
116
117                 atexit(free_text);
118
119                 cfread(text,1,len,ifile);
120
121                 cfclose(ifile);
122
123         } else {
124                 int c;
125                 char * p;
126
127                 len = cfilelength(tfile);
128
129                 MALLOC(text,char,len);
130
131                 atexit(free_text);
132
133                 //fread(text,1,len,tfile);
134                 p = text;
135                 do {
136                         c = cfgetc( tfile );
137                         if ( c != 13 )
138                                 *p++ = c;
139                 } while ( c!=EOF );
140
141                 cfclose(tfile);
142         }
143
144         for (i=0,tptr=text;i<N_TEXT_STRINGS;i++) {
145                 char *p;
146
147                 Text_string[i] = tptr;
148
149                 tptr = strchr(tptr,'\n');
150
151                 if (!tptr)
152                 {
153                         if (i == 644) break;    /* older datafiles */
154
155                         Error("Not enough strings in text file - expecting %d, found %d\n", N_TEXT_STRINGS, i);
156                 }
157
158                 if ( tptr ) *tptr++ = 0;
159
160                 if (have_binary)
161                         decode_text_line(Text_string[i]);
162
163                 //scan for special chars (like \n)
164                 for (p=Text_string[i];(p=strchr(p,'\\'));) {
165                         char newchar, *q;
166
167                         if (p[1] == 'n') newchar = '\n';
168                         else if (p[1] == 't') newchar = '\t';
169                         else if (p[1] == '\\') newchar = '\\';
170                         else
171                                 Error("Unsupported key sequence <\\%c> on line %d of file <%s>",p[1],i+1,filename);
172
173                         p[0] = newchar;
174
175                         //shift rest of string to left
176                         for (q = p+1; *q; q++) {
177                                 *q = *(q+1);
178                         }
179
180                         p++;
181                 }
182
183         }
184
185
186         if (i == 644)
187         {
188                 if (len == SHAREWARE_TEXTSIZE)
189                 {
190                         Text_string[173] = Text_string[172];
191                         Text_string[172] = Text_string[171];
192                         Text_string[171] = Text_string[170];
193                         Text_string[170] = Text_string[169];
194                         Text_string[169] = "Windows Joystick";
195                 }
196
197                 Text_string[644] = "Z1";
198                 Text_string[645] = "UN";
199                 Text_string[646] = "P1";
200                 Text_string[647] = "R1";
201                 Text_string[648] = "Y1";
202         }
203
204         //Assert(tptr==text+len || tptr==text+len-2);
205
206 }
207
208