1 /* $Id: gr.c,v 1.11 2003-03-28 09:27:07 btb Exp $ */
17 #include <SDL_image.h>
27 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
30 int sdl_video_flags = SDL_SWSURFACE | SDL_HWPALETTE;
31 char checkvidmodeok=0;
38 //added 05/19/99 Matt Mueller - locking stuff
42 int gr_testlocklevel=0;
44 inline void gr_dolock(const char *file,int line) {
46 if ( gr_testlocklevel==1 && SDL_MUSTLOCK(screen) ) {
48 chcksetwritable(screen.pixels,screen->w*screen->h*screen->format->BytesPerPixel);
50 if ( SDL_LockSurface(screen) < 0 )Error("could not lock screen (%s:%i)\n",file,line);
53 inline void gr_dounlock(void) {
55 if (gr_testlocklevel==0 && SDL_MUSTLOCK(screen) ) {
56 SDL_UnlockSurface(screen);
58 chcksetunwritable(screen.pixels,screen->w*screen->h*screen->format->BytesPerPixel);
65 void gr_palette_clear(); // Function prototype for gr_init;
70 //added 05/19/99 Matt Mueller - locking stuff
73 SDL_UpdateRect(screen,0,0,0,0);
76 extern int VGA_current_mode; // DPH: kludge - remove at all costs
78 int gr_set_mode(u_int32_t mode)
91 VGA_current_mode = mode;
93 if (screen != NULL) gr_palette_clear();
95 //added on 11/06/98 by Matt Mueller to set the title bar. (moved from below)
96 //sekmu: might wanna copy this litte blurb to one of the text files or something
97 //we want to set it here so that X window manager "Style" type commands work
98 //for example, in fvwm2 or fvwm95:
99 //Style "D1X*" NoTitle, NoHandles, BorderWidth 0
100 //if you can't use -fullscreen like me (crashes X), this is a big help in
101 //getting the window centered correctly (if you use SmartPlacement)
102 SDL_WM_SetCaption(PACKAGE_STRING, "Descent II");
107 #include "descent.xpm"
108 SDL_WM_SetIcon(IMG_ReadXPMFromArray(pixmap), NULL);
112 //edited 10/05/98 by Matt Mueller - make fullscreen mode optional
113 // changed by adb on 980913: added SDL_HWPALETTE (should be option?)
114 // changed by someone on 980923 to add SDL_FULLSCREEN
115 if(!checkvidmodeok || SDL_VideoModeOK(w,h,8,sdl_video_flags)){
116 screen = SDL_SetVideoMode(w, h, 8, sdl_video_flags);
120 // end changes by someone
121 // end changes by adb
123 if (screen == NULL) {
124 Error("Could not set %dx%dx8 video mode\n",w,h);
127 memset( grd_curscreen, 0, sizeof(grs_screen));
128 grd_curscreen->sc_mode = mode;
129 grd_curscreen->sc_w = w;
130 grd_curscreen->sc_h = h;
131 grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
132 grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
133 grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
134 grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
135 grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
136 grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
137 grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_LINEAR;
138 grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
139 gr_set_current_canvas(NULL);
140 //gr_enable_default_palette_loading();
142 //added on 9/30/98 by Matt Mueller to hide the mouse if its over the game window
145 //--moved up--added on 9/30/98 by Matt Mueller to set the title bar. Woohoo!
146 //--moved up-- SDL_WM_SetCaption(DESCENT_VERSION " " D1X_DATE, NULL);
147 //--moved up--end addition -MM
149 // gamefont_choose_game_font(w,h);
153 int gr_check_fullscreen(void){
154 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
157 int gr_toggle_fullscreen(void){
158 sdl_video_flags^=SDL_FULLSCREEN;
159 SDL_WM_ToggleFullScreen(screen);
160 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
165 // Only do this function once!
169 if (SDL_Init(SDL_INIT_VIDEO) < 0)
171 Error("SDL library video initialisation failed: %s.",SDL_GetError());
173 MALLOC( grd_curscreen,grs_screen,1 );
174 memset( grd_curscreen, 0, sizeof(grs_screen));
176 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
177 if (FindArg("-fullscreen"))
178 sdl_video_flags|=SDL_FULLSCREEN;
180 //added 05/19/99 Matt Mueller - make HW surface optional
181 if (FindArg("-hwsurface"))
182 sdl_video_flags|=SDL_HWSURFACE;
184 if (FindArg("-nosdlvidmodecheck"))
187 grd_curscreen->sc_canvas.cv_color = 0;
188 grd_curscreen->sc_canvas.cv_drawmode = 0;
189 grd_curscreen->sc_canvas.cv_font = NULL;
190 grd_curscreen->sc_canvas.cv_font_fg_color = 0;
191 grd_curscreen->sc_canvas.cv_font_bg_color = 0;
192 gr_set_current_canvas( &grd_curscreen->sc_canvas );
195 // added on 980913 by adb to add cleanup
197 // end changes by adb
207 d_free(grd_curscreen);
211 // Palette functions follow.
213 static int last_r=0, last_g=0, last_b=0;
215 void gr_palette_clear()
217 SDL_Palette *palette;
218 SDL_Color colors[256];
221 palette = screen->format->palette;
223 if (palette == NULL) {
224 return; // Display is not palettised
227 ncolors = palette->ncolors;
228 memset(colors, 0, ncolors * sizeof(SDL_Color));
230 SDL_SetColors(screen, colors, 0, 256);
232 gr_palette_faded_out = 1;
236 void gr_palette_step_up( int r, int g, int b )
239 ubyte *p = gr_palette;
242 SDL_Palette *palette;
243 SDL_Color colors[256];
245 if (gr_palette_faded_out) return;
247 if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
253 palette = screen->format->palette;
255 if (palette == NULL) {
256 return; // Display is not palettised
259 for (i=0; i<256; i++) {
260 temp = (int)(*p++) + r + gr_palette_gamma;
262 else if (temp>63) temp=63;
263 colors[i].r = temp * 4;
264 temp = (int)(*p++) + g + gr_palette_gamma;
266 else if (temp>63) temp=63;
267 colors[i].g = temp * 4;
268 temp = (int)(*p++) + b + gr_palette_gamma;
270 else if (temp>63) temp=63;
271 colors[i].b = temp * 4;
274 SDL_SetColors(screen, colors, 0, 256);
277 //added on 980913 by adb to fix palette problems
278 // need a min without side effects...
280 static inline int min(int x, int y) { return x < y ? x : y; }
283 void gr_palette_load( ubyte *pal )
286 SDL_Palette *palette;
287 SDL_Color colors[256];
289 for (i=0; i<768; i++ ) {
290 gr_current_pal[i] = pal[i];
291 if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
294 palette = screen->format->palette;
296 if (palette == NULL) {
297 return; // Display is not palettised
300 for (i = 0, j = 0; j < 256; j++) {
301 //changed on 980913 by adb to fix palette problems
302 colors[j].r = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
303 colors[j].g = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
304 colors[j].b = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
307 SDL_SetColors(screen, colors, 0, 256);
309 gr_palette_faded_out = 0;
310 init_computed_colors();
315 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
319 fix fade_palette[768];
320 fix fade_palette_delta[768];
322 SDL_Palette *palette;
323 SDL_Color fade_colors[256];
325 if (gr_palette_faded_out) return 0;
327 #if 1 //ifndef NDEBUG
328 if (grd_fades_disabled) {
334 palette = screen->format->palette;
335 if (palette == NULL) {
336 return -1; // Display is not palettised
339 if (pal==NULL) pal=gr_current_pal;
341 for (i=0; i<768; i++ ) {
342 gr_current_pal[i] = pal[i];
343 fade_palette[i] = i2f(pal[i]);
344 fade_palette_delta[i] = fade_palette[i] / nsteps;
346 for (j=0; j<nsteps; j++ ) {
347 for (i=0, k = 0; k<256; k++ ) {
348 fade_palette[i] -= fade_palette_delta[i];
349 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
350 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
351 c = f2i(fade_palette[i]);
353 fade_colors[k].r = c * 4;
356 fade_palette[i] -= fade_palette_delta[i];
357 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
358 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
359 c = f2i(fade_palette[i]);
361 fade_colors[k].g = c * 4;
364 fade_palette[i] -= fade_palette_delta[i];
365 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
366 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
367 c = f2i(fade_palette[i]);
369 fade_colors[k].b = c * 4;
373 SDL_SetColors(screen, fade_colors, 0, 256);
376 gr_palette_faded_out = 1;
382 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
384 int i, j, k, ncolors;
386 fix fade_palette[768];
387 fix fade_palette_delta[768];
389 SDL_Palette *palette;
390 SDL_Color fade_colors[256];
392 if (!gr_palette_faded_out) return 0;
394 #if 1 //ifndef NDEBUG
395 if (grd_fades_disabled) {
396 gr_palette_load(pal);
401 palette = screen->format->palette;
403 if (palette == NULL) {
404 return -1; // Display is not palettised
407 ncolors = palette->ncolors;
409 for (i=0; i<768; i++ ) {
410 gr_current_pal[i] = pal[i];
412 fade_palette_delta[i] = i2f(pal[i]) / nsteps;
415 for (j=0; j<nsteps; j++ ) {
416 for (i=0, k = 0; k<256; k++ ) {
417 fade_palette[i] += fade_palette_delta[i];
418 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
419 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
420 c = f2i(fade_palette[i]);
422 fade_colors[k].r = c * 4;
425 fade_palette[i] += fade_palette_delta[i];
426 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
427 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
428 c = f2i(fade_palette[i]);
430 fade_colors[k].g = c * 4;
433 fade_palette[i] += fade_palette_delta[i];
434 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
435 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
436 c = f2i(fade_palette[i]);
438 fade_colors[k].b = c * 4;
442 SDL_SetColors(screen, fade_colors, 0, 256);
444 //added on 980913 by adb to fix palette problems
445 gr_palette_load(pal);
448 gr_palette_faded_out = 0;
454 void gr_palette_read(ubyte * pal)
456 SDL_Palette *palette;
459 palette = screen->format->palette;
461 if (palette == NULL) {
462 return; // Display is not palettised
465 for (i = 0, j=0; i < 256; i++) {
466 pal[j++] = palette->colors[i].r / 4;
467 pal[j++] = palette->colors[i].g / 4;
468 pal[j++] = palette->colors[i].b / 4;