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