1 /* $Id: gr.c,v 1.13 2003-11-26 21:08:59 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;
35 char checkvidmodeok=0;
40 static SDL_Surface *real_screen;
45 //added 05/19/99 Matt Mueller - locking stuff
49 int gr_testlocklevel=0;
51 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 )Error("could not lock screen (%s:%i)\n",file,line);
60 inline void gr_dounlock(void) {
62 if (gr_testlocklevel==0 && SDL_MUSTLOCK(screen) ) {
63 SDL_UnlockSurface(screen);
65 chcksetunwritable(screen.pixels,screen->w*screen->h*screen->format->BytesPerPixel);
73 /* Create a new rotated surface for drawing */
74 SDL_Surface *CreateRotatedSurface(SDL_Surface *s)
76 return(SDL_CreateRGBSurface(s->flags, s->h, s->w,
77 s->format->BitsPerPixel,
84 /* Used to copy the rotated scratch surface to the screen */
85 void BlitRotatedSurface(SDL_Surface *from, SDL_Surface *to)
88 int bpp = from->format->BytesPerPixel;
89 int w=from->w, h=from->h, pitch=to->pitch;
91 Uint8 *pfrom, *pto, *to0;
93 SDL_LockSurface(from);
95 pfrom=(Uint8 *)from->pixels;
96 to0=(Uint8 *) to->pixels+pitch*(w-1);
103 if (bpp==1) *pto=*pfrom;
104 else if (bpp==2) *(Uint16 *)pto=*(Uint16 *)pfrom;
105 else if (bpp==4) *(Uint32 *)pto=*(Uint32 *)pfrom;
116 SDL_UnlockSurface(from);
117 SDL_UnlockSurface(to);
121 void gr_palette_clear(); // Function prototype for gr_init;
126 //added 05/19/99 Matt Mueller - locking stuff
130 BlitRotatedSurface(screen, real_screen);
131 SDL_UpdateRect(real_screen, 0, 0, 0, 0);
133 SDL_UpdateRect(screen, 0, 0, 0, 0);
137 extern int VGA_current_mode; // DPH: kludge - remove at all costs
139 int gr_set_mode(u_int32_t mode)
152 VGA_current_mode = mode;
154 if (screen != NULL) gr_palette_clear();
156 //added on 11/06/98 by Matt Mueller to set the title bar. (moved from below)
157 //sekmu: might wanna copy this litte blurb to one of the text files or something
158 //we want to set it here so that X window manager "Style" type commands work
159 //for example, in fvwm2 or fvwm95:
160 //Style "D1X*" NoTitle, NoHandles, BorderWidth 0
161 //if you can't use -fullscreen like me (crashes X), this is a big help in
162 //getting the window centered correctly (if you use SmartPlacement)
163 SDL_WM_SetCaption(PACKAGE_STRING, "Descent II");
168 #include "descent.xpm"
169 SDL_WM_SetIcon(IMG_ReadXPMFromArray(pixmap), NULL);
173 //edited 10/05/98 by Matt Mueller - make fullscreen mode optional
174 // changed by adb on 980913: added SDL_HWPALETTE (should be option?)
175 // changed by someone on 980923 to add SDL_FULLSCREEN
177 if(!checkvidmodeok || SDL_VideoModeOK(w,h,0,sdl_video_flags)){
179 real_screen = SDL_SetVideoMode(h, w, 0, sdl_video_flags);
180 screen = CreateRotatedSurface(real_screen);
182 screen = SDL_SetVideoMode(w, h, 0, sdl_video_flags);
188 if(!checkvidmodeok || SDL_VideoModeOK(w,h,8,sdl_video_flags)){
189 screen = SDL_SetVideoMode(w, h, 8, sdl_video_flags);
194 // end changes by someone
195 // end changes by adb
197 if (screen == NULL) {
198 Error("Could not set %dx%dx8 video mode\n",w,h);
201 memset( grd_curscreen, 0, sizeof(grs_screen));
202 grd_curscreen->sc_mode = mode;
203 grd_curscreen->sc_w = w;
204 grd_curscreen->sc_h = h;
205 grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
206 grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
207 grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
208 grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
209 grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
210 grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
211 grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_LINEAR;
212 grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
213 gr_set_current_canvas(NULL);
214 //gr_enable_default_palette_loading();
216 //added on 9/30/98 by Matt Mueller to hide the mouse if its over the game window
219 //--moved up--added on 9/30/98 by Matt Mueller to set the title bar. Woohoo!
220 //--moved up-- SDL_WM_SetCaption(DESCENT_VERSION " " D1X_DATE, NULL);
221 //--moved up--end addition -MM
223 // gamefont_choose_game_font(w,h);
227 int gr_check_fullscreen(void){
228 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
231 int gr_toggle_fullscreen(void){
232 sdl_video_flags^=SDL_FULLSCREEN;
233 SDL_WM_ToggleFullScreen(screen);
234 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
239 // Only do this function once!
243 if (SDL_Init(SDL_INIT_VIDEO) < 0)
245 Error("SDL library video initialisation failed: %s.",SDL_GetError());
247 MALLOC( grd_curscreen,grs_screen,1 );
248 memset( grd_curscreen, 0, sizeof(grs_screen));
250 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
251 if (FindArg("-fullscreen"))
252 sdl_video_flags|=SDL_FULLSCREEN;
254 //added 05/19/99 Matt Mueller - make HW surface optional
255 if (FindArg("-hwsurface"))
256 sdl_video_flags|=SDL_HWSURFACE;
258 if (FindArg("-nosdlvidmodecheck"))
261 grd_curscreen->sc_canvas.cv_color = 0;
262 grd_curscreen->sc_canvas.cv_drawmode = 0;
263 grd_curscreen->sc_canvas.cv_font = NULL;
264 grd_curscreen->sc_canvas.cv_font_fg_color = 0;
265 grd_curscreen->sc_canvas.cv_font_bg_color = 0;
266 gr_set_current_canvas( &grd_curscreen->sc_canvas );
269 // added on 980913 by adb to add cleanup
271 // end changes by adb
281 d_free(grd_curscreen);
285 // Palette functions follow.
287 static int last_r=0, last_g=0, last_b=0;
289 void gr_palette_clear()
291 SDL_Palette *palette;
292 SDL_Color colors[256];
295 palette = screen->format->palette;
297 if (palette == NULL) {
298 return; // Display is not palettised
301 ncolors = palette->ncolors;
302 memset(colors, 0, ncolors * sizeof(SDL_Color));
304 SDL_SetColors(screen, colors, 0, 256);
306 gr_palette_faded_out = 1;
310 void gr_palette_step_up( int r, int g, int b )
313 ubyte *p = gr_palette;
316 SDL_Palette *palette;
317 SDL_Color colors[256];
319 if (gr_palette_faded_out) return;
321 if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
327 palette = screen->format->palette;
329 if (palette == NULL) {
330 return; // Display is not palettised
333 for (i=0; i<256; i++) {
334 temp = (int)(*p++) + r + gr_palette_gamma;
336 else if (temp>63) temp=63;
337 colors[i].r = temp * 4;
338 temp = (int)(*p++) + g + gr_palette_gamma;
340 else if (temp>63) temp=63;
341 colors[i].g = temp * 4;
342 temp = (int)(*p++) + b + gr_palette_gamma;
344 else if (temp>63) temp=63;
345 colors[i].b = temp * 4;
348 SDL_SetColors(screen, colors, 0, 256);
351 //added on 980913 by adb to fix palette problems
352 // need a min without side effects...
354 static inline int min(int x, int y) { return x < y ? x : y; }
357 void gr_palette_load( ubyte *pal )
360 SDL_Palette *palette;
361 SDL_Color colors[256];
363 for (i=0; i<768; i++ ) {
364 gr_current_pal[i] = pal[i];
365 if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
368 palette = screen->format->palette;
370 if (palette == NULL) {
371 return; // Display is not palettised
374 for (i = 0, j = 0; j < 256; j++) {
375 //changed on 980913 by adb to fix palette problems
376 colors[j].r = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
377 colors[j].g = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
378 colors[j].b = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
381 SDL_SetColors(screen, colors, 0, 256);
383 gr_palette_faded_out = 0;
384 init_computed_colors();
389 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
393 fix fade_palette[768];
394 fix fade_palette_delta[768];
396 SDL_Palette *palette;
397 SDL_Color fade_colors[256];
399 if (gr_palette_faded_out) return 0;
401 #if 1 //ifndef NDEBUG
402 if (grd_fades_disabled) {
408 palette = screen->format->palette;
409 if (palette == NULL) {
410 return -1; // Display is not palettised
413 if (pal==NULL) pal=gr_current_pal;
415 for (i=0; i<768; i++ ) {
416 gr_current_pal[i] = pal[i];
417 fade_palette[i] = i2f(pal[i]);
418 fade_palette_delta[i] = fade_palette[i] / nsteps;
420 for (j=0; j<nsteps; j++ ) {
421 for (i=0, k = 0; k<256; k++ ) {
422 fade_palette[i] -= fade_palette_delta[i];
423 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
424 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
425 c = f2i(fade_palette[i]);
427 fade_colors[k].r = c * 4;
430 fade_palette[i] -= fade_palette_delta[i];
431 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
432 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
433 c = f2i(fade_palette[i]);
435 fade_colors[k].g = c * 4;
438 fade_palette[i] -= fade_palette_delta[i];
439 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
440 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
441 c = f2i(fade_palette[i]);
443 fade_colors[k].b = c * 4;
447 SDL_SetColors(screen, fade_colors, 0, 256);
450 gr_palette_faded_out = 1;
456 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
458 int i, j, k, ncolors;
460 fix fade_palette[768];
461 fix fade_palette_delta[768];
463 SDL_Palette *palette;
464 SDL_Color fade_colors[256];
466 if (!gr_palette_faded_out) return 0;
468 #if 1 //ifndef NDEBUG
469 if (grd_fades_disabled) {
470 gr_palette_load(pal);
475 palette = screen->format->palette;
477 if (palette == NULL) {
478 return -1; // Display is not palettised
481 ncolors = palette->ncolors;
483 for (i=0; i<768; i++ ) {
484 gr_current_pal[i] = pal[i];
486 fade_palette_delta[i] = i2f(pal[i]) / nsteps;
489 for (j=0; j<nsteps; j++ ) {
490 for (i=0, k = 0; k<256; k++ ) {
491 fade_palette[i] += fade_palette_delta[i];
492 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
493 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
494 c = f2i(fade_palette[i]);
496 fade_colors[k].r = c * 4;
499 fade_palette[i] += fade_palette_delta[i];
500 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
501 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
502 c = f2i(fade_palette[i]);
504 fade_colors[k].g = c * 4;
507 fade_palette[i] += fade_palette_delta[i];
508 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
509 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
510 c = f2i(fade_palette[i]);
512 fade_colors[k].b = c * 4;
516 SDL_SetColors(screen, fade_colors, 0, 256);
518 //added on 980913 by adb to fix palette problems
519 gr_palette_load(pal);
522 gr_palette_faded_out = 0;
528 void gr_palette_read(ubyte * pal)
530 SDL_Palette *palette;
533 palette = screen->format->palette;
535 if (palette == NULL) {
536 return; // Display is not palettised
539 for (i = 0, j=0; i < 256; i++) {
540 pal[j++] = palette->colors[i].r / 4;
541 pal[j++] = palette->colors[i].g / 4;
542 pal[j++] = palette->colors[i].b / 4;