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