]> icculus.org git repositories - btb/d2x.git/blob - arch/sdl/gr.c
add gamma cvar
[btb/d2x.git] / arch / sdl / gr.c
1 /*
2  *
3  * SDL video functions.
4  *
5  *
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include <conf.h>
10 #endif
11
12 #include <stdlib.h>
13 #include <string.h>
14 #include <SDL.h>
15 #ifdef SDL_IMAGE
16 #include <SDL_image.h>
17 #endif
18
19 #include "gr.h"
20 #include "grdef.h"
21 #include "palette.h"
22 #include "u_mem.h"
23 #include "error.h"
24 #include "inferno.h"
25 #include "timer.h"
26 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
27 #include "args.h"
28
29 #ifdef _WIN32_WCE // should really be checking for "Pocket PC" somehow
30 # define LANDSCAPE
31 #endif
32
33 int sdl_video_flags = SDL_SWSURFACE | SDL_HWPALETTE;
34 //end addition -MM
35
36 SDL_Surface *screen;
37 #ifdef LANDSCAPE
38 static SDL_Surface *real_screen, *screen2;
39 #endif
40
41 int gr_installed = 0;
42
43 //added 05/19/99 Matt Mueller - locking stuff
44 #ifdef GR_LOCK
45 #include "checker.h"
46 #ifdef TEST_GR_LOCK
47 int gr_testlocklevel=0;
48 #endif
49 inline void gr_dolock(const char *file,int line) {
50         gr_dotestlock();
51         if ( gr_testlocklevel==1 && SDL_MUSTLOCK(screen) ) {
52 #ifdef __CHECKER__
53                 chcksetwritable(screen.pixels,screen->w*screen->h*screen->format->BytesPerPixel);
54 #endif
55                 if ( SDL_LockSurface(screen) < 0 )Error("could not lock screen (%s:%i)\n",file,line);
56         }
57 }
58 inline void gr_dounlock(void) {
59         gr_dotestunlock();
60         if (gr_testlocklevel==0 && SDL_MUSTLOCK(screen) ) {
61                 SDL_UnlockSurface(screen);
62 #ifdef __CHECKER__
63                 chcksetunwritable(screen.pixels,screen->w*screen->h*screen->format->BytesPerPixel);
64 #endif
65         }
66 }
67 #endif
68 //end addition -MM
69
70 #ifdef LANDSCAPE
71 /* Create a new rotated surface for drawing */
72 SDL_Surface *CreateRotatedSurface(SDL_Surface *s)
73 {
74 #if 0
75     return(SDL_CreateRGBSurface(s->flags, s->h, s->w,
76     s->format->BitsPerPixel,
77     s->format->Rmask,
78     s->format->Gmask,
79     s->format->Bmask,
80     s->format->Amask));
81 #else
82     return(SDL_CreateRGBSurface(s->flags, s->h, s->w, 8, 0, 0, 0, 0));
83 #endif
84 }
85
86 /* Used to copy the rotated scratch surface to the screen */
87 void BlitRotatedSurface(SDL_Surface *from, SDL_Surface *to)
88 {
89
90     int bpp = from->format->BytesPerPixel;
91     int w=from->w, h=from->h, pitch=to->pitch;
92     int i,j;
93     Uint8 *pfrom, *pto, *to0;
94
95     SDL_LockSurface(from);
96     SDL_LockSurface(to);
97     pfrom=(Uint8 *)from->pixels;
98     to0=(Uint8 *) to->pixels+pitch*(w-1);
99     for (i=0; i<h; i++)
100     {
101         to0+=bpp;
102         pto=to0;
103         for (j=0; j<w; j++)
104         {
105             if (bpp==1) *pto=*pfrom;
106             else if (bpp==2) *(Uint16 *)pto=*(Uint16 *)pfrom;
107             else if (bpp==4) *(Uint32 *)pto=*(Uint32 *)pfrom;
108             else if (bpp==3)
109                 {
110                     pto[0]=pfrom[0];
111                     pto[1]=pfrom[1];
112                     pto[2]=pfrom[2];
113                 }
114             pfrom+=bpp;
115             pto-=pitch;
116         }
117     }
118     SDL_UnlockSurface(from);
119     SDL_UnlockSurface(to);
120 }
121 #endif
122
123 void gr_palette_clear(); // Function prototype for gr_init;
124
125
126 void gr_update()
127 {
128         //added 05/19/99 Matt Mueller - locking stuff
129 //      gr_testunlock();
130         //end addition -MM
131 #ifdef LANDSCAPE
132         screen2 = SDL_DisplayFormat(screen);
133         BlitRotatedSurface(screen2, real_screen);
134         //SDL_SetColors(real_screen, screen->format->palette->colors, 0, 256);
135         SDL_UpdateRect(real_screen, 0, 0, 0, 0);
136         SDL_FreeSurface(screen2);
137 #else
138         SDL_UpdateRect(screen, 0, 0, 0, 0);
139 #endif
140 }
141
142
143 int gr_check_mode(uint32_t mode)
144 {
145         int w, h;
146
147         w = SM_W(mode);
148         h = SM_H(mode);
149
150         return !SDL_VideoModeOK(w, h, 8, sdl_video_flags);
151 }
152
153
154 extern uint32_t VGA_current_mode; // DPH: kludge - remove at all costs
155
156 int gr_set_mode(uint32_t mode)
157 {
158         int w,h;
159
160 #ifdef NOGRAPH
161         return 0;
162 #endif
163
164         if (mode<=0)
165                 return 0;
166
167         if (mode == VGA_current_mode)
168                 return 0;
169
170         w=SM_W(mode);
171         h=SM_H(mode);
172         VGA_current_mode = mode;
173         
174         if (screen != NULL) gr_palette_clear();
175
176 //added on 11/06/98 by Matt Mueller to set the title bar. (moved from below)
177 //sekmu: might wanna copy this litte blurb to one of the text files or something
178 //we want to set it here so that X window manager "Style" type commands work
179 //for example, in fvwm2 or fvwm95:
180 //Style "D1X*"  NoTitle, NoHandles, BorderWidth 0
181 //if you can't use -fullscreen like me (crashes X), this is a big help in
182 //getting the window centered correctly (if you use SmartPlacement)
183         SDL_WM_SetCaption(PACKAGE_STRING, "Descent II");
184 //end addition -MM
185
186 #ifdef SDL_IMAGE
187         {
188 #include "descent.xpm"
189                 SDL_WM_SetIcon(IMG_ReadXPMFromArray(pixmap), NULL);
190         }
191 #endif
192
193 //edited 10/05/98 by Matt Mueller - make fullscreen mode optional
194           // changed by adb on 980913: added SDL_HWPALETTE (should be option?)
195         // changed by someone on 980923 to add SDL_FULLSCREEN
196
197 #ifdef LANDSCAPE
198         real_screen = SDL_SetVideoMode(h, w, 0, sdl_video_flags);
199         screen = CreateRotatedSurface(real_screen);
200 #else
201         screen = SDL_SetVideoMode(w, h, 8, sdl_video_flags);
202 #endif
203                 // end changes by someone
204         // end changes by adb
205 //end edit -MM
206         if (screen == NULL) {
207            Error("Could not set %dx%dx8 video mode\n",w,h);
208         }
209         memset( grd_curscreen, 0, sizeof(grs_screen));
210         grd_curscreen->sc_mode = mode;
211         grd_curscreen->sc_w = w;
212         grd_curscreen->sc_h = h;
213         grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
214         grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
215         grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
216         grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
217         grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
218         grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
219         grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_LINEAR;
220         grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
221         gr_set_current_canvas(NULL);
222         //gr_enable_default_palette_loading();
223
224 //--moved up--added on 9/30/98 by Matt Mueller to set the title bar.  Woohoo!
225 //--moved up--  SDL_WM_SetCaption(DESCENT_VERSION " " D1X_DATE, NULL);
226 //--moved up--end addition -MM
227
228 //      gamefont_choose_game_font(w,h);
229         return 0;
230 }
231
232 int gr_check_fullscreen(void){
233         return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
234 }
235
236 int gr_toggle_fullscreen(void){
237         sdl_video_flags^=SDL_FULLSCREEN;
238         SDL_WM_ToggleFullScreen(screen);
239         return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
240 }
241
242 int gr_init(void)
243 {
244         // Only do this function once!
245         if (gr_installed==1)
246                 return -1;
247
248         if (SDL_Init(SDL_INIT_VIDEO) < 0)
249         {
250                 Error("SDL library video initialisation failed: %s.",SDL_GetError());
251         }
252         MALLOC( grd_curscreen,grs_screen,1 );
253         memset( grd_curscreen, 0, sizeof(grs_screen));
254
255 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
256         if (FindArg("-fullscreen"))
257              sdl_video_flags|=SDL_FULLSCREEN;
258 //end addition -MM
259         //added 05/19/99 Matt Mueller - make HW surface optional
260         if (FindArg("-hwsurface"))
261              sdl_video_flags|=SDL_HWSURFACE;
262         //end addition -MM
263
264         grd_curscreen->sc_canvas.cv_color = 0;
265         grd_curscreen->sc_canvas.cv_drawmode = 0;
266         grd_curscreen->sc_canvas.cv_font = NULL;
267         grd_curscreen->sc_canvas.cv_font_fg_color = 0;
268         grd_curscreen->sc_canvas.cv_font_bg_color = 0;
269         gr_set_current_canvas( &grd_curscreen->sc_canvas );
270
271         cvar_registervariable(&gr_palette_gamma);
272
273         gr_installed = 1;
274         // added on 980913 by adb to add cleanup
275         atexit(gr_close);
276         // end changes by adb
277
278         return 0;
279 }
280
281 void gr_close()
282 {
283         if (gr_installed==1)
284         {
285                 gr_installed = 0;
286                 d_free(grd_curscreen);
287         }
288 }
289
290 // Palette functions follow.
291
292 static int last_r=0, last_g=0, last_b=0;
293
294 void gr_palette_clear()
295 {
296  SDL_Palette *palette;
297  SDL_Color colors[256];
298  int ncolors;
299
300  palette = screen->format->palette;
301
302  if (palette == NULL) {
303     return; // Display is not palettised
304  }
305
306  ncolors = palette->ncolors;
307  memset(colors, 0, ncolors * sizeof(SDL_Color));
308
309  SDL_SetColors(screen, colors, 0, 256);
310
311  gr_palette_faded_out = 1;
312 }
313
314
315 void gr_palette_step_up( int r, int g, int b )
316 {
317  int i;
318  ubyte *p = gr_palette;
319  int temp;
320
321  SDL_Palette *palette;
322  SDL_Color colors[256];
323
324  if (gr_palette_faded_out) return;
325
326  if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
327
328  last_r = r;
329  last_g = g;
330  last_b = b;
331
332  palette = screen->format->palette;
333
334  if (palette == NULL) {
335     return; // Display is not palettised
336  }
337
338  for (i=0; i<256; i++) {
339    temp = (int)(*p++) + r + gr_palette_gamma.intval;
340    if (temp<0) temp=0;
341    else if (temp>63) temp=63;
342    colors[i].r = temp * 4;
343    temp = (int)(*p++) + g + gr_palette_gamma.intval;
344    if (temp<0) temp=0;
345    else if (temp>63) temp=63;
346    colors[i].g = temp * 4;
347    temp = (int)(*p++) + b + gr_palette_gamma.intval;
348    if (temp<0) temp=0;
349    else if (temp>63) temp=63;
350    colors[i].b = temp * 4;
351  }
352
353  SDL_SetColors(screen, colors, 0, 256);
354 }
355
356 //added on 980913 by adb to fix palette problems
357 // need a min without side effects...
358 #undef min
359 static inline int min(int x, int y) { return x < y ? x : y; }
360 //end changes by adb
361
362 void gr_palette_load( ubyte *pal )      
363 {
364  int i, j;
365  SDL_Palette *palette;
366  SDL_Color colors[256];
367
368  for (i=0; i<768; i++ ) {
369      gr_current_pal[i] = pal[i];
370      if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
371  }
372
373  palette = screen->format->palette;
374
375  if (palette == NULL) {
376     return; // Display is not palettised
377  }
378
379  for (i = 0, j = 0; j < 256; j++) {
380      //changed on 980913 by adb to fix palette problems
381      colors[j].r = min(gr_current_pal[i++] + gr_palette_gamma.intval, 63) * 4;
382      colors[j].g = min(gr_current_pal[i++] + gr_palette_gamma.intval, 63) * 4;
383      colors[j].b = min(gr_current_pal[i++] + gr_palette_gamma.intval, 63) * 4;
384      //end changes by adb
385  }
386  SDL_SetColors(screen, colors, 0, 256);
387
388  gr_palette_faded_out = 0;
389  init_computed_colors();
390 }
391
392
393
394 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
395 {
396  int i, j, k;
397  ubyte c;
398  fix fade_palette[768];
399  fix fade_palette_delta[768];
400
401  SDL_Palette *palette;
402  SDL_Color fade_colors[256];
403
404  if (gr_palette_faded_out) return 0;
405
406 #if 1 //ifndef NDEBUG
407         if (grd_fades_disabled) {
408                 gr_palette_clear();
409                 return 0;
410         }
411 #endif
412
413  palette = screen->format->palette;
414  if (palette == NULL) {
415     return -1; // Display is not palettised
416  }
417
418  if (pal==NULL) pal=gr_current_pal;
419
420  for (i=0; i<768; i++ ) {
421      gr_current_pal[i] = pal[i];
422      fade_palette[i] = i2f(pal[i]);
423      fade_palette_delta[i] = fade_palette[i] / nsteps;
424  }
425  for (j=0; j<nsteps; j++ )      {
426      for (i=0, k = 0; k<256; k++ )      {
427          fade_palette[i] -= fade_palette_delta[i];
428          if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma.intval))
429             fade_palette[i] = i2f(pal[i] + gr_palette_gamma.intval);
430          c = f2i(fade_palette[i]);
431          if (c > 63) c = 63;
432          fade_colors[k].r = c * 4;
433          i++;
434
435          fade_palette[i] -= fade_palette_delta[i];
436          if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma.intval))
437             fade_palette[i] = i2f(pal[i] + gr_palette_gamma.intval);
438          c = f2i(fade_palette[i]);
439          if (c > 63) c = 63;
440          fade_colors[k].g = c * 4;
441          i++;
442
443          fade_palette[i] -= fade_palette_delta[i];
444          if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma.intval))
445             fade_palette[i] = i2f(pal[i] + gr_palette_gamma.intval);
446          c = f2i(fade_palette[i]);
447          if (c > 63) c = 63;
448          fade_colors[k].b = c * 4;
449          i++;
450      }
451
452   SDL_SetColors(screen, fade_colors, 0, 256);
453  }
454
455  gr_palette_faded_out = 1;
456  return 0;
457 }
458
459
460
461 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
462 {
463  int i, j, k, ncolors;
464  ubyte c;
465  fix fade_palette[768];
466  fix fade_palette_delta[768];
467
468  SDL_Palette *palette;
469  SDL_Color fade_colors[256];
470
471  if (!gr_palette_faded_out) return 0;
472
473 #if 1 //ifndef NDEBUG
474         if (grd_fades_disabled) {
475                 gr_palette_load(pal);
476                 return 0;
477         }
478 #endif
479
480  palette = screen->format->palette;
481
482  if (palette == NULL) {
483     return -1; // Display is not palettised
484  }
485
486  ncolors = palette->ncolors;
487
488  for (i=0; i<768; i++ ) {
489      gr_current_pal[i] = pal[i];
490      fade_palette[i] = 0;
491      fade_palette_delta[i] = i2f(pal[i]) / nsteps;
492  }
493
494  for (j=0; j<nsteps; j++ )      {
495      for (i=0, k = 0; k<256; k++ )      {
496          fade_palette[i] += fade_palette_delta[i];
497          if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma.intval))
498             fade_palette[i] = i2f(pal[i] + gr_palette_gamma.intval);
499          c = f2i(fade_palette[i]);
500          if (c > 63) c = 63;
501          fade_colors[k].r = c * 4;
502          i++;
503
504          fade_palette[i] += fade_palette_delta[i];
505          if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma.intval))
506             fade_palette[i] = i2f(pal[i] + gr_palette_gamma.intval);
507          c = f2i(fade_palette[i]);
508          if (c > 63) c = 63;
509          fade_colors[k].g = c * 4;
510          i++;
511
512          fade_palette[i] += fade_palette_delta[i];
513          if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma.intval))
514             fade_palette[i] = i2f(pal[i] + gr_palette_gamma.intval);
515          c = f2i(fade_palette[i]);
516          if (c > 63) c = 63;
517          fade_colors[k].b = c * 4;
518          i++;
519      }
520
521   SDL_SetColors(screen, fade_colors, 0, 256);
522  }
523  //added on 980913 by adb to fix palette problems
524  gr_palette_load(pal);
525  //end changes by adb
526
527  gr_palette_faded_out = 0;
528  return 0;
529 }
530
531
532
533 void gr_palette_read(ubyte * pal)
534 {
535  SDL_Palette *palette;
536  int i, j;
537
538  palette = screen->format->palette;
539
540  if (palette == NULL) {
541     return; // Display is not palettised
542  }
543
544  for (i = 0, j=0; i < 256; i++) {
545      pal[j++] = palette->colors[i].r / 4;
546      pal[j++] = palette->colors[i].g / 4;
547      pal[j++] = palette->colors[i].b / 4;
548  }
549 }