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