16 #include <SDL_image.h>
28 #ifdef _WIN32_WCE // should really be checking for "Pocket PC" somehow
32 int sdl_video_flags = SDL_SWSURFACE | SDL_HWPALETTE;
36 static SDL_Surface *real_screen, *screen2;
39 int vid_installed = 0;
41 //added 05/19/99 Matt Mueller - locking stuff
47 int gr_testlocklevel=0;
50 inline void gr_dolock(const char *file,int line)
53 if ( gr_testlocklevel == 1 && SDL_MUSTLOCK(screen) ) {
55 chcksetwritable(screen.pixels, screen->w * screen->h * screen->format->BytesPerPixel);
57 if ( SDL_LockSurface(screen) < 0 )
58 Error("could not lock screen (%s:%i)\n", file, line);
63 inline void gr_dounlock(void)
66 if ( gr_testlocklevel == 0 && SDL_MUSTLOCK(screen) ) {
67 SDL_UnlockSurface(screen);
69 chcksetunwritable(screen.pixels, screen->w * screen->h * screen->format->BytesPerPixel);
80 /* Create a new rotated surface for drawing */
81 SDL_Surface *CreateRotatedSurface(SDL_Surface *s)
83 return(SDL_CreateRGBSurface(s->flags, s->h, s->w, 8, 0, 0, 0, 0));
86 /* Used to copy the rotated scratch surface to the screen */
87 void BlitRotatedSurface(SDL_Surface *from, SDL_Surface *to)
90 int bpp = from->format->BytesPerPixel;
91 int w=from->w, h=from->h, pitch=to->pitch;
93 Uint8 *pfrom, *pto, *to0;
95 SDL_LockSurface(from);
97 pfrom=(Uint8 *)from->pixels;
98 to0=(Uint8 *) to->pixels+pitch*(w-1);
105 if (bpp==1) *pto=*pfrom;
106 else if (bpp==2) *(Uint16 *)pto=*(Uint16 *)pfrom;
107 else if (bpp==4) *(Uint32 *)pto=*(Uint32 *)pfrom;
118 SDL_UnlockSurface(from);
119 SDL_UnlockSurface(to);
128 screen2 = SDL_DisplayFormat(screen);
129 BlitRotatedSurface(screen2, real_screen);
130 //SDL_SetColors(real_screen, screen->format->palette->colors, 0, 256);
131 SDL_UpdateRect(real_screen, 0, 0, 0, 0);
132 SDL_FreeSurface(screen2);
134 SDL_UpdateRect(screen, 0, 0, 0, 0);
139 int vid_check_mode(uint32_t mode)
146 return !SDL_VideoModeOK(w, h, 8, sdl_video_flags);
150 uint32_t Vid_current_mode;
153 int vid_set_mode(uint32_t mode)
164 if (mode == Vid_current_mode)
169 Vid_current_mode = mode;
174 SDL_WM_SetCaption(PACKAGE_STRING, "Descent II");
178 #include "descent.xpm"
179 SDL_WM_SetIcon(IMG_ReadXPMFromArray(pixmap), NULL);
184 real_screen = SDL_SetVideoMode(h, w, 0, sdl_video_flags);
185 screen = CreateRotatedSurface(real_screen);
187 screen = SDL_SetVideoMode(w, h, 8, sdl_video_flags);
191 Error("Could not set %dx%dx8 video mode\n", w, h);
193 return gr_init_screen(BM_LINEAR, w, h, 0, 0, screen->pitch, (unsigned char *)screen->pixels);
197 int vid_check_fullscreen(void)
199 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
203 int vid_toggle_fullscreen(void)
205 sdl_video_flags ^= SDL_FULLSCREEN;
206 SDL_WM_ToggleFullScreen(screen);
207 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
213 // Only do this function once!
214 if (vid_installed == 1)
217 if (SDL_Init(SDL_INIT_VIDEO) < 0)
218 Error("SDL library video initialisation failed: %s.\n", SDL_GetError());
220 if (FindArg("-fullscreen"))
221 sdl_video_flags |= SDL_FULLSCREEN;
223 if (FindArg("-hwsurface"))
224 sdl_video_flags |= SDL_HWSURFACE;
236 if (vid_installed == 1)
241 // Palette functions follow.
243 static int last_r = 0, last_g = 0, last_b = 0;
246 void gr_palette_clear(void)
248 SDL_Palette *palette;
249 SDL_Color colors[256];
252 palette = screen->format->palette;
255 return; // Display is not palettised
257 ncolors = palette->ncolors;
258 memset(colors, 0, ncolors * sizeof(SDL_Color));
260 SDL_SetColors(screen, colors, 0, 256);
262 gr_palette_faded_out = 1;
266 void gr_palette_step_up( int r, int g, int b )
269 ubyte *p = gr_palette;
272 SDL_Palette *palette;
273 SDL_Color colors[256];
275 if (gr_palette_faded_out)
278 if ( (r == last_r) && (g == last_g) && (b == last_b) )
285 palette = screen->format->palette;
288 return; // Display is not palettised
290 for (i = 0; i < 256; i++) {
291 temp = (int)(*p++) + r + gr_palette_gamma.intval;
292 if (temp < 0) temp = 0;
293 else if (temp > 63) temp = 63;
294 colors[i].r = temp * 4;
296 temp = (int)(*p++) + g + gr_palette_gamma.intval;
297 if (temp < 0) temp = 0;
298 else if (temp > 63) temp = 63;
299 colors[i].g = temp * 4;
301 temp = (int)(*p++) + b + gr_palette_gamma.intval;
302 if (temp < 0) temp = 0;
303 else if (temp > 63) temp = 63;
304 colors[i].b = temp * 4;
307 SDL_SetColors(screen, colors, 0, 256);
311 // need a min without side effects...
313 static inline int min(int x, int y) { return x < y ? x : y; }
316 void gr_palette_load( ubyte *pal )
319 SDL_Palette *palette;
320 SDL_Color colors[256];
322 for (i = 0; i < 768; i++) {
323 gr_current_pal[i] = pal[i];
324 if (gr_current_pal[i] > 63)
325 gr_current_pal[i] = 63;
328 palette = screen->format->palette;
331 return; // Display is not palettised
333 for (i = 0, j = 0; j < 256; j++) {
334 colors[j].r = min(gr_current_pal[i++] + gr_palette_gamma.intval, 63) * 4;
335 colors[j].g = min(gr_current_pal[i++] + gr_palette_gamma.intval, 63) * 4;
336 colors[j].b = min(gr_current_pal[i++] + gr_palette_gamma.intval, 63) * 4;
339 SDL_SetColors(screen, colors, 0, 256);
341 gr_palette_faded_out = 0;
342 init_computed_colors();
346 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
350 fix fade_palette[768];
351 fix fade_palette_delta[768];
353 SDL_Palette *palette;
354 SDL_Color fade_colors[256];
356 if (gr_palette_faded_out)
359 #if 1 //ifndef NDEBUG
360 if (grd_fades_disabled) {
366 palette = screen->format->palette;
368 return -1; // Display is not palettised
371 pal = gr_current_pal;
373 for (i = 0; i < 768; i++) {
374 gr_current_pal[i] = pal[i];
375 fade_palette[i] = i2f(pal[i]);
376 fade_palette_delta[i] = fade_palette[i] / nsteps;
379 for (j = 0; j < nsteps; j++) {
380 for (i = 0, k = 0; k < 256; k++) {
381 fade_palette[i] -= fade_palette_delta[i];
382 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma.intval))
383 fade_palette[i] = i2f(pal[i] + gr_palette_gamma.intval);
384 c = f2i(fade_palette[i]);
386 fade_colors[k].r = c * 4;
389 fade_palette[i] -= fade_palette_delta[i];
390 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma.intval))
391 fade_palette[i] = i2f(pal[i] + gr_palette_gamma.intval);
392 c = f2i(fade_palette[i]);
394 fade_colors[k].g = c * 4;
397 fade_palette[i] -= fade_palette_delta[i];
398 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma.intval))
399 fade_palette[i] = i2f(pal[i] + gr_palette_gamma.intval);
400 c = f2i(fade_palette[i]);
402 fade_colors[k].b = c * 4;
406 SDL_SetColors(screen, fade_colors, 0, 256);
409 gr_palette_faded_out = 1;
411 SDL_FillRect(screen, NULL, 0);
417 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
419 int i, j, k, ncolors;
421 fix fade_palette[768];
422 fix fade_palette_delta[768];
424 SDL_Palette *palette;
425 SDL_Color fade_colors[256];
427 if (!gr_palette_faded_out)
430 #if 1 //ifndef NDEBUG
431 if (grd_fades_disabled) {
432 gr_palette_load(pal);
437 palette = screen->format->palette;
440 return -1; // Display is not palettised
442 ncolors = palette->ncolors;
444 for (i = 0; i < 768; i++) {
445 gr_current_pal[i] = pal[i];
447 fade_palette_delta[i] = i2f(pal[i]) / nsteps;
450 for (j = 0; j < nsteps; j++) {
451 for (i = 0, k = 0; k < 256; k++) {
452 fade_palette[i] += fade_palette_delta[i];
453 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma.intval))
454 fade_palette[i] = i2f(pal[i] + gr_palette_gamma.intval);
455 c = f2i(fade_palette[i]);
457 fade_colors[k].r = c * 4;
460 fade_palette[i] += fade_palette_delta[i];
461 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma.intval))
462 fade_palette[i] = i2f(pal[i] + gr_palette_gamma.intval);
463 c = f2i(fade_palette[i]);
465 fade_colors[k].g = c * 4;
468 fade_palette[i] += fade_palette_delta[i];
469 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma.intval))
470 fade_palette[i] = i2f(pal[i] + gr_palette_gamma.intval);
471 c = f2i(fade_palette[i]);
473 fade_colors[k].b = c * 4;
477 SDL_SetColors(screen, fade_colors, 0, 256);
480 gr_palette_load(pal);
482 gr_palette_faded_out = 0;
489 void gr_palette_read(ubyte * pal)
491 SDL_Palette *palette;
494 palette = screen->format->palette;
497 return; // Display is not palettised
499 for (i = 0, j=0; i < 256; i++) {
500 pal[j++] = palette->colors[i].r / 4;
501 pal[j++] = palette->colors[i].g / 4;
502 pal[j++] = palette->colors[i].b / 4;