]> icculus.org git repositories - btb/d2x.git/blob - arch/sdl/gr.c
remove needless sleep on linux hmiplay startup (d1x r1.6)
[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
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            exit(1);
207         }
208         memset( grd_curscreen, 0, sizeof(grs_screen));
209         grd_curscreen->sc_mode = mode;
210         grd_curscreen->sc_w = w;
211         grd_curscreen->sc_h = h;
212         grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
213         grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
214         grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
215         grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
216         grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
217         grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
218         grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_LINEAR;
219         grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
220         gr_set_current_canvas(NULL);
221         //gr_enable_default_palette_loading();
222
223 //added on 9/30/98 by Matt Mueller to hide the mouse if its over the game window
224         SDL_ShowCursor(0);
225 //end addition -MM
226 //--moved up--added on 9/30/98 by Matt Mueller to set the title bar.  Woohoo!
227 //--moved up--  SDL_WM_SetCaption(DESCENT_VERSION " " D1X_DATE, NULL);
228 //--moved up--end addition -MM
229
230 //      gamefont_choose_game_font(w,h);
231         return 0;
232 }
233
234 int gr_check_fullscreen(void){
235         return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
236 }
237
238 int gr_toggle_fullscreen(void){
239         sdl_video_flags^=SDL_FULLSCREEN;
240         SDL_WM_ToggleFullScreen(screen);
241         return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
242 }
243
244 int gr_init(void)
245 {
246         // Only do this function once!
247         if (gr_installed==1)
248                 return -1;
249
250         if (SDL_Init(SDL_INIT_VIDEO) < 0)
251         {
252                 Error("SDL library video initialisation failed: %s.",SDL_GetError());
253         }
254         MALLOC( grd_curscreen,grs_screen,1 );
255         memset( grd_curscreen, 0, sizeof(grs_screen));
256
257 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
258         if (FindArg("-fullscreen"))
259              sdl_video_flags|=SDL_FULLSCREEN;
260 //end addition -MM
261         //added 05/19/99 Matt Mueller - make HW surface optional
262         if (FindArg("-hwsurface"))
263              sdl_video_flags|=SDL_HWSURFACE;
264         //end addition -MM
265
266         grd_curscreen->sc_canvas.cv_color = 0;
267         grd_curscreen->sc_canvas.cv_drawmode = 0;
268         grd_curscreen->sc_canvas.cv_font = NULL;
269         grd_curscreen->sc_canvas.cv_font_fg_color = 0;
270         grd_curscreen->sc_canvas.cv_font_bg_color = 0;
271         gr_set_current_canvas( &grd_curscreen->sc_canvas );
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;
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;
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;
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, 63)) * 4;
382      colors[j].g = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
383      colors[j].b = (min(gr_current_pal[i++] + gr_palette_gamma, 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) )
429             fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
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) )
437             fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
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) )
445             fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
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) )
498             fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
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) )
506             fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
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) )
514             fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
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 }