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