]> icculus.org git repositories - btb/d2x.git/blob - arch/ogl/vid.c
use correct type
[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 OGL_RUNTIME_LOAD
416 #ifdef _WIN32
417 char *OglLibPath="opengl32.dll";
418 #endif
419 #ifdef __unix__
420 char *OglLibPath="libGL.so";
421 #endif
422 #ifdef macintosh
423 char *OglLibPath = NULL;
424 #endif
425
426 int ogl_rt_loaded=0;
427 int ogl_init_load_library(void)
428 {
429         int retcode=0;
430         if (!ogl_rt_loaded){
431                 int t;
432                 if ((t=FindArg("-gl_library")))
433                         OglLibPath=Args[t+1];
434
435                 retcode = OpenGL_LoadLibrary(true);
436                 if(retcode)
437                 {
438                         mprintf((0,"Opengl loaded ok\n"));
439         
440                         if(!glEnd)
441                         {
442                                 Error("Opengl: Functions not imported\n");
443                         }
444                 }else{
445                         Error("Opengl: error loading %s\n", OglLibPath);
446                 }
447                 ogl_rt_loaded=1;
448         }
449         return retcode;
450 }
451 #endif
452
453
454 int vid_init(void)
455 {
456         int t, glt = 0;
457
458         // Only do this function once!
459         if (vid_installed == 1)
460                 return -1;
461
462 #ifdef OGL_RUNTIME_LOAD
463         ogl_init_load_library();
464 #endif
465
466 #ifdef VID_SUPPORTS_FULLSCREEN_TOGGLE
467         if (FindArg("-gl_voodoo")){
468                 ogl_voodoohack=1;
469                 vid_toggle_fullscreen();
470         }
471         if (FindArg("-fullscreen"))
472                 vid_toggle_fullscreen();
473 #endif
474         if ((glt=FindArg("-gl_alttexmerge")))
475                 ogl_alttexmerge=1;
476         if ((t=FindArg("-gl_stdtexmerge")))
477                 if (t>=glt)//allow overriding of earlier args
478                         ogl_alttexmerge=0;
479
480         if ((glt = FindArg("-gl_16bittextures")))
481         {
482                 ogl_rgba_internalformat = GL_RGB5_A1;
483                 ogl_rgb_internalformat = GL_RGB5;
484         }
485
486         if ((glt=FindArg("-gl_mipmap"))){
487                 GL_texmagfilt=GL_LINEAR;
488                 GL_texminfilt=GL_LINEAR_MIPMAP_NEAREST;
489         }
490         if ((glt=FindArg("-gl_trilinear")))
491         {
492                 GL_texmagfilt = GL_LINEAR;
493                 GL_texminfilt = GL_LINEAR_MIPMAP_LINEAR;
494         }
495         if ((t=FindArg("-gl_simple"))){
496                 if (t>=glt){//allow overriding of earlier args
497                         glt=t;
498                         GL_texmagfilt=GL_NEAREST;
499                         GL_texminfilt=GL_NEAREST;
500                 }
501         }
502         if ((t=FindArg("-gl_texmagfilt")) || (t=FindArg("-gl_texmagfilter"))){
503                 if (t>=glt)//allow overriding of earlier args
504                         GL_texmagfilt=ogl_atotexfilti(Args[t+1],0);
505         }
506         if ((t=FindArg("-gl_texminfilt")) || (t=FindArg("-gl_texminfilter"))){
507                 if (t>=glt)//allow overriding of earlier args
508                         GL_texminfilt=ogl_atotexfilti(Args[t+1],1);
509         }
510         GL_needmipmaps=ogl_testneedmipmaps(GL_texminfilt);
511
512         if ((t = FindArg("-gl_anisotropy")) || (t = FindArg("-gl_anisotropic")))
513         {
514                 GL_texanisofilt=atof(Args[t + 1]);
515         }
516
517         mprintf((0,"gr_init: texmagfilt:%x texminfilt:%x needmipmaps=%i anisotropic:%f\n",GL_texmagfilt,GL_texminfilt,GL_needmipmaps,GL_texanisofilt));
518
519         
520         if ((t=FindArg("-gl_vidmem"))){
521                 ogl_mem_target=atoi(Args[t+1])*1024*1024;
522         }
523         if ((t=FindArg("-gl_reticle"))){
524                 gl_reticle=atoi(Args[t+1]);
525         }
526         //printf("ogl_mem_target=%i\n",ogl_mem_target);
527         
528         ogl_init();//platform specific initialization
529
530         ogl_init_texture_list_internal();
531                 
532         cmd_addcommand("gl_texturemode", ogl_cmd_texturemode, "gl_texturemode <x>\n" "    use OpenGL texture mode <x>. Available mode are\n"
533                                                                                      "        GL_NEAREST\n"
534                                                                                      "        GL_LINEAR\n"
535                                                                                      "        GL_NEAREST_MIPMAP_NEAREST\n"
536                                                                                      "        GL_LINEAR_MIPMAP_NEAREST\n"
537                                                                                      "        GL_NEAREST_MIPMAP_LINEAR\n"
538                                                                                      "        GL_LINEAR_MIPMAP_LINEAR");
539
540         vid_installed = 1;
541         
542         atexit(vid_close);
543
544         return 0;
545 }
546
547
548 void vid_close(void)
549 {
550 //      mprintf((0,"ogl init: %s %s %s - %s\n",glGetString(GL_VENDOR),glGetString(GL_RENDERER),glGetString(GL_VERSION),glGetString,(GL_EXTENSIONS)));
551         ogl_brightness_r = ogl_brightness_g = ogl_brightness_b = 0;
552         ogl_setbrightness_internal();
553
554         ogl_close();//platform specific code
555
556 #ifdef OGL_RUNTIME_LOAD
557         if (ogl_rt_loaded)
558                 OpenGL_LoadLibrary(false);
559 #endif
560 }
561 extern int r_upixelc;
562 void ogl_upixelc(int x, int y, int c){
563         r_upixelc++;
564 //      printf("gr_upixelc(%i,%i,%i)%i\n",x,y,c,Function_mode==FMODE_GAME);
565 //      if(Function_mode != FMODE_GAME){
566 //              grd_curcanv->cv_bitmap.bm_data[y*grd_curscreen->sc_canvas.cv_bitmap.bm_w+x]=c;
567 //      }else{
568                 OGL_DISABLE(TEXTURE_2D);
569                 glPointSize(1.0);
570                 glBegin(GL_POINTS);
571 //              glBegin(GL_LINES);
572 //      ogl_pal=gr_current_pal;
573                 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
574 //      ogl_pal=gr_palette;
575                 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);
576 //              glVertex2f(x/((float)last_width+1),1.0-y/((float)last_height+1));
577                 glEnd();
578 //      }
579 }
580 void ogl_urect(int left,int top,int right,int bot){
581         GLfloat xo,yo,xf,yf;
582         int c=COLOR;
583         
584         xo=(left+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
585         xf = (right + 1 + grd_curcanv->cv_bitmap.bm_x) / (float)last_width;
586         yo=1.0-(top+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
587         yf = 1.0 - (bot + 1 + grd_curcanv->cv_bitmap.bm_y) / (float)last_height;
588
589         OGL_DISABLE(TEXTURE_2D);
590         if (Gr_scanline_darkening_level >= GR_FADE_LEVELS)
591                 glColor3f(CPAL2Tr(c), CPAL2Tg(c), CPAL2Tb(c));
592         else
593                 glColor4f(CPAL2Tr(c), CPAL2Tg(c), CPAL2Tb(c), 1.0 - (float)Gr_scanline_darkening_level / ((float)GR_FADE_LEVELS - 1.0));
594         glBegin(GL_QUADS);
595         glVertex2f(xo,yo);
596         glVertex2f(xo,yf);
597         glVertex2f(xf,yf);
598         glVertex2f(xf,yo);
599         glEnd();
600 }
601 void ogl_ulinec(int left,int top,int right,int bot,int c){
602         GLfloat xo,yo,xf,yf;
603
604         xo = (left + grd_curcanv->cv_bitmap.bm_x + 0.5) / (float)last_width;
605         xf = (right + grd_curcanv->cv_bitmap.bm_x + 0.5) / (float)last_width;
606         yo = 1.0 - (top + grd_curcanv->cv_bitmap.bm_y + 0.5) / (float)last_height;
607         yf = 1.0 - (bot + grd_curcanv->cv_bitmap.bm_y + 0.5) / (float)last_height;
608
609         OGL_DISABLE(TEXTURE_2D);
610         glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
611         glBegin(GL_LINES);
612         glVertex2f(xo,yo);
613         glVertex2f(xf,yf);
614         glEnd();
615 }
616         
617
618 GLfloat last_r=0, last_g=0, last_b=0;
619 int do_pal_step=0;
620 void ogl_do_palfx(void){
621 //      GLfloat r,g,b,a;
622         OGL_DISABLE(TEXTURE_2D);
623         if (gr_palette_faded_out){
624 /*              glEnable(GL_BLEND);
625                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);*/
626                 glColor3f(0,0,0);
627 //              r=g=b=0.0;a=1.0;
628         }else{
629                 if (do_pal_step){
630                         //glBlendFunc(GL_SRC_COLOR, GL_DST_COLOR);
631                         glEnable(GL_BLEND);
632                         glBlendFunc(GL_ONE,GL_ONE);
633                         glColor3f(last_r,last_g,last_b);
634 //                      r=f2fl(last_r);g=f2fl(last_g);b=f2fl(last_b);a=0.5;
635                 }else
636                         return;
637         }
638         
639         
640         glBegin(GL_QUADS);
641         glVertex2f(0,0);
642         glVertex2f(0,1);
643         glVertex2f(1,1);
644         glVertex2f(1,0);
645         glEnd();
646         
647         glEnable(GL_BLEND);     
648         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
649 }
650
651 void gr_palette_clear()
652 {
653         gr_palette_faded_out=1;
654 }
655
656
657 int ogl_brightness_ok = 0;
658 int ogl_setgammaramp_ok = 1;
659 int ogl_brightness_r = 0, ogl_brightness_g = 0, ogl_brightness_b = 0;
660 static int old_b_r = 0, old_b_g = 0, old_b_b = 0;
661
662 void gr_palette_step_up(int r, int g, int b)
663 {
664         if (gr_palette_faded_out)
665                 return;
666
667         old_b_r = ogl_brightness_r;
668         old_b_g = ogl_brightness_g;
669         old_b_b = ogl_brightness_b;
670
671         ogl_brightness_r = max(r + gr_palette_gamma.intval, 0);
672         ogl_brightness_g = max(g + gr_palette_gamma.intval, 0);
673         ogl_brightness_b = max(b + gr_palette_gamma.intval, 0);
674
675         if (ogl_setgammaramp_ok &&
676             (old_b_r != ogl_brightness_r ||
677              old_b_g != ogl_brightness_g ||
678              old_b_b != ogl_brightness_b))
679                 ogl_brightness_ok = !ogl_setbrightness_internal();
680
681         if (!ogl_setgammaramp_ok || !ogl_brightness_ok)
682         {
683                 last_r = ogl_brightness_r / 63.0;
684                 last_g = ogl_brightness_g / 63.0;
685                 last_b = ogl_brightness_b / 63.0;
686
687                 do_pal_step = (r || g || b || gr_palette_gamma.intval);
688         }
689         else
690         {
691                 do_pal_step = 0;
692         }
693 }
694
695
696 void gr_palette_load( ubyte *pal )      
697 {
698  int i;//, j;
699
700  for (i=0; i<768; i++ ) {
701      gr_current_pal[i] = pal[i];
702      if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
703  }
704  //palette = screen->format->palette;
705
706  gr_palette_faded_out=0;
707
708         gr_palette_step_up(0, 0, 0); // make ogl_setbrightness_internal get run so that menus get brightened too.
709
710  init_computed_colors();
711
712         ogl_init_shared_palette();
713 }
714
715
716
717 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
718 {
719         int j;
720         unsigned char *buf;
721
722         if (gr_palette_faded_out) return 0;
723
724         if (grd_fades_disabled || !ogl_readpixels_ok) {
725                 gr_palette_faded_out = 1;
726                 return 0;
727         }
728
729         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
730         glEnable(GL_BLEND);
731         glDisable(GL_DEPTH_TEST);
732
733         MALLOC(buf, unsigned char, grd_curscreen->sc_w * grd_curscreen->sc_h * 3);
734         glReadBuffer(GL_FRONT);
735         glReadPixels(0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h, GL_RGB, GL_UNSIGNED_BYTE, buf);
736
737         for (j = 0; j < nsteps; j++) {
738                 glRasterPos2f(0,0);
739                 glDrawPixels(grd_curscreen->sc_w, grd_curscreen->sc_h, GL_RGB, GL_UNSIGNED_BYTE, buf);
740
741                 glColor4f(0.0, 0.0, 0.0, (float)(j) / (nsteps - 1)); // go from 0 to 1
742                 glBegin(GL_QUADS);
743                 glVertex2i(0, 0);
744                 glVertex2i(0, 1);
745                 glVertex2i(1, 1);
746                 glVertex2i(1, 0);
747                 glEnd();
748
749                 glFlush();
750
751                 timer_delay(f0_1 / 10);
752         }
753         d_free(buf);
754
755         gr_palette_faded_out=1;
756         return 0;
757 }
758
759
760
761 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
762 {
763         int j;
764         unsigned char *buf;
765
766         if (!gr_palette_faded_out) return 0;
767
768         if (grd_fades_disabled || !ogl_readpixels_ok) {
769                 gr_palette_faded_out = 0;
770                 return 0;
771         }
772
773         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
774         glEnable(GL_BLEND);
775         glDisable(GL_DEPTH_TEST);
776
777         MALLOC(buf, unsigned char, grd_curscreen->sc_w * grd_curscreen->sc_h * 3);
778         glReadBuffer(GL_FRONT);
779         glReadPixels(0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h, GL_RGB, GL_UNSIGNED_BYTE, buf);
780
781         for (j = 0; j < nsteps; j++) {
782                 glRasterPos2f(0,0);
783                 glDrawPixels(grd_curscreen->sc_w, grd_curscreen->sc_h, GL_RGB, GL_UNSIGNED_BYTE, buf);
784
785                 glColor4f(0.0, 0.0, 0.0, (float)(nsteps - 1 - j) / (nsteps - 1)); // go from 1 to 0
786                 glBegin(GL_QUADS);
787                 glVertex2i(0, 0);
788                 glVertex2i(0, 1);
789                 glVertex2i(1, 1);
790                 glVertex2i(1, 0);
791                 glEnd();
792
793                 glFlush();
794
795                 timer_delay(f0_1 / 10);
796         }
797         d_free(buf);
798
799         gr_palette_faded_out=0;
800         return 0;
801 }
802
803
804
805 void gr_palette_read(ubyte * pal)
806 {
807         int i;
808         for (i=0; i<768; i++ ) {
809                 pal[i]=gr_current_pal[i];
810                 if (pal[i] > 63) pal[i] = 63;
811         }
812 }
813
814 //writes out an uncompressed RGB .tga file
815 //if we got really spiffy, we could optionally link in libpng or something, and use that.
816 void write_bmp(char *savename,int w,int h,unsigned char *buf){
817         PHYSFS_file *f;
818
819         f = PHYSFSX_openWriteBuffered(savename);
820                 
821         if (f) {
822                 GLubyte    targaMagic[12] = { 0, //no identification field
823                          0,//no colormap
824                          2,//RGB image (well, BGR, actually)
825                          0, 0, 0, 0, 0, 0, 0, 0, 0 };//no colormap or image origin stuff.
826                 GLubyte blah;
827                 int r;
828                 GLubyte *s;
829                 int x,y;
830                 
831                 //write .TGA header.
832                 PHYSFS_write(f, targaMagic, sizeof(targaMagic), 1);
833                 PHYSFS_writeSLE16(f, w);
834                 PHYSFS_writeSLE16(f, h);
835                 PHYSFSX_writeU8(f, 24); // 24 bpp
836                 PHYSFSX_writeU8(f, 0);  // no attribute bits, origin is lowerleft, no interleave
837                 
838                 s=buf;
839                 for (y=0;y<h;y++){//TGAs use BGR ordering of data.
840                         for (x=0;x<w;x++){
841                                 blah=s[0];
842                                 s[0]=s[2];
843                                 s[2]=blah;
844                                 s+=3;                           
845                         }
846                 }
847                 x=0;y=w*h*3;
848                 while (y > 0)
849                 {
850                         r = (int)PHYSFS_write(f, buf + x, 1, y);
851                         if (r<=0){
852                                 mprintf((0,"screenshot error, couldn't write to %s (err %i)\n",savename,errno));
853                                 break;
854                         }
855                         x+=r;y-=r;
856                 }
857                 PHYSFS_close(f);
858         }else{
859                 mprintf((0,"screenshot error, couldn't open %s (err %i)\n",savename,errno));
860         }
861 }
862 void save_screen_shot(int automap_flag)
863 {
864 //      fix t1;
865         char message[100];
866         static int savenum=0;
867         char savename[13];
868         unsigned char *buf;
869         
870         if (!ogl_readpixels_ok){
871                 if (!automap_flag)
872                         hud_message(MSGC_GAME_FEEDBACK,"glReadPixels not supported on your configuration");
873                 return;
874         }
875
876         stop_time();
877
878 //added/changed on 10/31/98 by Victor Rachels to fix overwrite each new game
879         do
880         {
881                 if (savenum == 9999)
882                         savenum = 0;
883                 sprintf(savename, "scrn%04d.tga", savenum++);
884         } while (PHYSFS_exists(savename));
885
886         sprintf( message, "%s '%s'", TXT_DUMPING_SCREEN, savename );
887 //end this section addition/change - Victor Rachels
888
889         if (automap_flag) {
890 //      save_font = grd_curcanv->cv_font;
891 //      gr_set_curfont(GAME_FONT);
892 //      gr_set_fontcolor(gr_find_closest_color_current(0,31,0),-1);
893 //      gr_get_string_size(message,&w,&h,&aw);
894 //              modex_print_message(32, 2, message);
895         } else {
896                 hud_message(MSGC_GAME_FEEDBACK, "%s", message);
897         }
898         
899         buf = d_malloc(grd_curscreen->sc_w*grd_curscreen->sc_h*3);
900         glReadBuffer(GL_FRONT);
901         glReadPixels(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
902         write_bmp(savename,grd_curscreen->sc_w,grd_curscreen->sc_h,buf);
903         d_free(buf);
904
905         key_flush();
906         start_time();
907 }