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