16 #include <SDL_image.h>
27 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
30 #ifdef _WIN32_WCE // should really be checking for "Pocket PC" somehow
34 int sdl_video_flags = SDL_SWSURFACE | SDL_HWPALETTE;
39 static SDL_Surface *real_screen, *screen2;
42 int vid_installed = 0;
44 //added 05/19/99 Matt Mueller - locking stuff
48 int gr_testlocklevel=0;
50 inline void gr_dolock(const char *file,int line) {
52 if ( gr_testlocklevel==1 && SDL_MUSTLOCK(screen) ) {
54 chcksetwritable(screen.pixels,screen->w*screen->h*screen->format->BytesPerPixel);
56 if ( SDL_LockSurface(screen) < 0 )Error("could not lock screen (%s:%i)\n",file,line);
59 inline void gr_dounlock(void) {
61 if (gr_testlocklevel==0 && SDL_MUSTLOCK(screen) ) {
62 SDL_UnlockSurface(screen);
64 chcksetunwritable(screen.pixels,screen->w*screen->h*screen->format->BytesPerPixel);
72 /* Create a new rotated surface for drawing */
73 SDL_Surface *CreateRotatedSurface(SDL_Surface *s)
76 return(SDL_CreateRGBSurface(s->flags, s->h, s->w,
77 s->format->BitsPerPixel,
83 return(SDL_CreateRGBSurface(s->flags, s->h, s->w, 8, 0, 0, 0, 0));
87 /* Used to copy the rotated scratch surface to the screen */
88 void BlitRotatedSurface(SDL_Surface *from, SDL_Surface *to)
91 int bpp = from->format->BytesPerPixel;
92 int w=from->w, h=from->h, pitch=to->pitch;
94 Uint8 *pfrom, *pto, *to0;
96 SDL_LockSurface(from);
98 pfrom=(Uint8 *)from->pixels;
99 to0=(Uint8 *) to->pixels+pitch*(w-1);
106 if (bpp==1) *pto=*pfrom;
107 else if (bpp==2) *(Uint16 *)pto=*(Uint16 *)pfrom;
108 else if (bpp==4) *(Uint32 *)pto=*(Uint32 *)pfrom;
119 SDL_UnlockSurface(from);
120 SDL_UnlockSurface(to);
124 void gr_palette_clear(); // Function prototype for gr_init;
129 //added 05/19/99 Matt Mueller - locking stuff
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);
139 SDL_UpdateRect(screen, 0, 0, 0, 0);
144 int vid_check_mode(uint32_t mode)
151 return !SDL_VideoModeOK(w, h, 8, sdl_video_flags);
155 uint32_t Vid_current_mode;
158 int vid_set_mode(uint32_t mode)
169 if (mode == Vid_current_mode)
174 Vid_current_mode = mode;
176 if (screen != NULL) gr_palette_clear();
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");
190 #include "descent.xpm"
191 SDL_WM_SetIcon(IMG_ReadXPMFromArray(pixmap), NULL);
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
200 real_screen = SDL_SetVideoMode(h, w, 0, sdl_video_flags);
201 screen = CreateRotatedSurface(real_screen);
203 screen = SDL_SetVideoMode(w, h, 8, sdl_video_flags);
205 // end changes by someone
206 // end changes by adb
208 if (screen == NULL) {
209 Error("Could not set %dx%dx8 video mode\n",w,h);
212 return gr_init_screen(BM_LINEAR, w, h, 0, 0, screen->pitch, (unsigned char *)screen->pixels);
216 int vid_check_fullscreen(void)
218 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
222 int vid_toggle_fullscreen(void)
224 sdl_video_flags^=SDL_FULLSCREEN;
225 SDL_WM_ToggleFullScreen(screen);
226 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
232 // Only do this function once!
233 if (vid_installed == 1)
236 if (SDL_Init(SDL_INIT_VIDEO) < 0)
238 Error("SDL library video initialisation failed: %s.",SDL_GetError());
241 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
242 if (FindArg("-fullscreen"))
243 sdl_video_flags|=SDL_FULLSCREEN;
245 //added 05/19/99 Matt Mueller - make HW surface optional
246 if (FindArg("-hwsurface"))
247 sdl_video_flags|=SDL_HWSURFACE;
251 // added on 980913 by adb to add cleanup
253 // end changes by adb
261 if (vid_installed == 1)
265 // Palette functions follow.
267 static int last_r=0, last_g=0, last_b=0;
269 void gr_palette_clear()
271 SDL_Palette *palette;
272 SDL_Color colors[256];
275 palette = screen->format->palette;
277 if (palette == NULL) {
278 return; // Display is not palettised
281 ncolors = palette->ncolors;
282 memset(colors, 0, ncolors * sizeof(SDL_Color));
284 SDL_SetColors(screen, colors, 0, 256);
286 gr_palette_faded_out = 1;
290 void gr_palette_step_up( int r, int g, int b )
293 ubyte *p = gr_palette;
296 SDL_Palette *palette;
297 SDL_Color colors[256];
299 if (gr_palette_faded_out) return;
301 if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
307 palette = screen->format->palette;
309 if (palette == NULL) {
310 return; // Display is not palettised
313 for (i=0; i<256; i++) {
314 temp = (int)(*p++) + r + gr_palette_gamma.intval;
316 else if (temp>63) temp=63;
317 colors[i].r = temp * 4;
318 temp = (int)(*p++) + g + gr_palette_gamma.intval;
320 else if (temp>63) temp=63;
321 colors[i].g = temp * 4;
322 temp = (int)(*p++) + b + gr_palette_gamma.intval;
324 else if (temp>63) temp=63;
325 colors[i].b = temp * 4;
328 SDL_SetColors(screen, colors, 0, 256);
331 //added on 980913 by adb to fix palette problems
332 // need a min without side effects...
334 static inline int min(int x, int y) { return x < y ? x : y; }
337 void gr_palette_load( ubyte *pal )
340 SDL_Palette *palette;
341 SDL_Color colors[256];
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;
348 palette = screen->format->palette;
350 if (palette == NULL) {
351 return; // Display is not palettised
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;
361 SDL_SetColors(screen, colors, 0, 256);
363 gr_palette_faded_out = 0;
364 init_computed_colors();
369 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
373 fix fade_palette[768];
374 fix fade_palette_delta[768];
376 SDL_Palette *palette;
377 SDL_Color fade_colors[256];
379 if (gr_palette_faded_out) return 0;
381 #if 1 //ifndef NDEBUG
382 if (grd_fades_disabled) {
388 palette = screen->format->palette;
389 if (palette == NULL) {
390 return -1; // Display is not palettised
393 if (pal==NULL) pal=gr_current_pal;
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;
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]);
407 fade_colors[k].r = c * 4;
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]);
415 fade_colors[k].g = c * 4;
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]);
423 fade_colors[k].b = c * 4;
427 SDL_SetColors(screen, fade_colors, 0, 256);
430 gr_palette_faded_out = 1;
436 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
438 int i, j, k, ncolors;
440 fix fade_palette[768];
441 fix fade_palette_delta[768];
443 SDL_Palette *palette;
444 SDL_Color fade_colors[256];
446 if (!gr_palette_faded_out) return 0;
448 #if 1 //ifndef NDEBUG
449 if (grd_fades_disabled) {
450 gr_palette_load(pal);
455 palette = screen->format->palette;
457 if (palette == NULL) {
458 return -1; // Display is not palettised
461 ncolors = palette->ncolors;
463 for (i=0; i<768; i++ ) {
464 gr_current_pal[i] = pal[i];
466 fade_palette_delta[i] = i2f(pal[i]) / nsteps;
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]);
476 fade_colors[k].r = c * 4;
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]);
484 fade_colors[k].g = c * 4;
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]);
492 fade_colors[k].b = c * 4;
496 SDL_SetColors(screen, fade_colors, 0, 256);
498 //added on 980913 by adb to fix palette problems
499 gr_palette_load(pal);
502 gr_palette_faded_out = 0;
508 void gr_palette_read(ubyte * pal)
510 SDL_Palette *palette;
513 palette = screen->format->palette;
515 if (palette == NULL) {
516 return; // Display is not palettised
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;