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