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