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);
207 memset( grd_curscreen, 0, sizeof(grs_screen));
208 grd_curscreen->sc_mode = mode;
209 grd_curscreen->sc_w = w;
210 grd_curscreen->sc_h = h;
211 grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
212 grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
213 grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
214 grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
215 grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
216 grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
217 grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_LINEAR;
218 grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
219 gr_set_current_canvas(NULL);
220 //gr_enable_default_palette_loading();
222 //added on 9/30/98 by Matt Mueller to hide the mouse if its over the game window
225 //--moved up--added on 9/30/98 by Matt Mueller to set the title bar. Woohoo!
226 //--moved up-- SDL_WM_SetCaption(DESCENT_VERSION " " D1X_DATE, NULL);
227 //--moved up--end addition -MM
229 // gamefont_choose_game_font(w,h);
233 int gr_check_fullscreen(void){
234 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
237 int gr_toggle_fullscreen(void){
238 sdl_video_flags^=SDL_FULLSCREEN;
239 SDL_WM_ToggleFullScreen(screen);
240 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
245 // Only do this function once!
249 if (SDL_Init(SDL_INIT_VIDEO) < 0)
251 Error("SDL library video initialisation failed: %s.",SDL_GetError());
253 MALLOC( grd_curscreen,grs_screen,1 );
254 memset( grd_curscreen, 0, sizeof(grs_screen));
256 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
257 if (FindArg("-fullscreen"))
258 sdl_video_flags|=SDL_FULLSCREEN;
260 //added 05/19/99 Matt Mueller - make HW surface optional
261 if (FindArg("-hwsurface"))
262 sdl_video_flags|=SDL_HWSURFACE;
265 grd_curscreen->sc_canvas.cv_color = 0;
266 grd_curscreen->sc_canvas.cv_drawmode = 0;
267 grd_curscreen->sc_canvas.cv_font = NULL;
268 grd_curscreen->sc_canvas.cv_font_fg_color = 0;
269 grd_curscreen->sc_canvas.cv_font_bg_color = 0;
270 gr_set_current_canvas( &grd_curscreen->sc_canvas );
273 // added on 980913 by adb to add cleanup
275 // end changes by adb
285 d_free(grd_curscreen);
289 // Palette functions follow.
291 static int last_r=0, last_g=0, last_b=0;
293 void gr_palette_clear()
295 SDL_Palette *palette;
296 SDL_Color colors[256];
299 palette = screen->format->palette;
301 if (palette == NULL) {
302 return; // Display is not palettised
305 ncolors = palette->ncolors;
306 memset(colors, 0, ncolors * sizeof(SDL_Color));
308 SDL_SetColors(screen, colors, 0, 256);
310 gr_palette_faded_out = 1;
314 void gr_palette_step_up( int r, int g, int b )
317 ubyte *p = gr_palette;
320 SDL_Palette *palette;
321 SDL_Color colors[256];
323 if (gr_palette_faded_out) return;
325 if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
331 palette = screen->format->palette;
333 if (palette == NULL) {
334 return; // Display is not palettised
337 for (i=0; i<256; i++) {
338 temp = (int)(*p++) + r + gr_palette_gamma;
340 else if (temp>63) temp=63;
341 colors[i].r = temp * 4;
342 temp = (int)(*p++) + g + gr_palette_gamma;
344 else if (temp>63) temp=63;
345 colors[i].g = temp * 4;
346 temp = (int)(*p++) + b + gr_palette_gamma;
348 else if (temp>63) temp=63;
349 colors[i].b = temp * 4;
352 SDL_SetColors(screen, colors, 0, 256);
355 //added on 980913 by adb to fix palette problems
356 // need a min without side effects...
358 static inline int min(int x, int y) { return x < y ? x : y; }
361 void gr_palette_load( ubyte *pal )
364 SDL_Palette *palette;
365 SDL_Color colors[256];
367 for (i=0; i<768; i++ ) {
368 gr_current_pal[i] = pal[i];
369 if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
372 palette = screen->format->palette;
374 if (palette == NULL) {
375 return; // Display is not palettised
378 for (i = 0, j = 0; j < 256; j++) {
379 //changed on 980913 by adb to fix palette problems
380 colors[j].r = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
381 colors[j].g = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
382 colors[j].b = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
385 SDL_SetColors(screen, colors, 0, 256);
387 gr_palette_faded_out = 0;
388 init_computed_colors();
393 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
397 fix fade_palette[768];
398 fix fade_palette_delta[768];
400 SDL_Palette *palette;
401 SDL_Color fade_colors[256];
403 if (gr_palette_faded_out) return 0;
405 #if 1 //ifndef NDEBUG
406 if (grd_fades_disabled) {
412 palette = screen->format->palette;
413 if (palette == NULL) {
414 return -1; // Display is not palettised
417 if (pal==NULL) pal=gr_current_pal;
419 for (i=0; i<768; i++ ) {
420 gr_current_pal[i] = pal[i];
421 fade_palette[i] = i2f(pal[i]);
422 fade_palette_delta[i] = fade_palette[i] / nsteps;
424 for (j=0; j<nsteps; j++ ) {
425 for (i=0, k = 0; k<256; k++ ) {
426 fade_palette[i] -= fade_palette_delta[i];
427 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
428 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
429 c = f2i(fade_palette[i]);
431 fade_colors[k].r = c * 4;
434 fade_palette[i] -= fade_palette_delta[i];
435 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
436 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
437 c = f2i(fade_palette[i]);
439 fade_colors[k].g = c * 4;
442 fade_palette[i] -= fade_palette_delta[i];
443 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
444 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
445 c = f2i(fade_palette[i]);
447 fade_colors[k].b = c * 4;
451 SDL_SetColors(screen, fade_colors, 0, 256);
454 gr_palette_faded_out = 1;
460 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
462 int i, j, k, ncolors;
464 fix fade_palette[768];
465 fix fade_palette_delta[768];
467 SDL_Palette *palette;
468 SDL_Color fade_colors[256];
470 if (!gr_palette_faded_out) return 0;
472 #if 1 //ifndef NDEBUG
473 if (grd_fades_disabled) {
474 gr_palette_load(pal);
479 palette = screen->format->palette;
481 if (palette == NULL) {
482 return -1; // Display is not palettised
485 ncolors = palette->ncolors;
487 for (i=0; i<768; i++ ) {
488 gr_current_pal[i] = pal[i];
490 fade_palette_delta[i] = i2f(pal[i]) / nsteps;
493 for (j=0; j<nsteps; j++ ) {
494 for (i=0, k = 0; k<256; k++ ) {
495 fade_palette[i] += fade_palette_delta[i];
496 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
497 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
498 c = f2i(fade_palette[i]);
500 fade_colors[k].r = c * 4;
503 fade_palette[i] += fade_palette_delta[i];
504 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
505 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
506 c = f2i(fade_palette[i]);
508 fade_colors[k].g = c * 4;
511 fade_palette[i] += fade_palette_delta[i];
512 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
513 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
514 c = f2i(fade_palette[i]);
516 fade_colors[k].b = c * 4;
520 SDL_SetColors(screen, fade_colors, 0, 256);
522 //added on 980913 by adb to fix palette problems
523 gr_palette_load(pal);
526 gr_palette_faded_out = 0;
532 void gr_palette_read(ubyte * pal)
534 SDL_Palette *palette;
537 palette = screen->format->palette;
539 if (palette == NULL) {
540 return; // Display is not palettised
543 for (i = 0, j=0; i < 256; i++) {
544 pal[j++] = palette->colors[i].r / 4;
545 pal[j++] = palette->colors[i].g / 4;
546 pal[j++] = palette->colors[i].b / 4;