]> icculus.org git repositories - btb/d2x.git/blob - arch/ogl/vid.c
more header cleanup
[btb/d2x.git] / arch / ogl / vid.c
1 /*
2  *
3  * OGL video functions. - Added 9/15/99 Matthew Mueller
4  *
5  *
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include <conf.h>
10 #endif
11
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #ifdef _MSC_VER
16 #include <windows.h>
17 #endif
18
19 #if !defined(_MSC_VER) && !defined(macintosh)
20 #include <unistd.h>
21 #endif
22 #if !defined(macintosh)
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #endif
27
28 #include <errno.h>
29
30 #include "inferno.h"
31 #include "gr.h"
32 #include "vid.h"
33 #include "u_mem.h"
34 #include "error.h"
35 #include "timer.h"
36 #include "strutil.h"
37 #include "mono.h"
38 #include "args.h"
39 #include "key.h"
40 #include "physfsx.h"
41
42 #define DECLARE_VARS
43 #include "internal.h"
44 #if defined(__APPLE__) && defined(__MACH__)
45 #include <OpenGL/glu.h>
46 #undef GL_ARB_multitexture // hack!
47 #else
48 #include <GL/glu.h>
49 #endif
50
51
52 int ogl_voodoohack=0;
53
54 int vid_installed = 0;
55
56
57 void gr_palette_clear(); // Function prototype for gr_init;
58 int gl_initialized=0;
59 int gl_reticle = 0;
60
61 int ogl_fullscreen=0;
62
63
64 int vid_check_fullscreen(void)
65 {
66         return ogl_fullscreen;
67 }
68
69
70 static void do_fullscreen(int f)
71 {
72         if (ogl_voodoohack)
73                 ogl_fullscreen=1;//force fullscreen mode on voodoos.
74         else
75                 ogl_fullscreen=f;
76         if (gl_initialized){
77                 ogl_do_fullscreen_internal();
78         }
79 }
80
81
82 int vid_toggle_fullscreen(void)
83 {
84         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
90 int vid_toggle_fullscreen_menu(void)
91 {
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         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 vid_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         long t, sgi_max_textures = -1;
166         GLint arb_max_textures = -1;
167 #ifdef GL_NV_register_combiners
168         long nv_register_combiners = -1;
169 #endif
170         float anisotropic_max = 0;
171
172         gl_vendor = (const char *) glGetString (GL_VENDOR);
173         gl_renderer = (const char *) glGetString (GL_RENDERER);
174         gl_version = (const char *) glGetString (GL_VERSION);
175         gl_extensions = (const char *) glGetString (GL_EXTENSIONS);
176
177         con_printf(CON_VERBOSE, "OpenGL: vendor: %s\nOpenGL: renderer: %s\nOpenGL: version: %s\n",gl_vendor,gl_renderer,gl_version);
178
179         ogl_intensity4_ok = 1;
180         ogl_luminance4_alpha4_ok = 1;
181         ogl_rgba2_ok = 1;
182         ogl_gettexlevelparam_ok = 1;
183         ogl_setgammaramp_ok = 1;
184
185 #ifdef WGL_VIDEO
186         dglMultiTexCoord2fARB = (glMultiTexCoord2fARB_fp)wglGetProcAddress("glMultiTexCoord2fARB");
187         dglActiveTextureARB = (glActiveTextureARB_fp)wglGetProcAddress("glActiveTextureARB");
188         dglMultiTexCoord2fSGIS = (glMultiTexCoord2fSGIS_fp)wglGetProcAddress("glMultiTexCoord2fSGIS");
189         dglSelectTextureSGIS = (glSelectTextureSGIS_fp)wglGetProcAddress("glSelectTextureSGIS");
190         dglColorTableEXT = (glColorTableEXT_fp)wglGetProcAddress("glColorTableEXT");
191         dglCombinerParameteriNV = (glCombinerParameteriNV_fp)wglGetProcAddress("glCombinerParameteriNV");
192         dglCombinerInputNV = (glCombinerInputNV_fp)wglGetProcAddress("glCombinerInputNV");
193         dglCombinerOutputNV = (glCombinerOutputNV_fp)wglGetProcAddress("glCombinerOutputNV");
194         dglFinalCombinerInputNV = (glFinalCombinerInputNV_fp)wglGetProcAddress("glFinalCombinerInputNV");
195 #endif
196
197 #ifdef GL_ARB_multitexture
198         ogl_arb_multitexture_ok = (strstr(gl_extensions, "GL_ARB_multitexture") != 0 && glActiveTextureARB != 0);
199         mprintf((0,"c:%p d:%p e:%p\n",strstr(gl_extensions,"GL_ARB_multitexture"),glActiveTextureARB,glBegin));
200 #endif
201 #ifdef GL_SGIS_multitexture
202         ogl_sgis_multitexture_ok = (strstr(gl_extensions, "GL_SGIS_multitexture") != 0 && glSelectTextureSGIS != 0);
203         mprintf((0,"a:%p b:%p\n",strstr(gl_extensions,"GL_SGIS_multitexture"),glSelectTextureSGIS));
204 #endif
205         ogl_nv_texture_env_combine4_ok = (strstr(gl_extensions, "GL_NV_texture_env_combine4") != 0);
206 #ifdef GL_NV_register_combiners
207         ogl_nv_register_combiners_ok=(strstr(gl_extensions,"GL_NV_register_combiners")!=0 && glCombinerOutputNV!=0);
208 #endif
209
210         ogl_ext_texture_filter_anisotropic_ok = (strstr(gl_extensions, "GL_EXT_texture_filter_anisotropic") != 0);
211         if (ogl_ext_texture_filter_anisotropic_ok)
212                 glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &anisotropic_max);
213
214 #ifdef GL_EXT_paletted_texture
215         ogl_paletted_texture_ok = (strstr(gl_extensions, "GL_EXT_paletted_texture") != 0 && glColorTableEXT != 0);
216         ogl_shared_palette_ok = (strstr(gl_extensions, "GL_EXT_shared_texture_palette") != 0 && ogl_paletted_texture_ok);
217 #endif
218         //add driver specific hacks here.  whee.
219         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){
220                 ogl_intensity4_ok=0;//ignores alpha, always black background instead of transparent.
221                 ogl_readpixels_ok=0;//either just returns all black, or kills the X server entirely
222                 ogl_gettexlevelparam_ok=0;//returns random data..
223         }
224         if (stricmp(gl_vendor,"Matrox Graphics Inc.")==0){
225                 //displays garbage. reported by
226                 //  redomen@crcwnet.com (render="Matrox G400" version="1.1.3 5.52.015")
227                 //  orulz (Matrox G200)
228                 ogl_intensity4_ok=0;
229         }
230 #ifdef macintosh
231         if (stricmp(gl_renderer,"3dfx Voodoo 3")==0){ // strangely, includes Voodoo 2
232                 ogl_gettexlevelparam_ok=0; // Always returns 0
233         }
234 #endif
235
236         //allow overriding of stuff.
237 #ifdef GL_ARB_multitexture
238         if ((t=FindArg("-gl_arb_multitexture_ok"))){
239                 ogl_arb_multitexture_ok=atoi(Args[t+1]);
240         }
241         if (ogl_arb_multitexture_ok)
242                 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &arb_max_textures);
243 #endif
244 #ifdef GL_SGIS_multitexture
245         if ((t=FindArg("-gl_sgis_multitexture_ok"))){
246                 ogl_sgis_multitexture_ok=atoi(Args[t+1]);
247         }
248         if (ogl_sgis_multitexture_ok)
249                 glGetIntegerv(GL_MAX_TEXTURES_SGIS, &sgi_max_textures);
250 #endif
251 #ifdef GL_NV_register_combiners
252         if ((t = FindArg("-gl_nv_register_combiners_ok")))
253         {
254                 ogl_nv_register_combiners_ok=atoi(Args[t + 1]);
255         }
256         if (ogl_nv_register_combiners_ok)
257                 glGetIntegerv(GL_MAX_GENERAL_COMBINERS_NV, &nv_register_combiners);
258 #endif
259 #ifdef GL_EXT_paletted_texture
260         if ((t = FindArg("-gl_paletted_texture_ok")))
261         {
262                 ogl_paletted_texture_ok = atoi(Args[t + 1]);
263         }
264         if ((t = FindArg("-gl_shared_palette_ok")))
265         {
266                 ogl_shared_palette_ok = atoi(Args[t + 1]);
267         }
268         ogl_shared_palette_ok = ogl_shared_palette_ok && ogl_paletted_texture_ok; // shared palettes require palette support in the first place, obviously ;)
269         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);
270 #endif
271         if ((t=FindArg("-gl_intensity4_ok"))){
272                 ogl_intensity4_ok=atoi(Args[t+1]);
273         }
274         if ((t=FindArg("-gl_luminance4_alpha4_ok"))){
275                 ogl_luminance4_alpha4_ok=atoi(Args[t+1]);
276         }
277         if ((t=FindArg("-gl_rgba2_ok"))){
278                 ogl_rgba2_ok=atoi(Args[t+1]);
279         }
280         if ((t=FindArg("-gl_readpixels_ok"))){
281                 ogl_readpixels_ok=atoi(Args[t+1]);
282         }
283         if ((t=FindArg("-gl_gettexlevelparam_ok"))){
284                 ogl_gettexlevelparam_ok=atoi(Args[t+1]);
285         }
286         if ((t=FindArg("-gl_setgammaramp_ok")))
287         {
288                 ogl_setgammaramp_ok = atoi(Args[t + 1]);
289         }
290
291         con_printf(CON_DEBUG, "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);
292 #ifdef GL_NV_register_combiners
293         con_printf(CON_DEBUG, "gl_nv_register_combiners:%i(%i stages)\n", ogl_nv_register_combiners_ok, nv_register_combiners);
294 #endif
295         con_printf(CON_DEBUG, "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);
296 }
297
298
299 int vid_check_mode(uint32_t mode)
300 {
301         int w, h;
302
303         w = SM_W(mode);
304         h = SM_H(mode);
305         return ogl_check_mode(w, h); // platform specific code
306 }
307
308
309 uint32_t Vid_current_mode;
310
311
312 int vid_set_mode(uint32_t mode)
313 {
314         short w, h;
315
316 #ifdef NOGRAPH
317 return 0;
318 #endif
319 //      mode=0;
320         if (mode<=0)
321                 return 0;
322
323         if (mode == Vid_current_mode)
324                 return 0;
325
326         w=SM_W(mode);
327         h=SM_H(mode);
328         Vid_current_mode = mode;
329
330         //if (screen != NULL) gr_palette_clear();
331
332 //      ogl_init_state();
333
334         gr_init_screen(BM_OGL, w, h, 0, 0, w, NULL);
335
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
379
380 void ogl_cmd_texturemode(int argc, char **argv)
381 {
382         if (argc < 2)
383                 return;
384
385         if (!stricmp(argv[1], "GL_NEAREST")) {
386                 GL_texminfilt = GL_NEAREST;
387                 GL_texmagfilt = GL_NEAREST;
388         } else if (!stricmp(argv[1], "GL_LINEAR")) {
389                 GL_texminfilt = GL_LINEAR;
390                 GL_texmagfilt = GL_LINEAR;
391         } else if (!stricmp(argv[1], "GL_NEAREST_MIPMAP_NEAREST")) {
392                 GL_texminfilt = GL_NEAREST_MIPMAP_NEAREST;
393                 GL_texmagfilt = GL_NEAREST;
394         } else if (!stricmp(argv[1], "GL_LINEAR_MIPMAP_NEAREST")) {
395                 GL_texminfilt = GL_LINEAR_MIPMAP_NEAREST;
396                 GL_texmagfilt = GL_LINEAR;
397         } else if (!stricmp(argv[1], "GL_NEAREST_MIPMAP_LINEAR")) {
398                 GL_texminfilt = GL_NEAREST_MIPMAP_LINEAR;
399                 GL_texmagfilt = GL_NEAREST;
400         } else if (!stricmp(argv[1], "GL_LINEAR_MIPMAP_LINEAR")) {
401                 GL_texminfilt = GL_LINEAR_MIPMAP_LINEAR;
402                 GL_texmagfilt = GL_LINEAR;
403         } else
404                 return;
405
406         GL_needmipmaps=ogl_testneedmipmaps(GL_texminfilt);
407
408         ogl_smash_texture_list_internal();
409 }
410
411
412 #ifdef OGL_RUNTIME_LOAD
413 #ifdef _WIN32
414 char *OglLibPath="opengl32.dll";
415 #endif
416 #ifdef __unix__
417 char *OglLibPath="libGL.so";
418 #endif
419 #ifdef macintosh
420 char *OglLibPath = NULL;
421 #endif
422
423 int ogl_rt_loaded=0;
424 int ogl_init_load_library(void)
425 {
426         int retcode=0;
427         if (!ogl_rt_loaded){
428                 int t;
429                 if ((t=FindArg("-gl_library")))
430                         OglLibPath=Args[t+1];
431
432                 retcode = OpenGL_LoadLibrary(true);
433                 if(retcode)
434                 {
435                         mprintf((0,"Opengl loaded ok\n"));
436         
437                         if(!glEnd)
438                         {
439                                 Error("Opengl: Functions not imported\n");
440                         }
441                 }else{
442                         Error("Opengl: error loading %s\n", OglLibPath? OglLibPath : SDL_GetError());
443                 }
444                 ogl_rt_loaded=1;
445         }
446         return retcode;
447 }
448 #endif
449
450
451 int vid_init(void)
452 {
453         int t, glt = 0;
454
455         // Only do this function once!
456         if (vid_installed == 1)
457                 return -1;
458
459
460 #ifdef OGL_RUNTIME_LOAD
461         ogl_init_load_library();
462 #endif
463
464 #ifdef VID_SUPPORTS_FULLSCREEN_TOGGLE
465         if (FindArg("-gl_voodoo")){
466                 ogl_voodoohack=1;
467                 vid_toggle_fullscreen();
468         }
469         if (FindArg("-fullscreen"))
470                 vid_toggle_fullscreen();
471 #endif
472         if ((glt=FindArg("-gl_alttexmerge")))
473                 ogl_alttexmerge=1;
474         if ((t=FindArg("-gl_stdtexmerge")))
475                 if (t>=glt)//allow overriding of earlier args
476                         ogl_alttexmerge=0;
477
478         if ((glt = FindArg("-gl_16bittextures")))
479         {
480                 ogl_rgba_internalformat = GL_RGB5_A1;
481                 ogl_rgb_internalformat = GL_RGB5;
482         }
483
484         if ((glt=FindArg("-gl_mipmap"))){
485                 GL_texmagfilt=GL_LINEAR;
486                 GL_texminfilt=GL_LINEAR_MIPMAP_NEAREST;
487         }
488         if ((glt=FindArg("-gl_trilinear")))
489         {
490                 GL_texmagfilt = GL_LINEAR;
491                 GL_texminfilt = GL_LINEAR_MIPMAP_LINEAR;
492         }
493         if ((t=FindArg("-gl_simple"))){
494                 if (t>=glt){//allow overriding of earlier args
495                         glt=t;
496                         GL_texmagfilt=GL_NEAREST;
497                         GL_texminfilt=GL_NEAREST;
498                 }
499         }
500         if ((t=FindArg("-gl_texmagfilt")) || (t=FindArg("-gl_texmagfilter"))){
501                 if (t>=glt)//allow overriding of earlier args
502                         GL_texmagfilt=ogl_atotexfilti(Args[t+1],0);
503         }
504         if ((t=FindArg("-gl_texminfilt")) || (t=FindArg("-gl_texminfilter"))){
505                 if (t>=glt)//allow overriding of earlier args
506                         GL_texminfilt=ogl_atotexfilti(Args[t+1],1);
507         }
508         GL_needmipmaps=ogl_testneedmipmaps(GL_texminfilt);
509
510         if ((t = FindArg("-gl_anisotropy")) || (t = FindArg("-gl_anisotropic")))
511         {
512                 GL_texanisofilt=atof(Args[t + 1]);
513         }
514
515         mprintf((0,"gr_init: texmagfilt:%x texminfilt:%x needmipmaps=%i anisotropic:%f\n",GL_texmagfilt,GL_texminfilt,GL_needmipmaps,GL_texanisofilt));
516
517         
518         if ((t=FindArg("-gl_vidmem"))){
519                 ogl_mem_target=atoi(Args[t+1])*1024*1024;
520         }
521         if ((t=FindArg("-gl_reticle"))){
522                 gl_reticle=atoi(Args[t+1]);
523         }
524         //printf("ogl_mem_target=%i\n",ogl_mem_target);
525         
526         ogl_init();//platform specific initialization
527
528         ogl_init_texture_list_internal();
529                 
530         cmd_addcommand("gl_texturemode", ogl_cmd_texturemode);
531
532         vid_installed = 1;
533         
534         atexit(vid_close);
535
536         return 0;
537 }
538
539
540 void vid_close(void)
541 {
542 //      mprintf((0,"ogl init: %s %s %s - %s\n",glGetString(GL_VENDOR),glGetString(GL_RENDERER),glGetString(GL_VERSION),glGetString,(GL_EXTENSIONS)));
543         ogl_brightness_r = ogl_brightness_g = ogl_brightness_b = 0;
544         ogl_setbrightness_internal();
545
546         ogl_close();//platform specific code
547
548 #ifdef OGL_RUNTIME_LOAD
549         if (ogl_rt_loaded)
550                 OpenGL_LoadLibrary(false);
551 #endif
552 }
553 extern int r_upixelc;
554 void ogl_upixelc(int x, int y, int c){
555         r_upixelc++;
556 //      printf("gr_upixelc(%i,%i,%i)%i\n",x,y,c,Function_mode==FMODE_GAME);
557 //      if(Function_mode != FMODE_GAME){
558 //              grd_curcanv->cv_bitmap.bm_data[y*grd_curscreen->sc_canvas.cv_bitmap.bm_w+x]=c;
559 //      }else{
560                 OGL_DISABLE(TEXTURE_2D);
561                 glPointSize(1.0);
562                 glBegin(GL_POINTS);
563 //              glBegin(GL_LINES);
564 //      ogl_pal=gr_current_pal;
565                 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
566 //      ogl_pal=gr_palette;
567                 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);
568 //              glVertex2f(x/((float)last_width+1),1.0-y/((float)last_height+1));
569                 glEnd();
570 //      }
571 }
572 void ogl_urect(int left,int top,int right,int bot){
573         GLfloat xo,yo,xf,yf;
574         int c=COLOR;
575         
576         xo=(left+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
577         xf = (right + 1 + grd_curcanv->cv_bitmap.bm_x) / (float)last_width;
578         yo=1.0-(top+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
579         yf = 1.0 - (bot + 1 + grd_curcanv->cv_bitmap.bm_y) / (float)last_height;
580
581         OGL_DISABLE(TEXTURE_2D);
582         if (Gr_scanline_darkening_level >= GR_FADE_LEVELS)
583                 glColor3f(CPAL2Tr(c), CPAL2Tg(c), CPAL2Tb(c));
584         else
585                 glColor4f(CPAL2Tr(c), CPAL2Tg(c), CPAL2Tb(c), 1.0 - (float)Gr_scanline_darkening_level / ((float)GR_FADE_LEVELS - 1.0));
586         glBegin(GL_QUADS);
587         glVertex2f(xo,yo);
588         glVertex2f(xo,yf);
589         glVertex2f(xf,yf);
590         glVertex2f(xf,yo);
591         glEnd();
592 }
593 void ogl_ulinec(int left,int top,int right,int bot,int c){
594         GLfloat xo,yo,xf,yf;
595
596         xo = (left + grd_curcanv->cv_bitmap.bm_x + 0.5) / (float)last_width;
597         xf = (right + grd_curcanv->cv_bitmap.bm_x + 0.5) / (float)last_width;
598         yo = 1.0 - (top + grd_curcanv->cv_bitmap.bm_y + 0.5) / (float)last_height;
599         yf = 1.0 - (bot + grd_curcanv->cv_bitmap.bm_y + 0.5) / (float)last_height;
600
601         OGL_DISABLE(TEXTURE_2D);
602         glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
603         glBegin(GL_LINES);
604         glVertex2f(xo,yo);
605         glVertex2f(xf,yf);
606         glEnd();
607 }
608         
609
610 GLfloat last_r=0, last_g=0, last_b=0;
611 int do_pal_step=0;
612 void ogl_do_palfx(void){
613 //      GLfloat r,g,b,a;
614         OGL_DISABLE(TEXTURE_2D);
615         if (gr_palette_faded_out){
616 /*              glEnable(GL_BLEND);
617                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);*/
618                 glColor3f(0,0,0);
619 //              r=g=b=0.0;a=1.0;
620         }else{
621                 if (do_pal_step){
622                         //glBlendFunc(GL_SRC_COLOR, GL_DST_COLOR);
623                         glEnable(GL_BLEND);
624                         glBlendFunc(GL_ONE,GL_ONE);
625                         glColor3f(last_r,last_g,last_b);
626 //                      r=f2fl(last_r);g=f2fl(last_g);b=f2fl(last_b);a=0.5;
627                 }else
628                         return;
629         }
630         
631         
632         glBegin(GL_QUADS);
633         glVertex2f(0,0);
634         glVertex2f(0,1);
635         glVertex2f(1,1);
636         glVertex2f(1,0);
637         glEnd();
638         
639         glEnable(GL_BLEND);     
640         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
641 }
642
643 void gr_palette_clear()
644 {
645         gr_palette_faded_out=1;
646 }
647
648
649 int ogl_brightness_ok = 0;
650 int ogl_setgammaramp_ok = 1;
651 int ogl_brightness_r = 0, ogl_brightness_g = 0, ogl_brightness_b = 0;
652 static int old_b_r = 0, old_b_g = 0, old_b_b = 0;
653
654 void gr_palette_step_up(int r, int g, int b)
655 {
656         if (gr_palette_faded_out)
657                 return;
658
659         old_b_r = ogl_brightness_r;
660         old_b_g = ogl_brightness_g;
661         old_b_b = ogl_brightness_b;
662
663         ogl_brightness_r = max(r + gr_palette_gamma.intval, 0);
664         ogl_brightness_g = max(g + gr_palette_gamma.intval, 0);
665         ogl_brightness_b = max(b + gr_palette_gamma.intval, 0);
666
667         if (ogl_setgammaramp_ok &&
668             (old_b_r != ogl_brightness_r ||
669              old_b_g != ogl_brightness_g ||
670              old_b_b != ogl_brightness_b))
671                 ogl_brightness_ok = !ogl_setbrightness_internal();
672
673         if (!ogl_setgammaramp_ok || !ogl_brightness_ok)
674         {
675                 last_r = ogl_brightness_r / 63.0;
676                 last_g = ogl_brightness_g / 63.0;
677                 last_b = ogl_brightness_b / 63.0;
678
679                 do_pal_step = (r || g || b || gr_palette_gamma.intval);
680         }
681         else
682         {
683                 do_pal_step = 0;
684         }
685 }
686
687
688 void gr_palette_load( ubyte *pal )      
689 {
690  int i;//, j;
691
692  for (i=0; i<768; i++ ) {
693      gr_current_pal[i] = pal[i];
694      if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
695  }
696  //palette = screen->format->palette;
697
698  gr_palette_faded_out=0;
699
700         gr_palette_step_up(0, 0, 0); // make ogl_setbrightness_internal get run so that menus get brightened too.
701
702  init_computed_colors();
703
704         ogl_init_shared_palette();
705 }
706
707
708
709 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
710 {
711         int j;
712         unsigned char *buf;
713
714         if (gr_palette_faded_out) return 0;
715
716         if (grd_fades_disabled || !ogl_readpixels_ok) {
717                 gr_palette_faded_out = 1;
718                 return 0;
719         }
720
721         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
722         glEnable(GL_BLEND);
723         glDisable(GL_DEPTH_TEST);
724
725         MALLOC(buf, unsigned char, grd_curscreen->sc_w * grd_curscreen->sc_h * 3);
726         glReadBuffer(GL_FRONT);
727         glReadPixels(0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h, GL_RGB, GL_UNSIGNED_BYTE, buf);
728
729         for (j = 0; j < nsteps; j++) {
730                 glRasterPos2f(0,0);
731                 glDrawPixels(grd_curscreen->sc_w, grd_curscreen->sc_h, GL_RGB, GL_UNSIGNED_BYTE, buf);
732
733                 glColor4f(0.0, 0.0, 0.0, (float)(j) / (nsteps - 1)); // go from 0 to 1
734                 glBegin(GL_QUADS);
735                 glVertex2i(0, 0);
736                 glVertex2i(0, 1);
737                 glVertex2i(1, 1);
738                 glVertex2i(1, 0);
739                 glEnd();
740
741                 glFlush();
742
743                 timer_delay(f0_1 / 10);
744         }
745         d_free(buf);
746
747         gr_palette_faded_out=1;
748         return 0;
749 }
750
751
752
753 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
754 {
755         int j;
756         unsigned char *buf;
757
758         if (!gr_palette_faded_out) return 0;
759
760         if (grd_fades_disabled || !ogl_readpixels_ok) {
761                 gr_palette_faded_out = 0;
762                 return 0;
763         }
764
765         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
766         glEnable(GL_BLEND);
767         glDisable(GL_DEPTH_TEST);
768
769         MALLOC(buf, unsigned char, grd_curscreen->sc_w * grd_curscreen->sc_h * 3);
770         glReadBuffer(GL_FRONT);
771         glReadPixels(0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h, GL_RGB, GL_UNSIGNED_BYTE, buf);
772
773         for (j = 0; j < nsteps; j++) {
774                 glRasterPos2f(0,0);
775                 glDrawPixels(grd_curscreen->sc_w, grd_curscreen->sc_h, GL_RGB, GL_UNSIGNED_BYTE, buf);
776
777                 glColor4f(0.0, 0.0, 0.0, (float)(nsteps - 1 - j) / (nsteps - 1)); // go from 1 to 0
778                 glBegin(GL_QUADS);
779                 glVertex2i(0, 0);
780                 glVertex2i(0, 1);
781                 glVertex2i(1, 1);
782                 glVertex2i(1, 0);
783                 glEnd();
784
785                 glFlush();
786
787                 timer_delay(f0_1 / 10);
788         }
789         d_free(buf);
790
791         gr_palette_faded_out=0;
792         return 0;
793 }
794
795
796
797 void gr_palette_read(ubyte * pal)
798 {
799         int i;
800         for (i=0; i<768; i++ ) {
801                 pal[i]=gr_current_pal[i];
802                 if (pal[i] > 63) pal[i] = 63;
803         }
804 }
805
806 //writes out an uncompressed RGB .tga file
807 //if we got really spiffy, we could optionally link in libpng or something, and use that.
808 void write_bmp(char *savename,int w,int h,unsigned char *buf){
809         PHYSFS_file *f;
810
811         f = PHYSFSX_openWriteBuffered(savename);
812                 
813         if (f) {
814                 GLubyte    targaMagic[12] = { 0, //no identification field
815                          0,//no colormap
816                          2,//RGB image (well, BGR, actually)
817                          0, 0, 0, 0, 0, 0, 0, 0, 0 };//no colormap or image origin stuff.
818                 GLubyte blah;
819                 int r;
820                 GLubyte *s;
821                 int x,y;
822                 
823                 //write .TGA header.
824                 PHYSFS_write(f, targaMagic, sizeof(targaMagic), 1);
825                 PHYSFS_writeSLE16(f, w);
826                 PHYSFS_writeSLE16(f, h);
827                 PHYSFSX_writeU8(f, 24); // 24 bpp
828                 PHYSFSX_writeU8(f, 0);  // no attribute bits, origin is lowerleft, no interleave
829                 
830                 s=buf;
831                 for (y=0;y<h;y++){//TGAs use BGR ordering of data.
832                         for (x=0;x<w;x++){
833                                 blah=s[0];
834                                 s[0]=s[2];
835                                 s[2]=blah;
836                                 s+=3;                           
837                         }
838                 }
839                 x=0;y=w*h*3;
840                 while (y > 0)
841                 {
842                         r = (int)PHYSFS_write(f, buf + x, 1, y);
843                         if (r<=0){
844                                 mprintf((0,"screenshot error, couldn't write to %s (err %i)\n",savename,errno));
845                                 break;
846                         }
847                         x+=r;y-=r;
848                 }
849                 PHYSFS_close(f);
850         }else{
851                 mprintf((0,"screenshot error, couldn't open %s (err %i)\n",savename,errno));
852         }
853 }
854 void save_screen_shot(int automap_flag)
855 {
856 //      fix t1;
857         char message[100];
858         static int savenum=0;
859         char savename[13];
860         unsigned char *buf;
861         
862         if (!ogl_readpixels_ok){
863                 if (!automap_flag)
864                         hud_message(MSGC_GAME_FEEDBACK,"glReadPixels not supported on your configuration");
865                 return;
866         }
867
868         stop_time();
869
870 //added/changed on 10/31/98 by Victor Rachels to fix overwrite each new game
871         do
872         {
873                 if (savenum == 9999)
874                         savenum = 0;
875                 sprintf(savename, "scrn%04d.tga", savenum++);
876         } while (PHYSFS_exists(savename));
877
878         sprintf( message, "%s '%s'", TXT_DUMPING_SCREEN, savename );
879 //end this section addition/change - Victor Rachels
880
881         if (automap_flag) {
882 //      save_font = grd_curcanv->cv_font;
883 //      gr_set_curfont(GAME_FONT);
884 //      gr_set_fontcolor(gr_find_closest_color_current(0,31,0),-1);
885 //      gr_get_string_size(message,&w,&h,&aw);
886 //              modex_print_message(32, 2, message);
887         } else {
888                 hud_message(MSGC_GAME_FEEDBACK, "%s", message);
889         }
890         
891         buf = d_malloc(grd_curscreen->sc_w*grd_curscreen->sc_h*3);
892         glReadBuffer(GL_FRONT);
893         glReadPixels(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
894         write_bmp(savename,grd_curscreen->sc_w,grd_curscreen->sc_h,buf);
895         d_free(buf);
896
897         key_flush();
898         start_time();
899 }