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