Got rid of all compiler warnings, for non-OpenGL on linux, anyway...
[btb/d2x.git] / 2d / diff
1 13a14
2 > #include <conf.h>
3 23c24
4 < #include "mem.h"
5 ---
6 > #include "u_mem.h"
7 31a33
8 > #include "bitmap.h"
9 133a136
10
11 135a139,156
12 > //hack to allow color codes to be embedded in strings -MPM
13 > //note we subtract one from color, since 255 is "transparent" so it'll never be used, and 0 would otherwise end the string.
14 > //function must already have orig_color var set (or they could be passed as args...)
15 > //perhaps some sort of recursive orig_color type thing would be better, but that would be way too much trouble for little gain
16 > int gr_message_color_level=1;
17 > #define CHECK_EMBEDDED_COLORS() if ((*text_ptr >= 0x01) && (*text_ptr <= 0x03)) { \
18 >               text_ptr++; \
19 >               if (*text_ptr){ \
20 >                       if (gr_message_color_level >= *(text_ptr-1)) \
21 >                               FG_COLOR = *text_ptr - 1; \
22 >                       text_ptr++; \
23 >               } \
24 >       } \
25 >       else if ((*text_ptr >= 0x04) && (*text_ptr <= 0x06)){ \
26 >               if (gr_message_color_level >= *text_ptr - 3) \
27 >                       FG_COLOR=orig_color; \
28 >               text_ptr++; \
29 >       }
30 359c380
31
32 ---
33 > #ifdef __ENV_MSDOS__ 
34 749a771
35 > #endif
36 975a998
37 > #ifndef OGL
38 1049a1073,1336
39 > #else // OGL
40
41 > #include "../main/inferno.h"
42 > #include "ogl_init.h"
43 > #include "args.h"
44 > //font handling routines for OpenGL - Added 9/25/99 Matthew Mueller - they are here instead of in arch/ogl because they use all these defines
45
46 > int pow2ize(int x);//from ogl.c
47
48 > int get_font_total_width(grs_font * font){
49 >       if (font->ft_flags & FT_PROPORTIONAL){
50 >               int i,w=0,c=font->ft_minchar;
51 >               for (i=0;c<=font->ft_maxchar;i++,c++){
52 >                       if (font->ft_widths[i]<0)
53 >                               Error("heh?\n");
54 >                       w+=font->ft_widths[i];
55 >               }
56 >               return w;
57 >       }else{
58 >               return font->ft_w*(font->ft_maxchar-font->ft_minchar+1);
59 >       }
60 > }
61 > void ogl_font_choose_size(grs_font * font,int gap,int *rw,int *rh){
62 >       int     nchars = font->ft_maxchar-font->ft_minchar+1;
63 >       int r,x,y,nc=0,smallest=999999,smallr=-1,tries;
64 >       int smallprop=10000;
65 >       int h,w;
66 >       for (h=32;h<=256;h*=2){
67 > //            h=pow2ize(font->ft_h*rows+gap*(rows-1));
68 >               if (font->ft_h>h)continue;
69 >               r=(h/(font->ft_h+gap));
70 >               w=pow2ize((get_font_total_width(font)+(nchars-r)*gap)/r);
71 >               tries=0;
72 >               do {
73 >                       if (tries)
74 >                               w=pow2ize(w+1);
75 >                       if(tries>3){
76 >                               mprintf((0,"failed to fit (%ix%i, %ic)\n",w,h,nc));
77 >                               break;
78 >                       }
79 >                       nc=0;
80 >                       y=0;
81 >                       while(y+font->ft_h<=h){
82 >                               x=0;
83 >                               while (x<w){
84 >                                       if (nc==nchars)
85 >                                               break;
86 >                                       if (font->ft_flags & FT_PROPORTIONAL){
87 >                                               if (x+font->ft_widths[nc]+gap>w)break;
88 >                                               x+=font->ft_widths[nc++]+gap;
89 >                                       }else{
90 >                                               if (x+font->ft_w+gap>w)break;
91 >                                               x+=font->ft_w+gap;
92 >                                               nc++;
93 >                                       }
94 >                               }
95 >                               if (nc==nchars)
96 >                                       break;
97 >                               y+=font->ft_h+gap;
98 >                       }
99 >                       
100 >                       tries++;
101 >               }while(nc!=nchars);
102 >               if (nc!=nchars)
103 >                       continue;
104 >               mprintf((0,"fit: %ix%i  %i tries\n",w,h,tries));
105
106 >               if (w*h==smallest){//this gives squarer sizes priority (ie, 128x128 would be better than 512*32)
107 >                       if (w>=h){
108 >                               if (w/h<smallprop){
109 >                                       smallprop=w/h;
110 >                                       smallest++;//hack
111 >                               }
112 >                       }else{
113 >                               if (h/w<smallprop){
114 >                                       smallprop=h/w;
115 >                                       smallest++;//hack
116 >                               }
117 >                       }
118 >               }
119 >               if (w*h<smallest){
120 >                       smallr=1;
121 >                       smallest=w*h;
122 >                       *rw=w;
123 >                       *rh=h;
124 >               }
125 >       }
126 >       if (smallr<=0)
127 >               Error("couldn't fit font?\n");
128 >       mprintf((0,"using %ix%i\n",*rw,*rh));
129 >       printf("using %ix%i\n",*rw,*rh);
130 >       
131 > }
132
133 > void ogl_init_font(grs_font * font){
134 >       int     nchars = font->ft_maxchar-font->ft_minchar+1;
135 >       int i,w,h,tw,th,x,y,curx=0,cury=0;
136 >       char *fp;
137 >       //      char data[32*32*4];
138 >       char *data;
139 >       int gap=0;//having a gap just wastes ram, since we don't filter text textures at all.
140 >       //      char s[2];
141 >       ogl_font_choose_size(font,gap,&tw,&th);
142 >       data=malloc(tw*th);
143 >       gr_init_bitmap(&font->ft_parent_bitmap,BM_LINEAR,0,0,tw,th,tw,data);
144
145 >       font->ft_parent_bitmap.gltexture=ogl_get_free_texture();
146
147 >       font->ft_bitmaps=(grs_bitmap*)malloc( nchars * sizeof(grs_bitmap));
148 >       mprintf((0,"ogl_init_font %s, %s, nchars=%i, (%ix%i tex)\n",(font->ft_flags & FT_PROPORTIONAL)?"proportional":"fixedwidth",(font->ft_flags & FT_COLOR)?"color":"mono",nchars,tw,th));
149 >       //      s[1]=0;
150 >       h=font->ft_h;
151 >       //      sleep(5);
152
153 >       for(i=0;i<nchars;i++){
154 >               //              s[0]=font->ft_minchar+i;
155 >               //              gr_get_string_size(s,&w,&h,&aw);
156 >               if (font->ft_flags & FT_PROPORTIONAL)
157 >                       w=font->ft_widths[i];
158 >               else
159 >                       w=font->ft_w;
160 > //            mprintf((0,"char %i(%ix%i): ",i,w,h));
161 >               if (w<1 || w>256){
162 >                       mprintf((0,"grr\n"));continue;
163 >               }
164 >               if (curx+w+gap>tw){
165 >                       cury+=h+gap;
166 >                       curx=0;
167 >               }
168 >               if (cury+h>th)
169 >                       Error("font doesn't really fit (%i/%i)?\n",i,nchars);
170 >               if (font->ft_flags & FT_COLOR) {
171 >                       if (font->ft_flags & FT_PROPORTIONAL)
172 >                               fp = font->ft_chars[i];
173 >                       else
174 >                               fp = font->ft_data + i * w*h;
175 >                       for (y=0;y<h;y++)
176 >                               for (x=0;x<w;x++){
177 >                                       font->ft_parent_bitmap.bm_data[curx+x+(cury+y)*tw]=fp[x+y*w];
178 >                               }
179
180 >                       //                      gr_init_bitmap(&font->ft_bitmaps[i],BM_LINEAR,0,0,w,h,w,font->);
181 >               }else{
182 >                       int BitMask,bits=0,white=gr_find_closest_color(63,63,63);
183 >                       //                      if (w*h>sizeof(data))
184 >                       //                              Error("ogl_init_font: toobig\n");
185 >                       if (font->ft_flags & FT_PROPORTIONAL)
186 >                               fp = font->ft_chars[i];
187 >                       else
188 >                               fp = font->ft_data + i * BITS_TO_BYTES(w)*h;
189 >                       for (y=0;y<h;y++){
190 >                               BitMask=0;
191 >                               for (x=0; x< w; x++ )
192 >                               {
193 >                                       if (BitMask==0) {
194 >                                               bits = *fp++;
195 >                                               BitMask = 0x80;
196 >                                       }
197
198 >                                       if (bits & BitMask)
199 >                                               font->ft_parent_bitmap.bm_data[curx+x+(cury+y)*tw]=white;
200 >                                       else
201 >                                               font->ft_parent_bitmap.bm_data[curx+x+(cury+y)*tw]=255;
202 >                                       BitMask >>= 1;
203 >                               }
204 >                       }
205 >               }
206 >               gr_init_sub_bitmap(&font->ft_bitmaps[i],&font->ft_parent_bitmap,curx,cury,w,h);
207
208 >               curx+=w+gap;
209 >       }
210 >       if (!(font->ft_flags & FT_COLOR)) {
211 >               //use GL_INTENSITY instead of GL_RGB
212 >               if (ogl_intensity4_ok){
213 >                       font->ft_parent_bitmap.gltexture->internalformat=GL_INTENSITY4;
214 >                       font->ft_parent_bitmap.gltexture->format=GL_LUMINANCE;
215 >               }else if (ogl_luminance4_alpha4_ok){
216 >                       font->ft_parent_bitmap.gltexture->internalformat=GL_LUMINANCE4_ALPHA4;
217 >                       font->ft_parent_bitmap.gltexture->format=GL_LUMINANCE_ALPHA;
218 >               }else if (ogl_rgba2_ok){
219 >                       font->ft_parent_bitmap.gltexture->internalformat=GL_RGBA2;
220 >                       font->ft_parent_bitmap.gltexture->format=GL_RGBA;
221 >               }else{
222 >                       font->ft_parent_bitmap.gltexture->internalformat=ogl_rgba_format;
223 >                       font->ft_parent_bitmap.gltexture->format=GL_RGBA;
224 >               }
225 >       }
226 >       ogl_loadbmtexture_m(&font->ft_parent_bitmap,0);
227 > }
228
229 > int ogl_internal_string(int x, int y, char *s )
230 > {
231 >       ubyte * text_ptr, * next_row, * text_ptr1;
232 >       int width, spacing,letter;
233 >       int xx,yy;
234 >       int orig_color=FG_COLOR;//to allow easy reseting to default string color with colored strings -MPM
235
236 >       next_row = s;
237
238 >       yy = y;
239
240 >       if (grd_curscreen->sc_canvas.cv_bitmap.bm_type != BM_OGL)
241 >               Error("carp.\n");
242 >       while (next_row != NULL)
243 >       {
244 >               text_ptr1 = next_row;
245 >               next_row = NULL;
246
247 >               text_ptr = text_ptr1;
248
249 >               xx = x;
250
251 >               if (xx==0x8000)                 //centered
252 >                       xx = get_centered_x(text_ptr);
253
254 >               while (*text_ptr)
255 >               {
256 >                       if (*text_ptr == '\n' )
257 >                       {
258 >                               next_row = &text_ptr[1];
259 >                               yy += FHEIGHT;
260 >                               break;
261 >                       }
262
263 >                       letter = *text_ptr-FMINCHAR;
264
265 >                       get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
266
267 >                       if (!INFONT(letter) || *text_ptr<=0x06) {       //not in font, draw as space
268 >                               CHECK_EMBEDDED_COLORS() else{
269 >                                       xx += spacing;
270 >                                       text_ptr++;
271 >                               }
272 >                               continue;
273 >                       }
274 >                       
275 > //                    ogl_ubitblt(FONT->ft_bitmaps[letter].bm_w,FONT->ft_bitmaps[letter].bm_h,xx,yy,0,0,&FONT->ft_bitmaps[letter],NULL);
276 > //                    if (*text_ptr>='0' && *text_ptr<='9'){
277 >                       printf("%p\n",&FONT->ft_bitmaps[letter]);
278 >                       if (FFLAGS&FT_COLOR)
279 >                               gr_ubitmapm(xx,yy,&FONT->ft_bitmaps[letter]);
280 >                       else{
281 >                               if (grd_curcanv->cv_bitmap.bm_type==BM_OGL)
282 >                                       ogl_ubitmapm_c(xx,yy,&FONT->ft_bitmaps[letter],FG_COLOR);
283 >                               else
284 >                                       Error("ogl_internal_string: non-color string to non-ogl dest\n");
285 > //                                    gr_ubitmapm(xx,yy,&FONT->ft_bitmaps[letter]);//ignores color..
286 >                       }
287 >                       //}
288
289 >                       xx += spacing;
290
291 >                       text_ptr++;
292 >               }
293
294 >       }
295 >       return 0;
296 > }
297 > int gr_internal_color_string(int x, int y, char *s ){
298 >       return ogl_internal_string(x,y,s);
299 > }
300
301 > #endif // OGL
302
303 1118a1406
304 > #ifdef __ENV_MSDOS__
305 1123a1412
306 > #endif
307 1248a1538,1539
308 > #define swapshort(a) (a)
309 > #define swapint(a) (a)
310 1251a1543
311 >       old_grs_font *oldfont;
312 1284c1576,1578
313 <       font = (grs_font *) malloc(datasize);
314 ---
315 >       oldfont = (old_grs_font *) malloc(datasize);
316 >       font = (grs_font *) malloc(sizeof(grs_font));
317 >       font->oldfont = oldfont;
318 1288c1582,1590
319 <       cfread(font,1,datasize,fontfile);
320 ---
321 >       cfread(oldfont,1,datasize,fontfile);
322 >         font->ft_flags=swapshort(oldfont->ft_flags);
323 >         font->ft_w=swapshort(oldfont->ft_w);
324 >         font->ft_h=swapshort(oldfont->ft_h);
325 >         font->ft_baseline=swapshort(oldfont->ft_baseline);
326 >         font->ft_maxchar=oldfont->ft_maxchar;
327 >         font->ft_minchar=oldfont->ft_minchar;
328 >         font->ft_bytewidth=swapshort(oldfont->ft_bytewidth);
329
330 1307c1609
331 <               font->ft_widths = (short *) (((int) font->ft_widths) + ((ubyte *) font));
332 ---
333 >               font->ft_widths = (short *) (((int) oldfont->ft_widths) + ((ubyte *) oldfont));
334 1311c1613
335 <                       font->ft_widths[i] = SWAPSHORT(font->ft_widths[i]);
336 ---
337 >                       font->ft_widths[i] = swapshort(oldfont->ft_widths[i]);
338 1314c1616
339 <               font->ft_data = ((int) font->ft_data) + ((ubyte *) font);
340 ---
341 >               font->ft_data = ((int) oldfont->ft_data) + ((ubyte *) oldfont);
342 1330c1632
343 <               font->ft_data = ((unsigned char *) font) + sizeof(*font);
344 ---
345 >               font->ft_data = ((unsigned char *) oldfont) + sizeof(*oldfont);
346 1339c1641
347 <               font->ft_kerndata = ((int) font->ft_kerndata) + ((ubyte *) font);
348 ---
349 >               font->ft_kerndata = swapint(((int) oldfont->ft_kerndata) + ((ubyte *) oldfont));
350 1386a1689,1692
351
352 > #ifdef OGL
353 >       ogl_init_font(font);
354 > #endif