1 /* $Id: gr.c,v 1.15 2003-11-27 09:10:52 btb Exp $ */
17 #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;
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 gr_check_mode(u_int32_t mode)
151 return !SDL_VideoModeOK(w, h, 8, sdl_video_flags);
155 extern int VGA_current_mode; // DPH: kludge - remove at all costs
157 int gr_set_mode(u_int32_t mode)
170 VGA_current_mode = mode;
172 if (screen != NULL) gr_palette_clear();
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");
186 #include "descent.xpm"
187 SDL_WM_SetIcon(IMG_ReadXPMFromArray(pixmap), NULL);
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
196 real_screen = SDL_SetVideoMode(h, w, 0, sdl_video_flags);
197 screen = CreateRotatedSurface(real_screen);
199 screen = SDL_SetVideoMode(w, h, 8, sdl_video_flags);
201 // end changes by someone
202 // end changes by adb
204 if (screen == NULL) {
205 Error("Could not set %dx%dx8 video mode\n",w,h);
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();
223 //added on 9/30/98 by Matt Mueller to hide the mouse if its over the game window
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
230 // gamefont_choose_game_font(w,h);
234 int gr_check_fullscreen(void){
235 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
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;
246 // Only do this function once!
250 if (SDL_Init(SDL_INIT_VIDEO) < 0)
252 Error("SDL library video initialisation failed: %s.",SDL_GetError());
254 MALLOC( grd_curscreen,grs_screen,1 );
255 memset( grd_curscreen, 0, sizeof(grs_screen));
257 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
258 if (FindArg("-fullscreen"))
259 sdl_video_flags|=SDL_FULLSCREEN;
261 //added 05/19/99 Matt Mueller - make HW surface optional
262 if (FindArg("-hwsurface"))
263 sdl_video_flags|=SDL_HWSURFACE;
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 );
274 // added on 980913 by adb to add cleanup
276 // end changes by adb
286 d_free(grd_curscreen);
290 // Palette functions follow.
292 static int last_r=0, last_g=0, last_b=0;
294 void gr_palette_clear()
296 SDL_Palette *palette;
297 SDL_Color colors[256];
300 palette = screen->format->palette;
302 if (palette == NULL) {
303 return; // Display is not palettised
306 ncolors = palette->ncolors;
307 memset(colors, 0, ncolors * sizeof(SDL_Color));
309 SDL_SetColors(screen, colors, 0, 256);
311 gr_palette_faded_out = 1;
315 void gr_palette_step_up( int r, int g, int b )
318 ubyte *p = gr_palette;
321 SDL_Palette *palette;
322 SDL_Color colors[256];
324 if (gr_palette_faded_out) return;
326 if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
332 palette = screen->format->palette;
334 if (palette == NULL) {
335 return; // Display is not palettised
338 for (i=0; i<256; i++) {
339 temp = (int)(*p++) + r + gr_palette_gamma;
341 else if (temp>63) temp=63;
342 colors[i].r = temp * 4;
343 temp = (int)(*p++) + g + gr_palette_gamma;
345 else if (temp>63) temp=63;
346 colors[i].g = temp * 4;
347 temp = (int)(*p++) + b + gr_palette_gamma;
349 else if (temp>63) temp=63;
350 colors[i].b = temp * 4;
353 SDL_SetColors(screen, colors, 0, 256);
356 //added on 980913 by adb to fix palette problems
357 // need a min without side effects...
359 static inline int min(int x, int y) { return x < y ? x : y; }
362 void gr_palette_load( ubyte *pal )
365 SDL_Palette *palette;
366 SDL_Color colors[256];
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;
373 palette = screen->format->palette;
375 if (palette == NULL) {
376 return; // Display is not palettised
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;
386 SDL_SetColors(screen, colors, 0, 256);
388 gr_palette_faded_out = 0;
389 init_computed_colors();
394 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
398 fix fade_palette[768];
399 fix fade_palette_delta[768];
401 SDL_Palette *palette;
402 SDL_Color fade_colors[256];
404 if (gr_palette_faded_out) return 0;
406 #if 1 //ifndef NDEBUG
407 if (grd_fades_disabled) {
413 palette = screen->format->palette;
414 if (palette == NULL) {
415 return -1; // Display is not palettised
418 if (pal==NULL) pal=gr_current_pal;
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;
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]);
432 fade_colors[k].r = c * 4;
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]);
440 fade_colors[k].g = c * 4;
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]);
448 fade_colors[k].b = c * 4;
452 SDL_SetColors(screen, fade_colors, 0, 256);
455 gr_palette_faded_out = 1;
461 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
463 int i, j, k, ncolors;
465 fix fade_palette[768];
466 fix fade_palette_delta[768];
468 SDL_Palette *palette;
469 SDL_Color fade_colors[256];
471 if (!gr_palette_faded_out) return 0;
473 #if 1 //ifndef NDEBUG
474 if (grd_fades_disabled) {
475 gr_palette_load(pal);
480 palette = screen->format->palette;
482 if (palette == NULL) {
483 return -1; // Display is not palettised
486 ncolors = palette->ncolors;
488 for (i=0; i<768; i++ ) {
489 gr_current_pal[i] = pal[i];
491 fade_palette_delta[i] = i2f(pal[i]) / nsteps;
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]);
501 fade_colors[k].r = c * 4;
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]);
509 fade_colors[k].g = c * 4;
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]);
517 fade_colors[k].b = c * 4;
521 SDL_SetColors(screen, fade_colors, 0, 256);
523 //added on 980913 by adb to fix palette problems
524 gr_palette_load(pal);
527 gr_palette_faded_out = 0;
533 void gr_palette_read(ubyte * pal)
535 SDL_Palette *palette;
538 palette = screen->format->palette;
540 if (palette == NULL) {
541 return; // Display is not palettised
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;