]> icculus.org git repositories - btb/d2x.git/blob - arch/ogl/gr.c
SHAREWARE fixes
[btb/d2x.git] / arch / ogl / gr.c
1 /* $ Id: $ */
2 /*
3  *
4  * OGL video functions. - Added 9/15/99 Matthew Mueller
5  *
6  *
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include <conf.h>
11 #endif
12
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
16 #ifdef __WINDOWS__
17 #include <windows.h>
18 #endif
19
20 #ifdef __MACOSX__
21 //#include <OpenGL/gl.h>
22 #else
23 //#include <GL/gl.h>
24 #endif
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <errno.h>
30
31 #include "hudmsg.h"
32 #include "game.h"
33 #include "text.h"
34 #include "gr.h"
35 #include "gamefont.h"
36 #include "grdef.h"
37 #include "palette.h"
38 #include "u_mem.h"
39 #include "error.h"
40
41 #include "inferno.h"
42 #include "screens.h"
43
44 #include "strutil.h"
45 #include "mono.h"
46 #include "args.h"
47 #include "key.h"
48
49 #define DECLARE_VARS
50 #include "ogl_init.h"
51 #ifdef __MACOSX__
52 #include <OpenGL/glu.h>
53 #else
54 #include <GL/glu.h>
55 #endif
56
57 int ogl_voodoohack=0;
58
59 int gr_installed = 0;
60
61
62 void gr_palette_clear(); // Function prototype for gr_init;
63 int gl_initialized=0;
64 int gl_reticle=1;
65
66 int ogl_fullscreen=0;
67
68 int gr_check_fullscreen(void){
69         return ogl_fullscreen;
70 }
71
72 void gr_do_fullscreen(int f){
73         if (ogl_voodoohack)
74                 ogl_fullscreen=1;//force fullscreen mode on voodoos.
75         else
76                 ogl_fullscreen=f;
77         if (gl_initialized){
78                 ogl_do_fullscreen_internal();
79         }
80 }
81
82 int gr_toggle_fullscreen(void){
83         gr_do_fullscreen(!ogl_fullscreen);
84         //      grd_curscreen->sc_mode=0;//hack to get it to reset screen mode
85         return ogl_fullscreen;
86 }
87
88 int arch_toggle_fullscreen_menu(void){
89         unsigned char *buf=NULL;
90
91         if (ogl_readpixels_ok){
92                 MALLOC(buf,unsigned char,grd_curscreen->sc_w*grd_curscreen->sc_h*3);
93                 glReadBuffer(GL_FRONT);
94                 glReadPixels(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
95         }
96
97         gr_do_fullscreen(!ogl_fullscreen);
98
99         if (ogl_readpixels_ok){
100 //              glWritePixels(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
101                 glRasterPos2f(0,0);
102                 glDrawPixels(grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
103                 free(buf);
104         }
105         //      grd_curscreen->sc_mode=0;//hack to get it to reset screen mode
106
107         return ogl_fullscreen;
108 }
109
110 void ogl_init_state(void){
111         /* select clearing (background) color   */
112         glClearColor(0.0, 0.0, 0.0, 0.0);
113         glShadeModel(GL_SMOOTH);
114
115         /* initialize viewing values */
116         glMatrixMode(GL_PROJECTION);
117         glLoadIdentity();
118         glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
119         glScalef(1.0, -1.0, 1.0);
120         glTranslatef(0.0, -1.0, 0.0);
121         gr_palette_step_up(0,0,0);//in case its left over from in game
122 }
123
124 int last_screen_mode=-1;
125
126 void ogl_set_screen_mode(void){
127         if (last_screen_mode==Screen_mode)
128                 return;
129         OGL_VIEWPORT(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h);
130 //      OGL_VIEWPORT(grd_curcanv->cv_bitmap.bm_x,grd_curcanv->cv_bitmap.bm_y,grd_curcanv->cv_bitmap.bm_w,grd_curcanv->cv_bitmap.bm_h);
131         if (Screen_mode==SCREEN_GAME){
132                 glDrawBuffer(GL_BACK);
133         }else{
134                 glClearColor(0.0, 0.0, 0.0, 0.0);
135                 glDrawBuffer(GL_FRONT);
136                 glClear(GL_COLOR_BUFFER_BIT);
137                 glMatrixMode(GL_PROJECTION);
138                 glLoadIdentity();//clear matrix
139                 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
140                 glMatrixMode(GL_MODELVIEW);
141                 glLoadIdentity();//clear matrix
142                 glEnable(GL_BLEND);
143                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
144         }
145         last_screen_mode=Screen_mode;
146 }
147
148 void gr_update()
149 {
150         if (gl_initialized){
151
152                 if(Screen_mode != SCREEN_GAME){
153                         glFlush();
154                 }
155         }
156 }
157
158 const char *gl_vendor,*gl_renderer,*gl_version,*gl_extensions;
159
160 void ogl_get_verinfo(void){
161         int t;
162         gl_vendor=glGetString(GL_VENDOR);
163         gl_renderer=glGetString(GL_RENDERER);
164         gl_version=glGetString(GL_VERSION);
165         gl_extensions=glGetString(GL_EXTENSIONS);
166
167         con_printf(CON_VERBOSE, "gl vendor:%s renderer:%s version:%s extensions:%s\n",gl_vendor,gl_renderer,gl_version,gl_extensions);
168
169         ogl_intensity4_ok=1;ogl_luminance4_alpha4_ok=1;ogl_rgba2_ok=1;ogl_gettexlevelparam_ok=1;
170
171 #ifdef __WINDOWS__
172         dglMultiTexCoord2fARB = (glMultiTexCoord2fARB_fp)wglGetProcAddress("glMultiTexCoord2fARB");
173         dglActiveTextureARB = (glActiveTextureARB_fp)wglGetProcAddress("glActiveTextureARB");
174         dglMultiTexCoord2fSGIS = (glMultiTexCoord2fSGIS_fp)wglGetProcAddress("glMultiTexCoord2fSGIS");
175         dglSelectTextureSGIS = (glSelectTextureSGIS_fp)wglGetProcAddress("glSelectTextureSGIS");
176 #endif
177
178         //multitexturing doesn't work yet.
179 #ifdef GL_ARB_multitexture
180         ogl_arb_multitexture_ok=0;//(strstr(gl_extensions,"GL_ARB_multitexture")!=0 && glActiveTextureARB!=0 && 0);
181         mprintf((0,"c:%p d:%p e:%p\n",strstr(gl_extensions,"GL_ARB_multitexture"),glActiveTextureARB,glBegin));
182 #endif
183 #ifdef GL_SGIS_multitexture
184         ogl_sgis_multitexture_ok=0;//(strstr(gl_extensions,"GL_SGIS_multitexture")!=0 && glSelectTextureSGIS!=0 && 0);
185         mprintf((0,"a:%p b:%p\n",strstr(gl_extensions,"GL_SGIS_multitexture"),glSelectTextureSGIS));
186 #endif
187
188         //add driver specific hacks here.  whee.
189         if ((stricmp(gl_renderer,"Mesa NVIDIA RIVA 1.0\n")==0 || stricmp(gl_renderer,"Mesa NVIDIA RIVA 1.2\n")==0) && stricmp(gl_version,"1.2 Mesa 3.0")==0){
190                 ogl_intensity4_ok=0;//ignores alpha, always black background instead of transparent.
191                 ogl_readpixels_ok=0;//either just returns all black, or kills the X server entirely
192                 ogl_gettexlevelparam_ok=0;//returns random data..
193         }
194         if (stricmp(gl_vendor,"Matrox Graphics Inc.")==0){
195                 //displays garbage. reported by
196                 //  redomen@crcwnet.com (render="Matrox G400" version="1.1.3 5.52.015")
197                 //  orulz (Matrox G200)
198                 ogl_intensity4_ok=0;
199         }
200
201         //allow overriding of stuff.
202 #ifdef GL_ARB_multitexture
203         if ((t=FindArg("-gl_arb_multitexture_ok"))){
204                 ogl_arb_multitexture_ok=atoi(Args[t+1]);
205         }
206 #endif
207 #ifdef GL_SGIS_multitexture
208         if ((t=FindArg("-gl_sgis_multitexture_ok"))){
209                 ogl_sgis_multitexture_ok=atoi(Args[t+1]);
210         }
211 #endif
212         if ((t=FindArg("-gl_intensity4_ok"))){
213                 ogl_intensity4_ok=atoi(Args[t+1]);
214         }
215         if ((t=FindArg("-gl_luminance4_alpha4_ok"))){
216                 ogl_luminance4_alpha4_ok=atoi(Args[t+1]);
217         }
218         if ((t=FindArg("-gl_rgba2_ok"))){
219                 ogl_rgba2_ok=atoi(Args[t+1]);
220         }
221         if ((t=FindArg("-gl_readpixels_ok"))){
222                 ogl_readpixels_ok=atoi(Args[t+1]);
223         }
224         if ((t=FindArg("-gl_gettexlevelparam_ok"))){
225                 ogl_gettexlevelparam_ok=atoi(Args[t+1]);
226         }
227
228         con_printf(CON_VERBOSE, "gl_arb_multitexture:%i gl_sgis_multitexture:%i\n",ogl_arb_multitexture_ok,ogl_sgis_multitexture_ok);
229         con_printf(CON_VERBOSE, "gl_intensity4:%i gl_luminance4_alpha4:%i gl_rgba2:%i gl_readpixels:%i gl_gettexlevelparam:%i\n",ogl_intensity4_ok,ogl_luminance4_alpha4_ok,ogl_rgba2_ok,ogl_readpixels_ok,ogl_gettexlevelparam_ok);
230 }
231
232 int gr_set_mode(u_int32_t mode)
233 {
234         unsigned int w,h;
235         char *gr_bm_data;
236
237 #ifdef NOGRAPH
238 return 0;
239 #endif
240 //      mode=0;
241         if (mode<=0)
242                 return 0;
243
244         w=SM_W(mode);
245         h=SM_H(mode);
246
247         //if (screen != NULL) gr_palette_clear();
248
249 //      ogl_init_state();
250         
251         gr_bm_data=grd_curscreen->sc_canvas.cv_bitmap.bm_data;//since we use realloc, we want to keep this pointer around.
252         memset( grd_curscreen, 0, sizeof(grs_screen));
253         grd_curscreen->sc_mode = mode;
254         grd_curscreen->sc_w = w;
255         grd_curscreen->sc_h = h;
256         grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
257         grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
258         grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
259         grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
260         grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
261         //grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
262         grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = w;
263         grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_OGL;
264         //grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
265 //      mprintf((0,"ogl/gr.c: reallocing %p to %i\n",grd_curscreen->sc_canvas.cv_bitmap.bm_data,w*h));
266         grd_curscreen->sc_canvas.cv_bitmap.bm_data = d_realloc(gr_bm_data,w*h);
267         gr_set_current_canvas(NULL);
268         //gr_enable_default_palette_loading();
269         
270         ogl_init_window(w,h);//platform specific code
271
272         ogl_get_verinfo();
273
274         OGL_VIEWPORT(0,0,w,h);
275
276         ogl_set_screen_mode();
277
278 //      gamefont_choose_game_font(w,h);
279         
280         return 0;
281 }
282
283 #define GLstrcmptestr(a,b) if (stricmp(a,#b)==0 || stricmp(a,"GL_" #b)==0)return GL_ ## b;
284 int ogl_atotexfilti(char *a,int min){
285         GLstrcmptestr(a,NEAREST);
286         GLstrcmptestr(a,LINEAR);
287         if (min){//mipmaps are valid only for the min filter
288                 GLstrcmptestr(a,NEAREST_MIPMAP_NEAREST);
289                 GLstrcmptestr(a,NEAREST_MIPMAP_LINEAR);
290                 GLstrcmptestr(a,LINEAR_MIPMAP_NEAREST);
291                 GLstrcmptestr(a,LINEAR_MIPMAP_LINEAR);
292         }
293         Error("unknown/invalid texture filter %s\n",a);
294 //      return GL_NEAREST;
295 }
296 int ogl_testneedmipmaps(int i){
297         switch (i){
298                 case GL_NEAREST:
299                 case GL_LINEAR:
300                         return 0;
301                 case GL_NEAREST_MIPMAP_NEAREST:
302                 case GL_NEAREST_MIPMAP_LINEAR:
303                 case GL_LINEAR_MIPMAP_NEAREST:
304                 case GL_LINEAR_MIPMAP_LINEAR:
305                         return 1;
306         }
307         Error("unknown texture filter %x\n",i);
308 //      return -1;
309 }
310 #ifdef OGL_RUNTIME_LOAD
311 #if defined(__WINDOWS__) || defined(__MINGW32__)
312 char *OglLibPath="opengl32.dll";
313 #endif
314 #ifdef __unix__
315 char *OglLibPath="libGL.so";
316 #endif
317
318 int ogl_rt_loaded=0;
319 int ogl_init_load_library(void)
320 {
321         int retcode=0;
322         if (!ogl_rt_loaded){
323                 int t;
324                 if ((t=FindArg("-gl_library")))
325                         OglLibPath=Args[t+1];
326
327                 retcode = OpenGL_LoadLibrary(true);
328                 if(retcode)
329                 {
330                         mprintf((0,"Opengl loaded ok\n"));
331         
332                         if(!glEnd)
333                         {
334                                 Error("Opengl: Functions not imported\n");
335                         }
336                 }else{
337                         Error("Opengl: error loading %s\n",OglLibPath);
338                 }
339                 ogl_rt_loaded=1;
340         }
341         return retcode;
342 }
343 #endif
344
345 int gr_init()
346 {
347  int mode = SM(640,480);
348  int retcode,t,glt=0;
349         // Only do this function once!
350         if (gr_installed==1)
351                 return -1;
352
353
354 #ifdef OGL_RUNTIME_LOAD
355         ogl_init_load_library();
356 #endif
357
358 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
359         if (FindArg("-gl_voodoo")){
360                 ogl_voodoohack=1;
361                 gr_toggle_fullscreen();
362         }
363         if (FindArg("-fullscreen"))
364                 gr_toggle_fullscreen();
365 #endif
366         if ((glt=FindArg("-gl_alttexmerge")))
367                 ogl_alttexmerge=1;
368         if ((t=FindArg("-gl_stdtexmerge")))
369                 if (t>=glt)//allow overriding of earlier args
370                         ogl_alttexmerge=0;
371                         
372         if ((glt=FindArg("-gl_16bittextures")))
373                 ogl_rgba_format=GL_RGB5_A1;
374
375         if ((glt=FindArg("-gl_mipmap"))){
376                 GL_texmagfilt=GL_LINEAR;
377                 GL_texminfilt=GL_LINEAR_MIPMAP_NEAREST;
378         }
379         if ((t=FindArg("-gl_simple"))){
380                 if (t>=glt){//allow overriding of earlier args
381                         glt=t;
382                         GL_texmagfilt=GL_NEAREST;
383                         GL_texminfilt=GL_NEAREST;
384                 }
385         }
386         if ((t=FindArg("-gl_texmagfilt")) || (t=FindArg("-gl_texmagfilter"))){
387                 if (t>=glt)//allow overriding of earlier args
388                         GL_texmagfilt=ogl_atotexfilti(Args[t+1],0);
389         }
390         if ((t=FindArg("-gl_texminfilt")) || (t=FindArg("-gl_texminfilter"))){
391                 if (t>=glt)//allow overriding of earlier args
392                         GL_texminfilt=ogl_atotexfilti(Args[t+1],1);
393         }
394         GL_needmipmaps=ogl_testneedmipmaps(GL_texminfilt);
395         mprintf((0,"gr_init: texmagfilt:%x texminfilt:%x needmipmaps=%i\n",GL_texmagfilt,GL_texminfilt,GL_needmipmaps));
396         
397         if ((t=FindArg("-gl_vidmem"))){
398                 ogl_mem_target=atoi(Args[t+1])*1024*1024;
399         }
400         if ((t=FindArg("-gl_reticle"))){
401                 gl_reticle=atoi(Args[t+1]);
402         }
403         //printf("ogl_mem_target=%i\n",ogl_mem_target);
404         
405         ogl_init();//platform specific initialization
406
407         ogl_init_texture_list_internal();
408                 
409         MALLOC( grd_curscreen,grs_screen,1 );
410         memset( grd_curscreen, 0, sizeof(grs_screen));
411         grd_curscreen->sc_canvas.cv_bitmap.bm_data = NULL;
412
413         // Set the mode.
414         if ((retcode=gr_set_mode(mode)))
415         {
416                 return retcode;
417         }
418         grd_curscreen->sc_canvas.cv_color = 0;
419         grd_curscreen->sc_canvas.cv_drawmode = 0;
420         grd_curscreen->sc_canvas.cv_font = NULL;
421         grd_curscreen->sc_canvas.cv_font_fg_color = 0;
422         grd_curscreen->sc_canvas.cv_font_bg_color = 0;
423         gr_set_current_canvas( &grd_curscreen->sc_canvas );
424
425         gr_installed = 1;
426         
427         atexit(gr_close);
428
429         return 0;
430 }
431
432 void gr_close()
433 {
434 //      mprintf((0,"ogl init: %s %s %s - %s\n",glGetString(GL_VENDOR),glGetString(GL_RENDERER),glGetString(GL_VERSION),glGetString,(GL_EXTENSIONS)));
435
436         ogl_close();//platform specific code
437         if (grd_curscreen){
438                 if (grd_curscreen->sc_canvas.cv_bitmap.bm_data)
439                         d_free(grd_curscreen->sc_canvas.cv_bitmap.bm_data);
440                 d_free(grd_curscreen);
441         }
442 #ifdef OGL_RUNTIME_LOAD
443         if (ogl_rt_loaded)
444                 OpenGL_LoadLibrary(false);
445 #endif
446 }
447 extern int r_upixelc;
448 void ogl_upixelc(int x, int y, int c){
449         r_upixelc++;
450 //      printf("gr_upixelc(%i,%i,%i)%i\n",x,y,c,Function_mode==FMODE_GAME);
451 //      if(Function_mode != FMODE_GAME){
452 //              grd_curcanv->cv_bitmap.bm_data[y*grd_curscreen->sc_canvas.cv_bitmap.bm_w+x]=c;
453 //      }else{
454                 OGL_DISABLE(TEXTURE_2D);
455                 glPointSize(1.0);
456                 glBegin(GL_POINTS);
457 //              glBegin(GL_LINES);
458 //      ogl_pal=gr_current_pal;
459                 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
460 //      ogl_pal=gr_palette;
461                 glVertex2f((x+grd_curcanv->cv_bitmap.bm_x)/(float)last_width,1.0-(y+grd_curcanv->cv_bitmap.bm_y)/(float)last_height);
462 //              glVertex2f(x/((float)last_width+1),1.0-y/((float)last_height+1));
463                 glEnd();
464 //      }
465 }
466 void ogl_urect(int left,int top,int right,int bot){
467         GLfloat xo,yo,xf,yf;
468         int c=COLOR;
469         
470         xo=(left+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
471         xf=(right+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
472         yo=1.0-(top+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
473         yf=1.0-(bot+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
474         
475         OGL_DISABLE(TEXTURE_2D);
476         glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
477         glBegin(GL_QUADS);
478         glVertex2f(xo,yo);
479         glVertex2f(xo,yf);
480         glVertex2f(xf,yf);
481         glVertex2f(xf,yo);
482         glEnd();
483 }
484 void ogl_ulinec(int left,int top,int right,int bot,int c){
485         GLfloat xo,yo,xf,yf;
486         
487         xo=(left+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
488         xf=(right+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
489         yo=1.0-(top+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
490         yf=1.0-(bot+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
491         
492         OGL_DISABLE(TEXTURE_2D);
493         glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
494         glBegin(GL_LINES);
495         glVertex2f(xo,yo);
496         glVertex2f(xf,yf);
497         glEnd();
498 }
499         
500
501 GLfloat last_r=0, last_g=0, last_b=0;
502 int do_pal_step=0;
503 void ogl_do_palfx(void){
504 //      GLfloat r,g,b,a;
505         OGL_DISABLE(TEXTURE_2D);
506         if (gr_palette_faded_out){
507 /*              glEnable(GL_BLEND);
508                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);*/
509                 glColor3f(0,0,0);
510 //              r=g=b=0.0;a=1.0;
511         }else{
512                 if (do_pal_step){
513                         //glBlendFunc(GL_SRC_COLOR, GL_DST_COLOR);
514                         glEnable(GL_BLEND);
515                         glBlendFunc(GL_ONE,GL_ONE);
516                         glColor3f(last_r,last_g,last_b);
517 //                      r=f2fl(last_r);g=f2fl(last_g);b=f2fl(last_b);a=0.5;
518                 }else
519                         return;
520         }
521         
522         
523         glBegin(GL_QUADS);
524         glVertex2f(0,0);
525         glVertex2f(0,1);
526         glVertex2f(1,1);
527         glVertex2f(1,0);
528         glEnd();
529         
530         glEnable(GL_BLEND);     
531         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
532 }
533
534 void gr_palette_clear()
535 {
536         gr_palette_faded_out=1;
537 }
538
539
540 void gr_palette_step_up( int r, int g, int b )
541 {
542         if (gr_palette_faded_out) return;
543
544 //      if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
545
546 /*      last_r = r/63.0;
547         last_g = g/63.0;
548         last_b = b/63.0;
549         do_pal_step=(r || g || b);*/
550         
551         last_r = (r+gr_palette_gamma)/63.0;
552         last_g = (g+gr_palette_gamma)/63.0;
553         last_b = (b+gr_palette_gamma)/63.0;
554
555         do_pal_step=(r || g || b || gr_palette_gamma);
556         
557 }
558
559 //added on 980913 by adb to fix palette problems
560 // need a min without side effects...
561 #undef min
562 static inline int min(int x, int y) { return x < y ? x : y; }
563 //end changes by adb
564
565 void gr_palette_load( ubyte *pal )      
566 {
567  int i;//, j;
568
569  for (i=0; i<768; i++ ) {
570      gr_current_pal[i] = pal[i];
571      if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
572  }
573  //palette = screen->format->palette;
574
575  gr_palette_faded_out=0;
576
577  init_computed_colors();
578 }
579
580
581
582 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
583 {
584         gr_palette_faded_out=1;
585         return 0;
586 }
587
588
589
590 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
591 {
592         gr_palette_faded_out=0;
593         return 0;
594 }
595
596
597
598 void gr_palette_read(ubyte * pal)
599 {
600         int i;
601         for (i=0; i<768; i++ ) {
602                 pal[i]=gr_current_pal[i];
603                 if (pal[i] > 63) pal[i] = 63;
604         }
605 }
606
607 //writes out an uncompressed RGB .tga file
608 //if we got really spiffy, we could optionally link in libpng or something, and use that.
609 void write_bmp(char *savename,int w,int h,unsigned char *buf){
610         int f;
611 #if defined(__WINDOWS__) || defined(__MINGW32__)
612         f=open(savename,O_CREAT|O_EXCL|O_WRONLY,S_IRUSR|S_IWUSR);
613 #else
614         f=open(savename,O_CREAT|O_EXCL|O_WRONLY,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
615 #endif
616         if (f>=0){
617                 GLubyte    targaMagic[12] = { 0, //no identification field
618                          0,//no colormap
619                          2,//RGB image (well, BGR, actually)
620                          0, 0, 0, 0, 0, 0, 0, 0, 0 };//no colormap or image origin stuff.
621                 GLubyte blah;
622                 int r;
623                 GLubyte *s;
624                 int x,y;
625                 
626                 //write .TGA header.
627                 write (f,targaMagic,sizeof(targaMagic));
628                 blah=w%256;write (f,&blah,1);//w, low
629                 blah=w/256;write (f,&blah,1);//w, high
630                 blah=h%256;write (f,&blah,1);//h, low
631                 blah=h/256;write (f,&blah,1);//h, high
632                 blah=24;write (f,&blah,1);//24 bpp
633                 blah=0;write (f,&blah,1);//no attribute bits, origin is lowerleft, no interleave
634                 
635                 s=buf;
636                 for (y=0;y<h;y++){//TGAs use BGR ordering of data.
637                         for (x=0;x<w;x++){
638                                 blah=s[0];
639                                 s[0]=s[2];
640                                 s[2]=blah;
641                                 s+=3;                           
642                         }
643                 }
644                 x=0;y=w*h*3;
645                 while (x<y){
646                         r=write(f,buf+x,y);
647                         if (r<=0){
648                                 mprintf((0,"screenshot error, couldn't write to %s (err %i)\n",savename,errno));
649                                 break;
650                         }
651                         x+=r;y-=r;
652                 }
653                 close(f);
654         }else{
655                 mprintf((0,"screenshot error, couldn't open %s (err %i)\n",savename,errno));
656         }
657 }
658 void save_screen_shot(int automap_flag)
659 {
660 //      fix t1;
661         char message[100];
662         static int savenum=0;
663         char savename[13];
664         unsigned char *buf;
665         
666         if (!ogl_readpixels_ok){
667                 if (!automap_flag)
668                         hud_message(MSGC_GAME_FEEDBACK,"glReadPixels not supported on your configuration");
669                 return;
670         }
671
672         stop_time();
673
674 //added/changed on 10/31/98 by Victor Rachels to fix overwrite each new game
675         if ( savenum == 9999 ) savenum = 0;
676         sprintf(savename,"scrn%04d.tga",savenum++);
677
678         while(!access(savename,0))
679         {
680                 if ( savenum == 9999 ) savenum = 0;
681                 sprintf(savename,"scrn%04d.tga",savenum++);
682         }
683         sprintf( message, "%s '%s'", TXT_DUMPING_SCREEN, savename );
684 //end this section addition/change - Victor Rachels
685
686         if (automap_flag) {
687 //      save_font = grd_curcanv->cv_font;
688 //      gr_set_curfont(GAME_FONT);
689 //      gr_set_fontcolor(gr_find_closest_color_current(0,31,0),-1);
690 //      gr_get_string_size(message,&w,&h,&aw);
691 //              modex_print_message(32, 2, message);
692         } else {
693                 hud_message(MSGC_GAME_FEEDBACK,message);
694         }
695         
696         buf = d_malloc(grd_curscreen->sc_w*grd_curscreen->sc_h*3);
697         glReadBuffer(GL_FRONT);
698         glReadPixels(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
699         write_bmp(savename,grd_curscreen->sc_w,grd_curscreen->sc_h,buf);
700         d_free(buf);
701
702         key_flush();
703         start_time();
704 }