1 /* $Id: gr.c,v 1.10 2003-03-20 02:01:38 btb Exp $ */
24 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
27 int sdl_video_flags = SDL_SWSURFACE | SDL_HWPALETTE;
28 char checkvidmodeok=0;
35 //added 05/19/99 Matt Mueller - locking stuff
39 int gr_testlocklevel=0;
41 inline void gr_dolock(const char *file,int line) {
43 if ( gr_testlocklevel==1 && SDL_MUSTLOCK(screen) ) {
45 chcksetwritable(screen.pixels,screen->w*screen->h*screen->format->BytesPerPixel);
47 if ( SDL_LockSurface(screen) < 0 )Error("could not lock screen (%s:%i)\n",file,line);
50 inline void gr_dounlock(void) {
52 if (gr_testlocklevel==0 && SDL_MUSTLOCK(screen) ) {
53 SDL_UnlockSurface(screen);
55 chcksetunwritable(screen.pixels,screen->w*screen->h*screen->format->BytesPerPixel);
62 void gr_palette_clear(); // Function prototype for gr_init;
67 //added 05/19/99 Matt Mueller - locking stuff
70 SDL_UpdateRect(screen,0,0,0,0);
73 extern int VGA_current_mode; // DPH: kludge - remove at all costs
75 int gr_set_mode(u_int32_t mode)
88 VGA_current_mode = mode;
90 if (screen != NULL) gr_palette_clear();
92 //added on 11/06/98 by Matt Mueller to set the title bar. (moved from below)
93 //sekmu: might wanna copy this litte blurb to one of the text files or something
94 //we want to set it here so that X window manager "Style" type commands work
95 //for example, in fvwm2 or fvwm95:
96 //Style "D1X*" NoTitle, NoHandles, BorderWidth 0
97 //if you can't use -fullscreen like me (crashes X), this is a big help in
98 //getting the window centered correctly (if you use SmartPlacement)
99 SDL_WM_SetCaption(PACKAGE_STRING, "Descent II");
102 //edited 10/05/98 by Matt Mueller - make fullscreen mode optional
103 // changed by adb on 980913: added SDL_HWPALETTE (should be option?)
104 // changed by someone on 980923 to add SDL_FULLSCREEN
105 if(!checkvidmodeok || SDL_VideoModeOK(w,h,8,sdl_video_flags)){
106 screen = SDL_SetVideoMode(w, h, 8, sdl_video_flags);
110 // end changes by someone
111 // end changes by adb
113 if (screen == NULL) {
114 Error("Could not set %dx%dx8 video mode\n",w,h);
117 memset( grd_curscreen, 0, sizeof(grs_screen));
118 grd_curscreen->sc_mode = mode;
119 grd_curscreen->sc_w = w;
120 grd_curscreen->sc_h = h;
121 grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
122 grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
123 grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
124 grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
125 grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
126 grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
127 grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_LINEAR;
128 grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
129 gr_set_current_canvas(NULL);
130 //gr_enable_default_palette_loading();
132 //added on 9/30/98 by Matt Mueller to hide the mouse if its over the game window
135 //--moved up--added on 9/30/98 by Matt Mueller to set the title bar. Woohoo!
136 //--moved up-- SDL_WM_SetCaption(DESCENT_VERSION " " D1X_DATE, NULL);
137 //--moved up--end addition -MM
139 // gamefont_choose_game_font(w,h);
143 int gr_check_fullscreen(void){
144 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
147 int gr_toggle_fullscreen(void){
148 sdl_video_flags^=SDL_FULLSCREEN;
149 SDL_WM_ToggleFullScreen(screen);
150 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
155 // Only do this function once!
159 if (SDL_Init(SDL_INIT_VIDEO) < 0)
161 Error("SDL library video initialisation failed: %s.",SDL_GetError());
163 MALLOC( grd_curscreen,grs_screen,1 );
164 memset( grd_curscreen, 0, sizeof(grs_screen));
166 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
167 if (FindArg("-fullscreen"))
168 sdl_video_flags|=SDL_FULLSCREEN;
170 //added 05/19/99 Matt Mueller - make HW surface optional
171 if (FindArg("-hwsurface"))
172 sdl_video_flags|=SDL_HWSURFACE;
174 if (FindArg("-nosdlvidmodecheck"))
177 grd_curscreen->sc_canvas.cv_color = 0;
178 grd_curscreen->sc_canvas.cv_drawmode = 0;
179 grd_curscreen->sc_canvas.cv_font = NULL;
180 grd_curscreen->sc_canvas.cv_font_fg_color = 0;
181 grd_curscreen->sc_canvas.cv_font_bg_color = 0;
182 gr_set_current_canvas( &grd_curscreen->sc_canvas );
185 // added on 980913 by adb to add cleanup
187 // end changes by adb
197 d_free(grd_curscreen);
201 // Palette functions follow.
203 static int last_r=0, last_g=0, last_b=0;
205 void gr_palette_clear()
207 SDL_Palette *palette;
208 SDL_Color colors[256];
211 palette = screen->format->palette;
213 if (palette == NULL) {
214 return; // Display is not palettised
217 ncolors = palette->ncolors;
218 memset(colors, 0, ncolors * sizeof(SDL_Color));
220 SDL_SetColors(screen, colors, 0, 256);
222 gr_palette_faded_out = 1;
226 void gr_palette_step_up( int r, int g, int b )
229 ubyte *p = gr_palette;
232 SDL_Palette *palette;
233 SDL_Color colors[256];
235 if (gr_palette_faded_out) return;
237 if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
243 palette = screen->format->palette;
245 if (palette == NULL) {
246 return; // Display is not palettised
249 for (i=0; i<256; i++) {
250 temp = (int)(*p++) + r + gr_palette_gamma;
252 else if (temp>63) temp=63;
253 colors[i].r = temp * 4;
254 temp = (int)(*p++) + g + gr_palette_gamma;
256 else if (temp>63) temp=63;
257 colors[i].g = temp * 4;
258 temp = (int)(*p++) + b + gr_palette_gamma;
260 else if (temp>63) temp=63;
261 colors[i].b = temp * 4;
264 SDL_SetColors(screen, colors, 0, 256);
267 //added on 980913 by adb to fix palette problems
268 // need a min without side effects...
270 static inline int min(int x, int y) { return x < y ? x : y; }
273 void gr_palette_load( ubyte *pal )
276 SDL_Palette *palette;
277 SDL_Color colors[256];
279 for (i=0; i<768; i++ ) {
280 gr_current_pal[i] = pal[i];
281 if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
284 palette = screen->format->palette;
286 if (palette == NULL) {
287 return; // Display is not palettised
290 for (i = 0, j = 0; j < 256; j++) {
291 //changed on 980913 by adb to fix palette problems
292 colors[j].r = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
293 colors[j].g = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
294 colors[j].b = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
297 SDL_SetColors(screen, colors, 0, 256);
299 gr_palette_faded_out = 0;
300 init_computed_colors();
305 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
309 fix fade_palette[768];
310 fix fade_palette_delta[768];
312 SDL_Palette *palette;
313 SDL_Color fade_colors[256];
315 if (gr_palette_faded_out) return 0;
317 #if 1 //ifndef NDEBUG
318 if (grd_fades_disabled) {
324 palette = screen->format->palette;
325 if (palette == NULL) {
326 return -1; // Display is not palettised
329 if (pal==NULL) pal=gr_current_pal;
331 for (i=0; i<768; i++ ) {
332 gr_current_pal[i] = pal[i];
333 fade_palette[i] = i2f(pal[i]);
334 fade_palette_delta[i] = fade_palette[i] / nsteps;
336 for (j=0; j<nsteps; j++ ) {
337 for (i=0, k = 0; k<256; k++ ) {
338 fade_palette[i] -= fade_palette_delta[i];
339 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
340 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
341 c = f2i(fade_palette[i]);
343 fade_colors[k].r = c * 4;
346 fade_palette[i] -= fade_palette_delta[i];
347 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
348 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
349 c = f2i(fade_palette[i]);
351 fade_colors[k].g = c * 4;
354 fade_palette[i] -= fade_palette_delta[i];
355 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
356 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
357 c = f2i(fade_palette[i]);
359 fade_colors[k].b = c * 4;
363 SDL_SetColors(screen, fade_colors, 0, 256);
366 gr_palette_faded_out = 1;
372 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
374 int i, j, k, ncolors;
376 fix fade_palette[768];
377 fix fade_palette_delta[768];
379 SDL_Palette *palette;
380 SDL_Color fade_colors[256];
382 if (!gr_palette_faded_out) return 0;
384 #if 1 //ifndef NDEBUG
385 if (grd_fades_disabled) {
386 gr_palette_load(pal);
391 palette = screen->format->palette;
393 if (palette == NULL) {
394 return -1; // Display is not palettised
397 ncolors = palette->ncolors;
399 for (i=0; i<768; i++ ) {
400 gr_current_pal[i] = pal[i];
402 fade_palette_delta[i] = i2f(pal[i]) / nsteps;
405 for (j=0; j<nsteps; j++ ) {
406 for (i=0, k = 0; k<256; k++ ) {
407 fade_palette[i] += fade_palette_delta[i];
408 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
409 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
410 c = f2i(fade_palette[i]);
412 fade_colors[k].r = c * 4;
415 fade_palette[i] += fade_palette_delta[i];
416 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
417 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
418 c = f2i(fade_palette[i]);
420 fade_colors[k].g = c * 4;
423 fade_palette[i] += fade_palette_delta[i];
424 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
425 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
426 c = f2i(fade_palette[i]);
428 fade_colors[k].b = c * 4;
432 SDL_SetColors(screen, fade_colors, 0, 256);
434 //added on 980913 by adb to fix palette problems
435 gr_palette_load(pal);
438 gr_palette_faded_out = 0;
444 void gr_palette_read(ubyte * pal)
446 SDL_Palette *palette;
449 palette = screen->format->palette;
451 if (palette == NULL) {
452 return; // Display is not palettised
455 for (i = 0, j=0; i < 256; i++) {
456 pal[j++] = palette->colors[i].r / 4;
457 pal[j++] = palette->colors[i].g / 4;
458 pal[j++] = palette->colors[i].b / 4;