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