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