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