added new arg and printfs for mouse driver problems (d1x r1.2, r1.2)
[btb/d2x.git] / arch / ogl / gr.c
1 /* $Id: gr.c,v 1.25 2004-05-20 23:38:18 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         float anisotropic_max = 0;
167
168         gl_vendor=glGetString(GL_VENDOR);
169         gl_renderer=glGetString(GL_RENDERER);
170         gl_version=glGetString(GL_VERSION);
171         gl_extensions=glGetString(GL_EXTENSIONS);
172
173         con_printf(CON_VERBOSE, "gl vendor:%s renderer:%s version:%s extensions:%s\n",gl_vendor,gl_renderer,gl_version,gl_extensions);
174
175         ogl_intensity4_ok = 1;
176         ogl_luminance4_alpha4_ok = 1;
177         ogl_rgba2_ok = 1;
178         ogl_gettexlevelparam_ok = 1;
179         ogl_setgammaramp_ok = 1;
180
181 #ifdef WGL_VIDEO
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 #ifdef GL_ARB_multitexture
189         ogl_arb_multitexture_ok = (strstr(gl_extensions, "GL_ARB_multitexture") != 0 && glActiveTextureARB != 0);
190         mprintf((0,"c:%p d:%p e:%p\n",strstr(gl_extensions,"GL_ARB_multitexture"),glActiveTextureARB,glBegin));
191 #endif
192 #ifdef GL_SGIS_multitexture
193         ogl_sgis_multitexture_ok = (strstr(gl_extensions, "GL_SGIS_multitexture") != 0 && glSelectTextureSGIS != 0);
194         mprintf((0,"a:%p b:%p\n",strstr(gl_extensions,"GL_SGIS_multitexture"),glSelectTextureSGIS));
195 #endif
196         ogl_nv_texture_env_combine4_ok = (strstr(gl_extensions, "GL_NV_texture_env_combine4") != 0);
197
198         ogl_ext_texture_filter_anisotropic_ok = (strstr(gl_extensions, "GL_EXT_texture_filter_anisotropic") != 0);
199         if (ogl_ext_texture_filter_anisotropic_ok)
200                 glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &anisotropic_max);
201
202         //add driver specific hacks here.  whee.
203         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){
204                 ogl_intensity4_ok=0;//ignores alpha, always black background instead of transparent.
205                 ogl_readpixels_ok=0;//either just returns all black, or kills the X server entirely
206                 ogl_gettexlevelparam_ok=0;//returns random data..
207         }
208         if (stricmp(gl_vendor,"Matrox Graphics Inc.")==0){
209                 //displays garbage. reported by
210                 //  redomen@crcwnet.com (render="Matrox G400" version="1.1.3 5.52.015")
211                 //  orulz (Matrox G200)
212                 ogl_intensity4_ok=0;
213         }
214
215         //allow overriding of stuff.
216 #ifdef GL_ARB_multitexture
217         if ((t=FindArg("-gl_arb_multitexture_ok"))){
218                 ogl_arb_multitexture_ok=atoi(Args[t+1]);
219         }
220         if (ogl_arb_multitexture_ok)
221                 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &arb_max_textures);
222 #endif
223 #ifdef GL_SGIS_multitexture
224         if ((t=FindArg("-gl_sgis_multitexture_ok"))){
225                 ogl_sgis_multitexture_ok=atoi(Args[t+1]);
226         }
227         if (ogl_sgis_multitexture_ok)
228                 glGetIntegerv(GL_MAX_TEXTURES_SGIS, &sgi_max_textures);
229 #endif
230         if ((t=FindArg("-gl_intensity4_ok"))){
231                 ogl_intensity4_ok=atoi(Args[t+1]);
232         }
233         if ((t=FindArg("-gl_luminance4_alpha4_ok"))){
234                 ogl_luminance4_alpha4_ok=atoi(Args[t+1]);
235         }
236         if ((t=FindArg("-gl_rgba2_ok"))){
237                 ogl_rgba2_ok=atoi(Args[t+1]);
238         }
239         if ((t=FindArg("-gl_readpixels_ok"))){
240                 ogl_readpixels_ok=atoi(Args[t+1]);
241         }
242         if ((t=FindArg("-gl_gettexlevelparam_ok"))){
243                 ogl_gettexlevelparam_ok=atoi(Args[t+1]);
244         }
245         if ((t=FindArg("-gl_setgammaramp_ok")))
246         {
247                 ogl_setgammaramp_ok = atoi(Args[t + 1]);
248         }
249
250         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);
251         con_printf(CON_VERBOSE, "gl_intensity4:%i gl_luminance4_alpha4:%i gl_rgba2:%i gl_readpixels:%i gl_gettexlevelparam:%i gl_setgammaramp_ok:%i gl_ext_texture_filter_anisotropic:%i(%f max)\n", ogl_intensity4_ok, ogl_luminance4_alpha4_ok, ogl_rgba2_ok, ogl_readpixels_ok, ogl_gettexlevelparam_ok, ogl_setgammaramp_ok, ogl_ext_texture_filter_anisotropic_ok, anisotropic_max);
252 }
253
254
255 int gr_check_mode(u_int32_t mode)
256 {
257         int w, h;
258
259         w = SM_W(mode);
260         h = SM_H(mode);
261         return ogl_check_mode(w, h); // platform specific code
262 }
263
264
265 extern int VGA_current_mode; // DPH: kludge - remove at all costs
266
267 int gr_set_mode(u_int32_t mode)
268 {
269         unsigned int w,h;
270         char *gr_bm_data;
271
272 #ifdef NOGRAPH
273 return 0;
274 #endif
275 //      mode=0;
276         if (mode<=0)
277                 return 0;
278
279         w=SM_W(mode);
280         h=SM_H(mode);
281         VGA_current_mode = mode;
282
283         //if (screen != NULL) gr_palette_clear();
284
285 //      ogl_init_state();
286         
287         gr_bm_data=grd_curscreen->sc_canvas.cv_bitmap.bm_data;//since we use realloc, we want to keep this pointer around.
288         memset( grd_curscreen, 0, sizeof(grs_screen));
289         grd_curscreen->sc_mode = mode;
290         grd_curscreen->sc_w = w;
291         grd_curscreen->sc_h = h;
292         grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
293         grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
294         grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
295         grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
296         grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
297         //grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
298         grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = w;
299         grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_OGL;
300         //grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
301 //      mprintf((0,"ogl/gr.c: reallocing %p to %i\n",grd_curscreen->sc_canvas.cv_bitmap.bm_data,w*h));
302         grd_curscreen->sc_canvas.cv_bitmap.bm_data = d_realloc(gr_bm_data,w*h);
303         gr_set_current_canvas(NULL);
304         //gr_enable_default_palette_loading();
305         
306         ogl_init_window(w,h);//platform specific code
307
308         ogl_get_verinfo();
309
310         OGL_VIEWPORT(0,0,w,h);
311
312         ogl_set_screen_mode();
313
314 //      gamefont_choose_game_font(w,h);
315         
316         return 0;
317 }
318
319 #define GLstrcmptestr(a,b) if (stricmp(a,#b)==0 || stricmp(a,"GL_" #b)==0)return GL_ ## b;
320 int ogl_atotexfilti(char *a,int min){
321         GLstrcmptestr(a,NEAREST);
322         GLstrcmptestr(a,LINEAR);
323         if (min){//mipmaps are valid only for the min filter
324                 GLstrcmptestr(a,NEAREST_MIPMAP_NEAREST);
325                 GLstrcmptestr(a,NEAREST_MIPMAP_LINEAR);
326                 GLstrcmptestr(a,LINEAR_MIPMAP_NEAREST);
327                 GLstrcmptestr(a,LINEAR_MIPMAP_LINEAR);
328         }
329         Error("unknown/invalid texture filter %s\n",a);
330 //      return GL_NEAREST;
331 }
332 int ogl_testneedmipmaps(int i){
333         switch (i){
334                 case GL_NEAREST:
335                 case GL_LINEAR:
336                         return 0;
337                 case GL_NEAREST_MIPMAP_NEAREST:
338                 case GL_NEAREST_MIPMAP_LINEAR:
339                 case GL_LINEAR_MIPMAP_NEAREST:
340                 case GL_LINEAR_MIPMAP_LINEAR:
341                         return 1;
342         }
343         Error("unknown texture filter %x\n",i);
344 //      return -1;
345 }
346 #ifdef OGL_RUNTIME_LOAD
347 #ifdef _WIN32
348 char *OglLibPath="opengl32.dll";
349 #endif
350 #ifdef __unix__
351 char *OglLibPath="libGL.so";
352 #endif
353
354 int ogl_rt_loaded=0;
355 int ogl_init_load_library(void)
356 {
357         int retcode=0;
358         if (!ogl_rt_loaded){
359                 int t;
360                 if ((t=FindArg("-gl_library")))
361                         OglLibPath=Args[t+1];
362
363                 retcode = OpenGL_LoadLibrary(true);
364                 if(retcode)
365                 {
366                         mprintf((0,"Opengl loaded ok\n"));
367         
368                         if(!glEnd)
369                         {
370                                 Error("Opengl: Functions not imported\n");
371                         }
372                 }else{
373                         Error("Opengl: error loading %s\n",OglLibPath);
374                 }
375                 ogl_rt_loaded=1;
376         }
377         return retcode;
378 }
379 #endif
380
381 int gr_init()
382 {
383         int mode = SM(640,480);
384         int retcode, t, glt = 0;
385
386         // Only do this function once!
387         if (gr_installed==1)
388                 return -1;
389
390
391 #ifdef OGL_RUNTIME_LOAD
392         ogl_init_load_library();
393 #endif
394
395 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
396         if (FindArg("-gl_voodoo")){
397                 ogl_voodoohack=1;
398                 gr_toggle_fullscreen();
399         }
400         if (FindArg("-fullscreen"))
401                 gr_toggle_fullscreen();
402 #endif
403         if ((glt=FindArg("-gl_alttexmerge")))
404                 ogl_alttexmerge=1;
405         if ((t=FindArg("-gl_stdtexmerge")))
406                 if (t>=glt)//allow overriding of earlier args
407                         ogl_alttexmerge=0;
408                         
409         if ((glt=FindArg("-gl_16bittextures")))
410                 ogl_rgba_format=GL_RGB5_A1;
411
412         if ((glt=FindArg("-gl_mipmap"))){
413                 GL_texmagfilt=GL_LINEAR;
414                 GL_texminfilt=GL_LINEAR_MIPMAP_NEAREST;
415         }
416         if ((glt=FindArg("-gl_trilinear")))
417         {
418                 GL_texmagfilt = GL_LINEAR;
419                 GL_texminfilt = GL_LINEAR_MIPMAP_LINEAR;
420         }
421         if ((t=FindArg("-gl_simple"))){
422                 if (t>=glt){//allow overriding of earlier args
423                         glt=t;
424                         GL_texmagfilt=GL_NEAREST;
425                         GL_texminfilt=GL_NEAREST;
426                 }
427         }
428         if ((t=FindArg("-gl_texmagfilt")) || (t=FindArg("-gl_texmagfilter"))){
429                 if (t>=glt)//allow overriding of earlier args
430                         GL_texmagfilt=ogl_atotexfilti(Args[t+1],0);
431         }
432         if ((t=FindArg("-gl_texminfilt")) || (t=FindArg("-gl_texminfilter"))){
433                 if (t>=glt)//allow overriding of earlier args
434                         GL_texminfilt=ogl_atotexfilti(Args[t+1],1);
435         }
436         GL_needmipmaps=ogl_testneedmipmaps(GL_texminfilt);
437
438         if ((t = FindArg("-gl_anisotropy")) || (t = FindArg("-gl_anisotropic")))
439         {
440                 GL_texanisofilt=atof(Args[t + 1]);
441         }
442
443         mprintf((0,"gr_init: texmagfilt:%x texminfilt:%x needmipmaps=%i anisotropic:%f\n",GL_texmagfilt,GL_texminfilt,GL_needmipmaps,GL_texanisofilt));
444
445         
446         if ((t=FindArg("-gl_vidmem"))){
447                 ogl_mem_target=atoi(Args[t+1])*1024*1024;
448         }
449         if ((t=FindArg("-gl_reticle"))){
450                 gl_reticle=atoi(Args[t+1]);
451         }
452         //printf("ogl_mem_target=%i\n",ogl_mem_target);
453         
454         ogl_init();//platform specific initialization
455
456         ogl_init_texture_list_internal();
457                 
458         MALLOC( grd_curscreen,grs_screen,1 );
459         memset( grd_curscreen, 0, sizeof(grs_screen));
460         grd_curscreen->sc_canvas.cv_bitmap.bm_data = NULL;
461
462         // Set the mode.
463         if ((retcode=gr_set_mode(mode)))
464         {
465                 return retcode;
466         }
467
468         grd_curscreen->sc_canvas.cv_color = 0;
469         grd_curscreen->sc_canvas.cv_drawmode = 0;
470         grd_curscreen->sc_canvas.cv_font = NULL;
471         grd_curscreen->sc_canvas.cv_font_fg_color = 0;
472         grd_curscreen->sc_canvas.cv_font_bg_color = 0;
473         gr_set_current_canvas( &grd_curscreen->sc_canvas );
474
475         gr_installed = 1;
476         
477         atexit(gr_close);
478
479         return 0;
480 }
481
482 void gr_close()
483 {
484 //      mprintf((0,"ogl init: %s %s %s - %s\n",glGetString(GL_VENDOR),glGetString(GL_RENDERER),glGetString(GL_VERSION),glGetString,(GL_EXTENSIONS)));
485
486         ogl_close();//platform specific code
487         if (grd_curscreen){
488                 if (grd_curscreen->sc_canvas.cv_bitmap.bm_data)
489                         d_free(grd_curscreen->sc_canvas.cv_bitmap.bm_data);
490                 d_free(grd_curscreen);
491         }
492 #ifdef OGL_RUNTIME_LOAD
493         if (ogl_rt_loaded)
494                 OpenGL_LoadLibrary(false);
495 #endif
496 }
497 extern int r_upixelc;
498 void ogl_upixelc(int x, int y, int c){
499         r_upixelc++;
500 //      printf("gr_upixelc(%i,%i,%i)%i\n",x,y,c,Function_mode==FMODE_GAME);
501 //      if(Function_mode != FMODE_GAME){
502 //              grd_curcanv->cv_bitmap.bm_data[y*grd_curscreen->sc_canvas.cv_bitmap.bm_w+x]=c;
503 //      }else{
504                 OGL_DISABLE(TEXTURE_2D);
505                 glPointSize(1.0);
506                 glBegin(GL_POINTS);
507 //              glBegin(GL_LINES);
508 //      ogl_pal=gr_current_pal;
509                 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
510 //      ogl_pal=gr_palette;
511                 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);
512 //              glVertex2f(x/((float)last_width+1),1.0-y/((float)last_height+1));
513                 glEnd();
514 //      }
515 }
516 void ogl_urect(int left,int top,int right,int bot){
517         GLfloat xo,yo,xf,yf;
518         int c=COLOR;
519         
520         xo=(left+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
521         xf = (right + 1 + grd_curcanv->cv_bitmap.bm_x) / (float)last_width;
522         yo=1.0-(top+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
523         yf = 1.0 - (bot + 1 + grd_curcanv->cv_bitmap.bm_y) / (float)last_height;
524
525         OGL_DISABLE(TEXTURE_2D);
526         glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
527         glBegin(GL_QUADS);
528         glVertex2f(xo,yo);
529         glVertex2f(xo,yf);
530         glVertex2f(xf,yf);
531         glVertex2f(xf,yo);
532         glEnd();
533 }
534 void ogl_ulinec(int left,int top,int right,int bot,int c){
535         GLfloat xo,yo,xf,yf;
536
537         xo = (left + grd_curcanv->cv_bitmap.bm_x + 0.5) / (float)last_width;
538         xf = (right + grd_curcanv->cv_bitmap.bm_x + 0.5) / (float)last_width;
539         yo = 1.0 - (top + grd_curcanv->cv_bitmap.bm_y + 0.5) / (float)last_height;
540         yf = 1.0 - (bot + grd_curcanv->cv_bitmap.bm_y + 0.5) / (float)last_height;
541
542         OGL_DISABLE(TEXTURE_2D);
543         glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
544         glBegin(GL_LINES);
545         glVertex2f(xo,yo);
546         glVertex2f(xf,yf);
547         glEnd();
548 }
549         
550
551 GLfloat last_r=0, last_g=0, last_b=0;
552 int do_pal_step=0;
553 void ogl_do_palfx(void){
554 //      GLfloat r,g,b,a;
555         OGL_DISABLE(TEXTURE_2D);
556         if (gr_palette_faded_out){
557 /*              glEnable(GL_BLEND);
558                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);*/
559                 glColor3f(0,0,0);
560 //              r=g=b=0.0;a=1.0;
561         }else{
562                 if (do_pal_step){
563                         //glBlendFunc(GL_SRC_COLOR, GL_DST_COLOR);
564                         glEnable(GL_BLEND);
565                         glBlendFunc(GL_ONE,GL_ONE);
566                         glColor3f(last_r,last_g,last_b);
567 //                      r=f2fl(last_r);g=f2fl(last_g);b=f2fl(last_b);a=0.5;
568                 }else
569                         return;
570         }
571         
572         
573         glBegin(GL_QUADS);
574         glVertex2f(0,0);
575         glVertex2f(0,1);
576         glVertex2f(1,1);
577         glVertex2f(1,0);
578         glEnd();
579         
580         glEnable(GL_BLEND);     
581         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
582 }
583
584 void gr_palette_clear()
585 {
586         gr_palette_faded_out=1;
587 }
588
589
590 int ogl_brightness_ok = 0;
591 int ogl_setgammaramp_ok = 1;
592 int ogl_brightness_r = 0, ogl_brightness_g = 0, ogl_brightness_b = 0;
593 static int old_b_r = 0, old_b_g = 0, old_b_b = 0;
594
595 void gr_palette_step_up(int r, int g, int b)
596 {
597         if (gr_palette_faded_out)
598                 return;
599
600         old_b_r = ogl_brightness_r;
601         old_b_g = ogl_brightness_g;
602         old_b_b = ogl_brightness_b;
603
604         ogl_brightness_r = max(r + gr_palette_gamma, 0);
605         ogl_brightness_g = max(g + gr_palette_gamma, 0);
606         ogl_brightness_b = max(b + gr_palette_gamma, 0);
607
608         if (ogl_setgammaramp_ok &&
609             (old_b_r != ogl_brightness_r ||
610              old_b_g != ogl_brightness_g ||
611              old_b_b != ogl_brightness_b))
612                 ogl_brightness_ok = !ogl_setbrightness_internal();
613
614         if (!ogl_setgammaramp_ok || !ogl_brightness_ok)
615         {
616                 last_r = ogl_brightness_r / 63.0;
617                 last_g = ogl_brightness_g / 63.0;
618                 last_b = ogl_brightness_b / 63.0;
619
620                 do_pal_step = (r || g || b || gr_palette_gamma);
621         }
622         else
623         {
624                 do_pal_step = 0;
625         }
626 }
627
628 //added on 980913 by adb to fix palette problems
629 // need a min without side effects...
630 #undef min
631 static inline int min(int x, int y) { return x < y ? x : y; }
632 //end changes by adb
633
634 void gr_palette_load( ubyte *pal )      
635 {
636  int i;//, j;
637
638  for (i=0; i<768; i++ ) {
639      gr_current_pal[i] = pal[i];
640      if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
641  }
642  //palette = screen->format->palette;
643
644  gr_palette_faded_out=0;
645
646         gr_palette_step_up(0, 0, 0); // make ogl_setbrightness_internal get run so that menus get brightened too.
647
648  init_computed_colors();
649 }
650
651
652
653 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
654 {
655         gr_palette_faded_out=1;
656         return 0;
657 }
658
659
660
661 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
662 {
663         gr_palette_faded_out=0;
664         return 0;
665 }
666
667
668
669 void gr_palette_read(ubyte * pal)
670 {
671         int i;
672         for (i=0; i<768; i++ ) {
673                 pal[i]=gr_current_pal[i];
674                 if (pal[i] > 63) pal[i] = 63;
675         }
676 }
677
678 //writes out an uncompressed RGB .tga file
679 //if we got really spiffy, we could optionally link in libpng or something, and use that.
680 void write_bmp(char *savename,int w,int h,unsigned char *buf){
681         int f;
682 #ifdef _WIN32
683         f=open(savename,O_CREAT|O_EXCL|O_WRONLY,S_IREAD|S_IWRITE);
684 #else
685         f=open(savename,O_CREAT|O_EXCL|O_WRONLY,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
686 #endif
687         if (f>=0){
688                 GLubyte    targaMagic[12] = { 0, //no identification field
689                          0,//no colormap
690                          2,//RGB image (well, BGR, actually)
691                          0, 0, 0, 0, 0, 0, 0, 0, 0 };//no colormap or image origin stuff.
692                 GLubyte blah;
693                 int r;
694                 GLubyte *s;
695                 int x,y;
696                 
697                 //write .TGA header.
698                 write (f,targaMagic,sizeof(targaMagic));
699                 blah=w%256;write (f,&blah,1);//w, low
700                 blah=w/256;write (f,&blah,1);//w, high
701                 blah=h%256;write (f,&blah,1);//h, low
702                 blah=h/256;write (f,&blah,1);//h, high
703                 blah=24;write (f,&blah,1);//24 bpp
704                 blah=0;write (f,&blah,1);//no attribute bits, origin is lowerleft, no interleave
705                 
706                 s=buf;
707                 for (y=0;y<h;y++){//TGAs use BGR ordering of data.
708                         for (x=0;x<w;x++){
709                                 blah=s[0];
710                                 s[0]=s[2];
711                                 s[2]=blah;
712                                 s+=3;                           
713                         }
714                 }
715                 x=0;y=w*h*3;
716                 while (x<y){
717                         r=write(f,buf+x,y);
718                         if (r<=0){
719                                 mprintf((0,"screenshot error, couldn't write to %s (err %i)\n",savename,errno));
720                                 break;
721                         }
722                         x+=r;y-=r;
723                 }
724                 close(f);
725         }else{
726                 mprintf((0,"screenshot error, couldn't open %s (err %i)\n",savename,errno));
727         }
728 }
729 void save_screen_shot(int automap_flag)
730 {
731 //      fix t1;
732         char message[100];
733         static int savenum=0;
734         char savename[13];
735         unsigned char *buf;
736         
737         if (!ogl_readpixels_ok){
738                 if (!automap_flag)
739                         hud_message(MSGC_GAME_FEEDBACK,"glReadPixels not supported on your configuration");
740                 return;
741         }
742
743         stop_time();
744
745 //added/changed on 10/31/98 by Victor Rachels to fix overwrite each new game
746         if ( savenum == 9999 ) savenum = 0;
747         sprintf(savename,"scrn%04d.tga",savenum++);
748
749         while(!access(savename,0))
750         {
751                 if ( savenum == 9999 ) savenum = 0;
752                 sprintf(savename,"scrn%04d.tga",savenum++);
753         }
754         sprintf( message, "%s '%s'", TXT_DUMPING_SCREEN, savename );
755 //end this section addition/change - Victor Rachels
756
757         if (automap_flag) {
758 //      save_font = grd_curcanv->cv_font;
759 //      gr_set_curfont(GAME_FONT);
760 //      gr_set_fontcolor(gr_find_closest_color_current(0,31,0),-1);
761 //      gr_get_string_size(message,&w,&h,&aw);
762 //              modex_print_message(32, 2, message);
763         } else {
764                 hud_message(MSGC_GAME_FEEDBACK,message);
765         }
766         
767         buf = d_malloc(grd_curscreen->sc_w*grd_curscreen->sc_h*3);
768         glReadBuffer(GL_FRONT);
769         glReadPixels(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
770         write_bmp(savename,grd_curscreen->sc_w,grd_curscreen->sc_h,buf);
771         d_free(buf);
772
773         key_flush();
774         start_time();
775 }