oops, can't get rid of main INCLUDE yet...
[btb/d2x.git] / arch / ogl / gr.c
1 /* $Id: gr.c,v 1.11 2003-03-15 12:07:43 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 #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
351         // Only do this function once!
352         if (gr_installed==1)
353                 return -1;
354
355
356 #ifdef OGL_RUNTIME_LOAD
357         ogl_init_load_library();
358 #endif
359
360 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
361         if (FindArg("-gl_voodoo")){
362                 ogl_voodoohack=1;
363                 gr_toggle_fullscreen();
364         }
365         if (FindArg("-fullscreen"))
366                 gr_toggle_fullscreen();
367 #endif
368         if ((glt=FindArg("-gl_alttexmerge")))
369                 ogl_alttexmerge=1;
370         if ((t=FindArg("-gl_stdtexmerge")))
371                 if (t>=glt)//allow overriding of earlier args
372                         ogl_alttexmerge=0;
373                         
374         if ((glt=FindArg("-gl_16bittextures")))
375                 ogl_rgba_format=GL_RGB5_A1;
376
377         if ((glt=FindArg("-gl_mipmap"))){
378                 GL_texmagfilt=GL_LINEAR;
379                 GL_texminfilt=GL_LINEAR_MIPMAP_NEAREST;
380         }
381         if ((t=FindArg("-gl_simple"))){
382                 if (t>=glt){//allow overriding of earlier args
383                         glt=t;
384                         GL_texmagfilt=GL_NEAREST;
385                         GL_texminfilt=GL_NEAREST;
386                 }
387         }
388         if ((t=FindArg("-gl_texmagfilt")) || (t=FindArg("-gl_texmagfilter"))){
389                 if (t>=glt)//allow overriding of earlier args
390                         GL_texmagfilt=ogl_atotexfilti(Args[t+1],0);
391         }
392         if ((t=FindArg("-gl_texminfilt")) || (t=FindArg("-gl_texminfilter"))){
393                 if (t>=glt)//allow overriding of earlier args
394                         GL_texminfilt=ogl_atotexfilti(Args[t+1],1);
395         }
396         GL_needmipmaps=ogl_testneedmipmaps(GL_texminfilt);
397         mprintf((0,"gr_init: texmagfilt:%x texminfilt:%x needmipmaps=%i\n",GL_texmagfilt,GL_texminfilt,GL_needmipmaps));
398         
399         if ((t=FindArg("-gl_vidmem"))){
400                 ogl_mem_target=atoi(Args[t+1])*1024*1024;
401         }
402         if ((t=FindArg("-gl_reticle"))){
403                 gl_reticle=atoi(Args[t+1]);
404         }
405         //printf("ogl_mem_target=%i\n",ogl_mem_target);
406         
407         ogl_init();//platform specific initialization
408
409         ogl_init_texture_list_internal();
410                 
411         MALLOC( grd_curscreen,grs_screen,1 );
412         memset( grd_curscreen, 0, sizeof(grs_screen));
413         grd_curscreen->sc_canvas.cv_bitmap.bm_data = NULL;
414
415         // Set the mode.
416         if ((retcode=gr_set_mode(mode)))
417         {
418                 return retcode;
419         }
420
421         grd_curscreen->sc_canvas.cv_color = 0;
422         grd_curscreen->sc_canvas.cv_drawmode = 0;
423         grd_curscreen->sc_canvas.cv_font = NULL;
424         grd_curscreen->sc_canvas.cv_font_fg_color = 0;
425         grd_curscreen->sc_canvas.cv_font_bg_color = 0;
426         gr_set_current_canvas( &grd_curscreen->sc_canvas );
427
428         gr_installed = 1;
429         
430         atexit(gr_close);
431
432         return 0;
433 }
434
435 void gr_close()
436 {
437 //      mprintf((0,"ogl init: %s %s %s - %s\n",glGetString(GL_VENDOR),glGetString(GL_RENDERER),glGetString(GL_VERSION),glGetString,(GL_EXTENSIONS)));
438
439         ogl_close();//platform specific code
440         if (grd_curscreen){
441                 if (grd_curscreen->sc_canvas.cv_bitmap.bm_data)
442                         d_free(grd_curscreen->sc_canvas.cv_bitmap.bm_data);
443                 d_free(grd_curscreen);
444         }
445 #ifdef OGL_RUNTIME_LOAD
446         if (ogl_rt_loaded)
447                 OpenGL_LoadLibrary(false);
448 #endif
449 }
450 extern int r_upixelc;
451 void ogl_upixelc(int x, int y, int c){
452         r_upixelc++;
453 //      printf("gr_upixelc(%i,%i,%i)%i\n",x,y,c,Function_mode==FMODE_GAME);
454 //      if(Function_mode != FMODE_GAME){
455 //              grd_curcanv->cv_bitmap.bm_data[y*grd_curscreen->sc_canvas.cv_bitmap.bm_w+x]=c;
456 //      }else{
457                 OGL_DISABLE(TEXTURE_2D);
458                 glPointSize(1.0);
459                 glBegin(GL_POINTS);
460 //              glBegin(GL_LINES);
461 //      ogl_pal=gr_current_pal;
462                 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
463 //      ogl_pal=gr_palette;
464                 glVertex2f((x+grd_curcanv->cv_bitmap.bm_x)/(float)last_width,1.0-(y+grd_curcanv->cv_bitmap.bm_y)/(float)last_height);
465 //              glVertex2f(x/((float)last_width+1),1.0-y/((float)last_height+1));
466                 glEnd();
467 //      }
468 }
469 void ogl_urect(int left,int top,int right,int bot){
470         GLfloat xo,yo,xf,yf;
471         int c=COLOR;
472         
473         xo=(left+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
474         xf=(right+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
475         yo=1.0-(top+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
476         yf=1.0-(bot+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
477         
478         OGL_DISABLE(TEXTURE_2D);
479         glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
480         glBegin(GL_QUADS);
481         glVertex2f(xo,yo);
482         glVertex2f(xo,yf);
483         glVertex2f(xf,yf);
484         glVertex2f(xf,yo);
485         glEnd();
486 }
487 void ogl_ulinec(int left,int top,int right,int bot,int c){
488         GLfloat xo,yo,xf,yf;
489         
490         xo=(left+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
491         xf=(right+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
492         yo=1.0-(top+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
493         yf=1.0-(bot+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
494         
495         OGL_DISABLE(TEXTURE_2D);
496         glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
497         glBegin(GL_LINES);
498         glVertex2f(xo,yo);
499         glVertex2f(xf,yf);
500         glEnd();
501 }
502         
503
504 GLfloat last_r=0, last_g=0, last_b=0;
505 int do_pal_step=0;
506 void ogl_do_palfx(void){
507 //      GLfloat r,g,b,a;
508         OGL_DISABLE(TEXTURE_2D);
509         if (gr_palette_faded_out){
510 /*              glEnable(GL_BLEND);
511                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);*/
512                 glColor3f(0,0,0);
513 //              r=g=b=0.0;a=1.0;
514         }else{
515                 if (do_pal_step){
516                         //glBlendFunc(GL_SRC_COLOR, GL_DST_COLOR);
517                         glEnable(GL_BLEND);
518                         glBlendFunc(GL_ONE,GL_ONE);
519                         glColor3f(last_r,last_g,last_b);
520 //                      r=f2fl(last_r);g=f2fl(last_g);b=f2fl(last_b);a=0.5;
521                 }else
522                         return;
523         }
524         
525         
526         glBegin(GL_QUADS);
527         glVertex2f(0,0);
528         glVertex2f(0,1);
529         glVertex2f(1,1);
530         glVertex2f(1,0);
531         glEnd();
532         
533         glEnable(GL_BLEND);     
534         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
535 }
536
537 void gr_palette_clear()
538 {
539         gr_palette_faded_out=1;
540 }
541
542
543 void gr_palette_step_up( int r, int g, int b )
544 {
545         if (gr_palette_faded_out) return;
546
547 //      if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
548
549 /*      last_r = r/63.0;
550         last_g = g/63.0;
551         last_b = b/63.0;
552         do_pal_step=(r || g || b);*/
553         
554         last_r = (r+gr_palette_gamma)/63.0;
555         last_g = (g+gr_palette_gamma)/63.0;
556         last_b = (b+gr_palette_gamma)/63.0;
557
558         do_pal_step=(r || g || b || gr_palette_gamma);
559         
560 }
561
562 //added on 980913 by adb to fix palette problems
563 // need a min without side effects...
564 #undef min
565 static inline int min(int x, int y) { return x < y ? x : y; }
566 //end changes by adb
567
568 void gr_palette_load( ubyte *pal )      
569 {
570  int i;//, j;
571
572  for (i=0; i<768; i++ ) {
573      gr_current_pal[i] = pal[i];
574      if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
575  }
576  //palette = screen->format->palette;
577
578  gr_palette_faded_out=0;
579
580  init_computed_colors();
581 }
582
583
584
585 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
586 {
587         gr_palette_faded_out=1;
588         return 0;
589 }
590
591
592
593 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
594 {
595         gr_palette_faded_out=0;
596         return 0;
597 }
598
599
600
601 void gr_palette_read(ubyte * pal)
602 {
603         int i;
604         for (i=0; i<768; i++ ) {
605                 pal[i]=gr_current_pal[i];
606                 if (pal[i] > 63) pal[i] = 63;
607         }
608 }
609
610 //writes out an uncompressed RGB .tga file
611 //if we got really spiffy, we could optionally link in libpng or something, and use that.
612 void write_bmp(char *savename,int w,int h,unsigned char *buf){
613         int f;
614 #if defined(__WINDOWS__) || defined(__MINGW32__)
615         f=open(savename,O_CREAT|O_EXCL|O_WRONLY,S_IRUSR|S_IWUSR);
616 #else
617         f=open(savename,O_CREAT|O_EXCL|O_WRONLY,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
618 #endif
619         if (f>=0){
620                 GLubyte    targaMagic[12] = { 0, //no identification field
621                          0,//no colormap
622                          2,//RGB image (well, BGR, actually)
623                          0, 0, 0, 0, 0, 0, 0, 0, 0 };//no colormap or image origin stuff.
624                 GLubyte blah;
625                 int r;
626                 GLubyte *s;
627                 int x,y;
628                 
629                 //write .TGA header.
630                 write (f,targaMagic,sizeof(targaMagic));
631                 blah=w%256;write (f,&blah,1);//w, low
632                 blah=w/256;write (f,&blah,1);//w, high
633                 blah=h%256;write (f,&blah,1);//h, low
634                 blah=h/256;write (f,&blah,1);//h, high
635                 blah=24;write (f,&blah,1);//24 bpp
636                 blah=0;write (f,&blah,1);//no attribute bits, origin is lowerleft, no interleave
637                 
638                 s=buf;
639                 for (y=0;y<h;y++){//TGAs use BGR ordering of data.
640                         for (x=0;x<w;x++){
641                                 blah=s[0];
642                                 s[0]=s[2];
643                                 s[2]=blah;
644                                 s+=3;                           
645                         }
646                 }
647                 x=0;y=w*h*3;
648                 while (x<y){
649                         r=write(f,buf+x,y);
650                         if (r<=0){
651                                 mprintf((0,"screenshot error, couldn't write to %s (err %i)\n",savename,errno));
652                                 break;
653                         }
654                         x+=r;y-=r;
655                 }
656                 close(f);
657         }else{
658                 mprintf((0,"screenshot error, couldn't open %s (err %i)\n",savename,errno));
659         }
660 }
661 void save_screen_shot(int automap_flag)
662 {
663 //      fix t1;
664         char message[100];
665         static int savenum=0;
666         char savename[13];
667         unsigned char *buf;
668         
669         if (!ogl_readpixels_ok){
670                 if (!automap_flag)
671                         hud_message(MSGC_GAME_FEEDBACK,"glReadPixels not supported on your configuration");
672                 return;
673         }
674
675         stop_time();
676
677 //added/changed on 10/31/98 by Victor Rachels to fix overwrite each new game
678         if ( savenum == 9999 ) savenum = 0;
679         sprintf(savename,"scrn%04d.tga",savenum++);
680
681         while(!access(savename,0))
682         {
683                 if ( savenum == 9999 ) savenum = 0;
684                 sprintf(savename,"scrn%04d.tga",savenum++);
685         }
686         sprintf( message, "%s '%s'", TXT_DUMPING_SCREEN, savename );
687 //end this section addition/change - Victor Rachels
688
689         if (automap_flag) {
690 //      save_font = grd_curcanv->cv_font;
691 //      gr_set_curfont(GAME_FONT);
692 //      gr_set_fontcolor(gr_find_closest_color_current(0,31,0),-1);
693 //      gr_get_string_size(message,&w,&h,&aw);
694 //              modex_print_message(32, 2, message);
695         } else {
696                 hud_message(MSGC_GAME_FEEDBACK,message);
697         }
698         
699         buf = d_malloc(grd_curscreen->sc_w*grd_curscreen->sc_h*3);
700         glReadBuffer(GL_FRONT);
701         glReadPixels(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
702         write_bmp(savename,grd_curscreen->sc_w,grd_curscreen->sc_h,buf);
703         d_free(buf);
704
705         key_flush();
706         start_time();
707 }