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