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