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