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