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