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