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