16 #include <SDL_image.h>
26 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
29 #ifdef _WIN32_WCE // should really be checking for "Pocket PC" somehow
33 int sdl_video_flags = SDL_SWSURFACE | SDL_HWPALETTE;
38 static SDL_Surface *real_screen, *screen2;
43 //added 05/19/99 Matt Mueller - locking stuff
47 int gr_testlocklevel=0;
49 inline void gr_dolock(const char *file,int line) {
51 if ( gr_testlocklevel==1 && SDL_MUSTLOCK(screen) ) {
53 chcksetwritable(screen.pixels,screen->w*screen->h*screen->format->BytesPerPixel);
55 if ( SDL_LockSurface(screen) < 0 )Error("could not lock screen (%s:%i)\n",file,line);
58 inline void gr_dounlock(void) {
60 if (gr_testlocklevel==0 && SDL_MUSTLOCK(screen) ) {
61 SDL_UnlockSurface(screen);
63 chcksetunwritable(screen.pixels,screen->w*screen->h*screen->format->BytesPerPixel);
71 /* Create a new rotated surface for drawing */
72 SDL_Surface *CreateRotatedSurface(SDL_Surface *s)
75 return(SDL_CreateRGBSurface(s->flags, s->h, s->w,
76 s->format->BitsPerPixel,
82 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);
123 void gr_palette_clear(); // Function prototype for gr_init;
128 //added 05/19/99 Matt Mueller - locking stuff
132 screen2 = SDL_DisplayFormat(screen);
133 BlitRotatedSurface(screen2, real_screen);
134 //SDL_SetColors(real_screen, screen->format->palette->colors, 0, 256);
135 SDL_UpdateRect(real_screen, 0, 0, 0, 0);
136 SDL_FreeSurface(screen2);
138 SDL_UpdateRect(screen, 0, 0, 0, 0);
143 int gr_check_mode(uint32_t mode)
150 return !SDL_VideoModeOK(w, h, 8, sdl_video_flags);
154 extern int VGA_current_mode; // DPH: kludge - remove at all costs
156 int gr_set_mode(uint32_t mode)
167 if (mode == VGA_current_mode)
172 VGA_current_mode = mode;
174 if (screen != NULL) gr_palette_clear();
176 //added on 11/06/98 by Matt Mueller to set the title bar. (moved from below)
177 //sekmu: might wanna copy this litte blurb to one of the text files or something
178 //we want to set it here so that X window manager "Style" type commands work
179 //for example, in fvwm2 or fvwm95:
180 //Style "D1X*" NoTitle, NoHandles, BorderWidth 0
181 //if you can't use -fullscreen like me (crashes X), this is a big help in
182 //getting the window centered correctly (if you use SmartPlacement)
183 SDL_WM_SetCaption(PACKAGE_STRING, "Descent II");
188 #include "descent.xpm"
189 SDL_WM_SetIcon(IMG_ReadXPMFromArray(pixmap), NULL);
193 //edited 10/05/98 by Matt Mueller - make fullscreen mode optional
194 // changed by adb on 980913: added SDL_HWPALETTE (should be option?)
195 // changed by someone on 980923 to add SDL_FULLSCREEN
198 real_screen = SDL_SetVideoMode(h, w, 0, sdl_video_flags);
199 screen = CreateRotatedSurface(real_screen);
201 screen = SDL_SetVideoMode(w, h, 8, sdl_video_flags);
203 // end changes by someone
204 // end changes by adb
206 if (screen == NULL) {
207 Error("Could not set %dx%dx8 video mode\n",w,h);
209 memset( grd_curscreen, 0, sizeof(grs_screen));
210 grd_curscreen->sc_mode = mode;
211 grd_curscreen->sc_w = w;
212 grd_curscreen->sc_h = h;
213 grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
214 grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
215 grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
216 grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
217 grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
218 grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
219 grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_LINEAR;
220 grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
221 gr_set_current_canvas(NULL);
222 //gr_enable_default_palette_loading();
224 //added on 9/30/98 by Matt Mueller to hide the mouse if its over the game window
227 //--moved up--added on 9/30/98 by Matt Mueller to set the title bar. Woohoo!
228 //--moved up-- SDL_WM_SetCaption(DESCENT_VERSION " " D1X_DATE, NULL);
229 //--moved up--end addition -MM
231 // gamefont_choose_game_font(w,h);
235 int gr_check_fullscreen(void){
236 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
239 int gr_toggle_fullscreen(void){
240 sdl_video_flags^=SDL_FULLSCREEN;
241 SDL_WM_ToggleFullScreen(screen);
242 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
247 // Only do this function once!
251 if (SDL_Init(SDL_INIT_VIDEO) < 0)
253 Error("SDL library video initialisation failed: %s.",SDL_GetError());
255 MALLOC( grd_curscreen,grs_screen,1 );
256 memset( grd_curscreen, 0, sizeof(grs_screen));
258 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
259 if (FindArg("-fullscreen"))
260 sdl_video_flags|=SDL_FULLSCREEN;
262 //added 05/19/99 Matt Mueller - make HW surface optional
263 if (FindArg("-hwsurface"))
264 sdl_video_flags|=SDL_HWSURFACE;
267 grd_curscreen->sc_canvas.cv_color = 0;
268 grd_curscreen->sc_canvas.cv_drawmode = 0;
269 grd_curscreen->sc_canvas.cv_font = NULL;
270 grd_curscreen->sc_canvas.cv_font_fg_color = 0;
271 grd_curscreen->sc_canvas.cv_font_bg_color = 0;
272 gr_set_current_canvas( &grd_curscreen->sc_canvas );
275 // added on 980913 by adb to add cleanup
277 // end changes by adb
287 d_free(grd_curscreen);
291 // Palette functions follow.
293 static int last_r=0, last_g=0, last_b=0;
295 void gr_palette_clear()
297 SDL_Palette *palette;
298 SDL_Color colors[256];
301 palette = screen->format->palette;
303 if (palette == NULL) {
304 return; // Display is not palettised
307 ncolors = palette->ncolors;
308 memset(colors, 0, ncolors * sizeof(SDL_Color));
310 SDL_SetColors(screen, colors, 0, 256);
312 gr_palette_faded_out = 1;
316 void gr_palette_step_up( int r, int g, int b )
319 ubyte *p = gr_palette;
322 SDL_Palette *palette;
323 SDL_Color colors[256];
325 if (gr_palette_faded_out) return;
327 if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
333 palette = screen->format->palette;
335 if (palette == NULL) {
336 return; // Display is not palettised
339 for (i=0; i<256; i++) {
340 temp = (int)(*p++) + r + gr_palette_gamma;
342 else if (temp>63) temp=63;
343 colors[i].r = temp * 4;
344 temp = (int)(*p++) + g + gr_palette_gamma;
346 else if (temp>63) temp=63;
347 colors[i].g = temp * 4;
348 temp = (int)(*p++) + b + gr_palette_gamma;
350 else if (temp>63) temp=63;
351 colors[i].b = temp * 4;
354 SDL_SetColors(screen, colors, 0, 256);
357 //added on 980913 by adb to fix palette problems
358 // need a min without side effects...
360 static inline int min(int x, int y) { return x < y ? x : y; }
363 void gr_palette_load( ubyte *pal )
366 SDL_Palette *palette;
367 SDL_Color colors[256];
369 for (i=0; i<768; i++ ) {
370 gr_current_pal[i] = pal[i];
371 if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
374 palette = screen->format->palette;
376 if (palette == NULL) {
377 return; // Display is not palettised
380 for (i = 0, j = 0; j < 256; j++) {
381 //changed on 980913 by adb to fix palette problems
382 colors[j].r = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
383 colors[j].g = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
384 colors[j].b = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
387 SDL_SetColors(screen, colors, 0, 256);
389 gr_palette_faded_out = 0;
390 init_computed_colors();
395 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
399 fix fade_palette[768];
400 fix fade_palette_delta[768];
402 SDL_Palette *palette;
403 SDL_Color fade_colors[256];
405 if (gr_palette_faded_out) return 0;
407 #if 1 //ifndef NDEBUG
408 if (grd_fades_disabled) {
414 palette = screen->format->palette;
415 if (palette == NULL) {
416 return -1; // Display is not palettised
419 if (pal==NULL) pal=gr_current_pal;
421 for (i=0; i<768; i++ ) {
422 gr_current_pal[i] = pal[i];
423 fade_palette[i] = i2f(pal[i]);
424 fade_palette_delta[i] = fade_palette[i] / nsteps;
426 for (j=0; j<nsteps; j++ ) {
427 for (i=0, k = 0; k<256; k++ ) {
428 fade_palette[i] -= fade_palette_delta[i];
429 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
430 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
431 c = f2i(fade_palette[i]);
433 fade_colors[k].r = c * 4;
436 fade_palette[i] -= fade_palette_delta[i];
437 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
438 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
439 c = f2i(fade_palette[i]);
441 fade_colors[k].g = c * 4;
444 fade_palette[i] -= fade_palette_delta[i];
445 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
446 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
447 c = f2i(fade_palette[i]);
449 fade_colors[k].b = c * 4;
453 SDL_SetColors(screen, fade_colors, 0, 256);
456 gr_palette_faded_out = 1;
462 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
464 int i, j, k, ncolors;
466 fix fade_palette[768];
467 fix fade_palette_delta[768];
469 SDL_Palette *palette;
470 SDL_Color fade_colors[256];
472 if (!gr_palette_faded_out) return 0;
474 #if 1 //ifndef NDEBUG
475 if (grd_fades_disabled) {
476 gr_palette_load(pal);
481 palette = screen->format->palette;
483 if (palette == NULL) {
484 return -1; // Display is not palettised
487 ncolors = palette->ncolors;
489 for (i=0; i<768; i++ ) {
490 gr_current_pal[i] = pal[i];
492 fade_palette_delta[i] = i2f(pal[i]) / nsteps;
495 for (j=0; j<nsteps; j++ ) {
496 for (i=0, k = 0; k<256; k++ ) {
497 fade_palette[i] += fade_palette_delta[i];
498 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
499 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
500 c = f2i(fade_palette[i]);
502 fade_colors[k].r = c * 4;
505 fade_palette[i] += fade_palette_delta[i];
506 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
507 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
508 c = f2i(fade_palette[i]);
510 fade_colors[k].g = c * 4;
513 fade_palette[i] += fade_palette_delta[i];
514 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
515 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
516 c = f2i(fade_palette[i]);
518 fade_colors[k].b = c * 4;
522 SDL_SetColors(screen, fade_colors, 0, 256);
524 //added on 980913 by adb to fix palette problems
525 gr_palette_load(pal);
528 gr_palette_faded_out = 0;
534 void gr_palette_read(ubyte * pal)
536 SDL_Palette *palette;
539 palette = screen->format->palette;
541 if (palette == NULL) {
542 return; // Display is not palettised
545 for (i = 0, j=0; i < 256; i++) {
546 pal[j++] = palette->colors[i].r / 4;
547 pal[j++] = palette->colors[i].g / 4;
548 pal[j++] = palette->colors[i].b / 4;