1 /* $Id: gr.c,v 1.14 2003-11-27 04:50:22 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;
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)
75 return(SDL_CreateRGBSurface(s->flags, s->h, s->w,
76 s->format->BitsPerPixel,
83 /* Used to copy the rotated scratch surface to the screen */
84 void BlitRotatedSurface(SDL_Surface *from, SDL_Surface *to)
87 int bpp = from->format->BytesPerPixel;
88 int w=from->w, h=from->h, pitch=to->pitch;
90 Uint8 *pfrom, *pto, *to0;
92 SDL_LockSurface(from);
94 pfrom=(Uint8 *)from->pixels;
95 to0=(Uint8 *) to->pixels+pitch*(w-1);
102 if (bpp==1) *pto=*pfrom;
103 else if (bpp==2) *(Uint16 *)pto=*(Uint16 *)pfrom;
104 else if (bpp==4) *(Uint32 *)pto=*(Uint32 *)pfrom;
115 SDL_UnlockSurface(from);
116 SDL_UnlockSurface(to);
120 void gr_palette_clear(); // Function prototype for gr_init;
125 //added 05/19/99 Matt Mueller - locking stuff
129 BlitRotatedSurface(screen, real_screen);
130 SDL_SetColors(real_screen, screen->format->palette->colors, 0, 256);
131 SDL_UpdateRect(real_screen, 0, 0, 0, 0);
133 SDL_UpdateRect(screen, 0, 0, 0, 0);
138 int gr_check_mode(u_int32_t mode)
145 return !SDL_VideoModeOK(w, h, 8, sdl_video_flags);
149 extern int VGA_current_mode; // DPH: kludge - remove at all costs
151 int gr_set_mode(u_int32_t mode)
164 VGA_current_mode = mode;
166 if (screen != NULL) gr_palette_clear();
168 //added on 11/06/98 by Matt Mueller to set the title bar. (moved from below)
169 //sekmu: might wanna copy this litte blurb to one of the text files or something
170 //we want to set it here so that X window manager "Style" type commands work
171 //for example, in fvwm2 or fvwm95:
172 //Style "D1X*" NoTitle, NoHandles, BorderWidth 0
173 //if you can't use -fullscreen like me (crashes X), this is a big help in
174 //getting the window centered correctly (if you use SmartPlacement)
175 SDL_WM_SetCaption(PACKAGE_STRING, "Descent II");
180 #include "descent.xpm"
181 SDL_WM_SetIcon(IMG_ReadXPMFromArray(pixmap), NULL);
185 //edited 10/05/98 by Matt Mueller - make fullscreen mode optional
186 // changed by adb on 980913: added SDL_HWPALETTE (should be option?)
187 // changed by someone on 980923 to add SDL_FULLSCREEN
190 real_screen = SDL_SetVideoMode(h, w, 8, sdl_video_flags);
191 screen = CreateRotatedSurface(real_screen);
193 screen = SDL_SetVideoMode(w, h, 8, sdl_video_flags);
195 // end changes by someone
196 // end changes by adb
198 if (screen == NULL) {
199 Error("Could not set %dx%dx8 video mode\n",w,h);
202 memset( grd_curscreen, 0, sizeof(grs_screen));
203 grd_curscreen->sc_mode = mode;
204 grd_curscreen->sc_w = w;
205 grd_curscreen->sc_h = h;
206 grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
207 grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
208 grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
209 grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
210 grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
211 grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
212 grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_LINEAR;
213 grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
214 gr_set_current_canvas(NULL);
215 //gr_enable_default_palette_loading();
217 //added on 9/30/98 by Matt Mueller to hide the mouse if its over the game window
220 //--moved up--added on 9/30/98 by Matt Mueller to set the title bar. Woohoo!
221 //--moved up-- SDL_WM_SetCaption(DESCENT_VERSION " " D1X_DATE, NULL);
222 //--moved up--end addition -MM
224 // gamefont_choose_game_font(w,h);
228 int gr_check_fullscreen(void){
229 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
232 int gr_toggle_fullscreen(void){
233 sdl_video_flags^=SDL_FULLSCREEN;
234 SDL_WM_ToggleFullScreen(screen);
235 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
240 // Only do this function once!
244 if (SDL_Init(SDL_INIT_VIDEO) < 0)
246 Error("SDL library video initialisation failed: %s.",SDL_GetError());
248 MALLOC( grd_curscreen,grs_screen,1 );
249 memset( grd_curscreen, 0, sizeof(grs_screen));
251 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
252 if (FindArg("-fullscreen"))
253 sdl_video_flags|=SDL_FULLSCREEN;
255 //added 05/19/99 Matt Mueller - make HW surface optional
256 if (FindArg("-hwsurface"))
257 sdl_video_flags|=SDL_HWSURFACE;
260 grd_curscreen->sc_canvas.cv_color = 0;
261 grd_curscreen->sc_canvas.cv_drawmode = 0;
262 grd_curscreen->sc_canvas.cv_font = NULL;
263 grd_curscreen->sc_canvas.cv_font_fg_color = 0;
264 grd_curscreen->sc_canvas.cv_font_bg_color = 0;
265 gr_set_current_canvas( &grd_curscreen->sc_canvas );
268 // added on 980913 by adb to add cleanup
270 // end changes by adb
280 d_free(grd_curscreen);
284 // Palette functions follow.
286 static int last_r=0, last_g=0, last_b=0;
288 void gr_palette_clear()
290 SDL_Palette *palette;
291 SDL_Color colors[256];
294 palette = screen->format->palette;
296 if (palette == NULL) {
297 return; // Display is not palettised
300 ncolors = palette->ncolors;
301 memset(colors, 0, ncolors * sizeof(SDL_Color));
303 SDL_SetColors(screen, colors, 0, 256);
305 gr_palette_faded_out = 1;
309 void gr_palette_step_up( int r, int g, int b )
312 ubyte *p = gr_palette;
315 SDL_Palette *palette;
316 SDL_Color colors[256];
318 if (gr_palette_faded_out) return;
320 if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
326 palette = screen->format->palette;
328 if (palette == NULL) {
329 return; // Display is not palettised
332 for (i=0; i<256; i++) {
333 temp = (int)(*p++) + r + gr_palette_gamma;
335 else if (temp>63) temp=63;
336 colors[i].r = temp * 4;
337 temp = (int)(*p++) + g + gr_palette_gamma;
339 else if (temp>63) temp=63;
340 colors[i].g = temp * 4;
341 temp = (int)(*p++) + b + gr_palette_gamma;
343 else if (temp>63) temp=63;
344 colors[i].b = temp * 4;
347 SDL_SetColors(screen, colors, 0, 256);
350 //added on 980913 by adb to fix palette problems
351 // need a min without side effects...
353 static inline int min(int x, int y) { return x < y ? x : y; }
356 void gr_palette_load( ubyte *pal )
359 SDL_Palette *palette;
360 SDL_Color colors[256];
362 for (i=0; i<768; i++ ) {
363 gr_current_pal[i] = pal[i];
364 if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
367 palette = screen->format->palette;
369 if (palette == NULL) {
370 return; // Display is not palettised
373 for (i = 0, j = 0; j < 256; j++) {
374 //changed on 980913 by adb to fix palette problems
375 colors[j].r = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
376 colors[j].g = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
377 colors[j].b = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
380 SDL_SetColors(screen, colors, 0, 256);
382 gr_palette_faded_out = 0;
383 init_computed_colors();
388 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
392 fix fade_palette[768];
393 fix fade_palette_delta[768];
395 SDL_Palette *palette;
396 SDL_Color fade_colors[256];
398 if (gr_palette_faded_out) return 0;
400 #if 1 //ifndef NDEBUG
401 if (grd_fades_disabled) {
407 palette = screen->format->palette;
408 if (palette == NULL) {
409 return -1; // Display is not palettised
412 if (pal==NULL) pal=gr_current_pal;
414 for (i=0; i<768; i++ ) {
415 gr_current_pal[i] = pal[i];
416 fade_palette[i] = i2f(pal[i]);
417 fade_palette_delta[i] = fade_palette[i] / nsteps;
419 for (j=0; j<nsteps; j++ ) {
420 for (i=0, k = 0; k<256; k++ ) {
421 fade_palette[i] -= fade_palette_delta[i];
422 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
423 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
424 c = f2i(fade_palette[i]);
426 fade_colors[k].r = c * 4;
429 fade_palette[i] -= fade_palette_delta[i];
430 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
431 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
432 c = f2i(fade_palette[i]);
434 fade_colors[k].g = c * 4;
437 fade_palette[i] -= fade_palette_delta[i];
438 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
439 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
440 c = f2i(fade_palette[i]);
442 fade_colors[k].b = c * 4;
446 SDL_SetColors(screen, fade_colors, 0, 256);
449 gr_palette_faded_out = 1;
455 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
457 int i, j, k, ncolors;
459 fix fade_palette[768];
460 fix fade_palette_delta[768];
462 SDL_Palette *palette;
463 SDL_Color fade_colors[256];
465 if (!gr_palette_faded_out) return 0;
467 #if 1 //ifndef NDEBUG
468 if (grd_fades_disabled) {
469 gr_palette_load(pal);
474 palette = screen->format->palette;
476 if (palette == NULL) {
477 return -1; // Display is not palettised
480 ncolors = palette->ncolors;
482 for (i=0; i<768; i++ ) {
483 gr_current_pal[i] = pal[i];
485 fade_palette_delta[i] = i2f(pal[i]) / nsteps;
488 for (j=0; j<nsteps; j++ ) {
489 for (i=0, k = 0; k<256; k++ ) {
490 fade_palette[i] += fade_palette_delta[i];
491 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
492 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
493 c = f2i(fade_palette[i]);
495 fade_colors[k].r = c * 4;
498 fade_palette[i] += fade_palette_delta[i];
499 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
500 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
501 c = f2i(fade_palette[i]);
503 fade_colors[k].g = c * 4;
506 fade_palette[i] += fade_palette_delta[i];
507 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
508 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
509 c = f2i(fade_palette[i]);
511 fade_colors[k].b = c * 4;
515 SDL_SetColors(screen, fade_colors, 0, 256);
517 //added on 980913 by adb to fix palette problems
518 gr_palette_load(pal);
521 gr_palette_faded_out = 0;
527 void gr_palette_read(ubyte * pal)
529 SDL_Palette *palette;
532 palette = screen->format->palette;
534 if (palette == NULL) {
535 return; // Display is not palettised
538 for (i = 0, j=0; i < 256; i++) {
539 pal[j++] = palette->colors[i].r / 4;
540 pal[j++] = palette->colors[i].g / 4;
541 pal[j++] = palette->colors[i].b / 4;