]> icculus.org git repositories - btb/d2x.git/blob - arch/sdl/gr.c
change u_int*_t to C99 standard uint*_t
[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(uint32_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(uint32_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         if (mode == VGA_current_mode)
169                 return 0;
170
171         w=SM_W(mode);
172         h=SM_H(mode);
173         VGA_current_mode = mode;
174         
175         if (screen != NULL) gr_palette_clear();
176
177 //added on 11/06/98 by Matt Mueller to set the title bar. (moved from below)
178 //sekmu: might wanna copy this litte blurb to one of the text files or something
179 //we want to set it here so that X window manager "Style" type commands work
180 //for example, in fvwm2 or fvwm95:
181 //Style "D1X*"  NoTitle, NoHandles, BorderWidth 0
182 //if you can't use -fullscreen like me (crashes X), this is a big help in
183 //getting the window centered correctly (if you use SmartPlacement)
184         SDL_WM_SetCaption(PACKAGE_STRING, "Descent II");
185 //end addition -MM
186
187 #ifdef SDL_IMAGE
188         {
189 #include "descent.xpm"
190                 SDL_WM_SetIcon(IMG_ReadXPMFromArray(pixmap), NULL);
191         }
192 #endif
193
194 //edited 10/05/98 by Matt Mueller - make fullscreen mode optional
195           // changed by adb on 980913: added SDL_HWPALETTE (should be option?)
196         // changed by someone on 980923 to add SDL_FULLSCREEN
197
198 #ifdef LANDSCAPE
199         real_screen = SDL_SetVideoMode(h, w, 0, sdl_video_flags);
200         screen = CreateRotatedSurface(real_screen);
201 #else
202         screen = SDL_SetVideoMode(w, h, 8, sdl_video_flags);
203 #endif
204                 // end changes by someone
205         // end changes by adb
206 //end edit -MM
207         if (screen == NULL) {
208            Error("Could not set %dx%dx8 video mode\n",w,h);
209         }
210         memset( grd_curscreen, 0, sizeof(grs_screen));
211         grd_curscreen->sc_mode = mode;
212         grd_curscreen->sc_w = w;
213         grd_curscreen->sc_h = h;
214         grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
215         grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
216         grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
217         grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
218         grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
219         grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
220         grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_LINEAR;
221         grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
222         gr_set_current_canvas(NULL);
223         //gr_enable_default_palette_loading();
224
225 //added on 9/30/98 by Matt Mueller to hide the mouse if its over the game window
226         SDL_ShowCursor(0);
227 //end addition -MM
228 //--moved up--added on 9/30/98 by Matt Mueller to set the title bar.  Woohoo!
229 //--moved up--  SDL_WM_SetCaption(DESCENT_VERSION " " D1X_DATE, NULL);
230 //--moved up--end addition -MM
231
232 //      gamefont_choose_game_font(w,h);
233         return 0;
234 }
235
236 int gr_check_fullscreen(void){
237         return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
238 }
239
240 int gr_toggle_fullscreen(void){
241         sdl_video_flags^=SDL_FULLSCREEN;
242         SDL_WM_ToggleFullScreen(screen);
243         return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
244 }
245
246 int gr_init(void)
247 {
248         // Only do this function once!
249         if (gr_installed==1)
250                 return -1;
251
252         if (SDL_Init(SDL_INIT_VIDEO) < 0)
253         {
254                 Error("SDL library video initialisation failed: %s.",SDL_GetError());
255         }
256         MALLOC( grd_curscreen,grs_screen,1 );
257         memset( grd_curscreen, 0, sizeof(grs_screen));
258
259 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
260         if (FindArg("-fullscreen"))
261              sdl_video_flags|=SDL_FULLSCREEN;
262 //end addition -MM
263         //added 05/19/99 Matt Mueller - make HW surface optional
264         if (FindArg("-hwsurface"))
265              sdl_video_flags|=SDL_HWSURFACE;
266         //end addition -MM
267
268         grd_curscreen->sc_canvas.cv_color = 0;
269         grd_curscreen->sc_canvas.cv_drawmode = 0;
270         grd_curscreen->sc_canvas.cv_font = NULL;
271         grd_curscreen->sc_canvas.cv_font_fg_color = 0;
272         grd_curscreen->sc_canvas.cv_font_bg_color = 0;
273         gr_set_current_canvas( &grd_curscreen->sc_canvas );
274
275         gr_installed = 1;
276         // added on 980913 by adb to add cleanup
277         atexit(gr_close);
278         // end changes by adb
279
280         return 0;
281 }
282
283 void gr_close()
284 {
285         if (gr_installed==1)
286         {
287                 gr_installed = 0;
288                 d_free(grd_curscreen);
289         }
290 }
291
292 // Palette functions follow.
293
294 static int last_r=0, last_g=0, last_b=0;
295
296 void gr_palette_clear()
297 {
298  SDL_Palette *palette;
299  SDL_Color colors[256];
300  int ncolors;
301
302  palette = screen->format->palette;
303
304  if (palette == NULL) {
305     return; // Display is not palettised
306  }
307
308  ncolors = palette->ncolors;
309  memset(colors, 0, ncolors * sizeof(SDL_Color));
310
311  SDL_SetColors(screen, colors, 0, 256);
312
313  gr_palette_faded_out = 1;
314 }
315
316
317 void gr_palette_step_up( int r, int g, int b )
318 {
319  int i;
320  ubyte *p = gr_palette;
321  int temp;
322
323  SDL_Palette *palette;
324  SDL_Color colors[256];
325
326  if (gr_palette_faded_out) return;
327
328  if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
329
330  last_r = r;
331  last_g = g;
332  last_b = b;
333
334  palette = screen->format->palette;
335
336  if (palette == NULL) {
337     return; // Display is not palettised
338  }
339
340  for (i=0; i<256; i++) {
341    temp = (int)(*p++) + r + gr_palette_gamma;
342    if (temp<0) temp=0;
343    else if (temp>63) temp=63;
344    colors[i].r = temp * 4;
345    temp = (int)(*p++) + g + gr_palette_gamma;
346    if (temp<0) temp=0;
347    else if (temp>63) temp=63;
348    colors[i].g = temp * 4;
349    temp = (int)(*p++) + b + gr_palette_gamma;
350    if (temp<0) temp=0;
351    else if (temp>63) temp=63;
352    colors[i].b = temp * 4;
353  }
354
355  SDL_SetColors(screen, colors, 0, 256);
356 }
357
358 //added on 980913 by adb to fix palette problems
359 // need a min without side effects...
360 #undef min
361 static inline int min(int x, int y) { return x < y ? x : y; }
362 //end changes by adb
363
364 void gr_palette_load( ubyte *pal )      
365 {
366  int i, j;
367  SDL_Palette *palette;
368  SDL_Color colors[256];
369
370  for (i=0; i<768; i++ ) {
371      gr_current_pal[i] = pal[i];
372      if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
373  }
374
375  palette = screen->format->palette;
376
377  if (palette == NULL) {
378     return; // Display is not palettised
379  }
380
381  for (i = 0, j = 0; j < 256; j++) {
382      //changed on 980913 by adb to fix palette problems
383      colors[j].r = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
384      colors[j].g = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
385      colors[j].b = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
386      //end changes by adb
387  }
388  SDL_SetColors(screen, colors, 0, 256);
389
390  gr_palette_faded_out = 0;
391  init_computed_colors();
392 }
393
394
395
396 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
397 {
398  int i, j, k;
399  ubyte c;
400  fix fade_palette[768];
401  fix fade_palette_delta[768];
402
403  SDL_Palette *palette;
404  SDL_Color fade_colors[256];
405
406  if (gr_palette_faded_out) return 0;
407
408 #if 1 //ifndef NDEBUG
409         if (grd_fades_disabled) {
410                 gr_palette_clear();
411                 return 0;
412         }
413 #endif
414
415  palette = screen->format->palette;
416  if (palette == NULL) {
417     return -1; // Display is not palettised
418  }
419
420  if (pal==NULL) pal=gr_current_pal;
421
422  for (i=0; i<768; i++ ) {
423      gr_current_pal[i] = pal[i];
424      fade_palette[i] = i2f(pal[i]);
425      fade_palette_delta[i] = fade_palette[i] / nsteps;
426  }
427  for (j=0; j<nsteps; j++ )      {
428      for (i=0, k = 0; k<256; k++ )      {
429          fade_palette[i] -= fade_palette_delta[i];
430          if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
431             fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
432          c = f2i(fade_palette[i]);
433          if (c > 63) c = 63;
434          fade_colors[k].r = c * 4;
435          i++;
436
437          fade_palette[i] -= fade_palette_delta[i];
438          if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
439             fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
440          c = f2i(fade_palette[i]);
441          if (c > 63) c = 63;
442          fade_colors[k].g = c * 4;
443          i++;
444
445          fade_palette[i] -= fade_palette_delta[i];
446          if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
447             fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
448          c = f2i(fade_palette[i]);
449          if (c > 63) c = 63;
450          fade_colors[k].b = c * 4;
451          i++;
452      }
453
454   SDL_SetColors(screen, fade_colors, 0, 256);
455  }
456
457  gr_palette_faded_out = 1;
458  return 0;
459 }
460
461
462
463 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
464 {
465  int i, j, k, ncolors;
466  ubyte c;
467  fix fade_palette[768];
468  fix fade_palette_delta[768];
469
470  SDL_Palette *palette;
471  SDL_Color fade_colors[256];
472
473  if (!gr_palette_faded_out) return 0;
474
475 #if 1 //ifndef NDEBUG
476         if (grd_fades_disabled) {
477                 gr_palette_load(pal);
478                 return 0;
479         }
480 #endif
481
482  palette = screen->format->palette;
483
484  if (palette == NULL) {
485     return -1; // Display is not palettised
486  }
487
488  ncolors = palette->ncolors;
489
490  for (i=0; i<768; i++ ) {
491      gr_current_pal[i] = pal[i];
492      fade_palette[i] = 0;
493      fade_palette_delta[i] = i2f(pal[i]) / nsteps;
494  }
495
496  for (j=0; j<nsteps; j++ )      {
497      for (i=0, k = 0; k<256; k++ )      {
498          fade_palette[i] += fade_palette_delta[i];
499          if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
500             fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
501          c = f2i(fade_palette[i]);
502          if (c > 63) c = 63;
503          fade_colors[k].r = c * 4;
504          i++;
505
506          fade_palette[i] += fade_palette_delta[i];
507          if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
508             fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
509          c = f2i(fade_palette[i]);
510          if (c > 63) c = 63;
511          fade_colors[k].g = c * 4;
512          i++;
513
514          fade_palette[i] += fade_palette_delta[i];
515          if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
516             fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
517          c = f2i(fade_palette[i]);
518          if (c > 63) c = 63;
519          fade_colors[k].b = c * 4;
520          i++;
521      }
522
523   SDL_SetColors(screen, fade_colors, 0, 256);
524  }
525  //added on 980913 by adb to fix palette problems
526  gr_palette_load(pal);
527  //end changes by adb
528
529  gr_palette_faded_out = 0;
530  return 0;
531 }
532
533
534
535 void gr_palette_read(ubyte * pal)
536 {
537  SDL_Palette *palette;
538  int i, j;
539
540  palette = screen->format->palette;
541
542  if (palette == NULL) {
543     return; // Display is not palettised
544  }
545
546  for (i = 0, j=0; i < 256; i++) {
547      pal[j++] = palette->colors[i].r / 4;
548      pal[j++] = palette->colors[i].g / 4;
549      pal[j++] = palette->colors[i].b / 4;
550  }
551 }