2 * $Source: /cvs/cvsroot/d2x/video/sdl_gr.c,v $
5 * $Date: 2001-10-09 08:17:07 $
9 * $Log: not supported by cvs2svn $
10 * Revision 1.4 2001/01/31 13:59:23 bradleyb
11 * Fullscreen toggle added to screen res menu
13 * Revision 1.3 2001/01/29 13:47:52 bradleyb
14 * Fixed build, some minor cleanups.
30 //added on 9/30/98 by Matt Mueller to set the title bar. Woohoo!
36 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
39 int sdl_video_flags = SDL_SWSURFACE | SDL_HWPALETTE;
40 char checkvidmodeok=0;
47 //added 05/19/99 Matt Mueller - locking stuff
51 int gr_testlocklevel=0;
53 inline void gr_dolock(const char *file,int line) {
55 if ( gr_testlocklevel==1 && SDL_MUSTLOCK(screen) ) {
57 chcksetwritable(screen.pixels,screen->w*screen->h*screen->format->BytesPerPixel);
59 if ( SDL_LockSurface(screen) < 0 )Error("could not lock screen (%s:%i)\n",file,line);
62 inline void gr_dounlock(void) {
64 if (gr_testlocklevel==0 && SDL_MUSTLOCK(screen) ) {
65 SDL_UnlockSurface(screen);
67 chcksetunwritable(screen.pixels,screen->w*screen->h*screen->format->BytesPerPixel);
74 void gr_palette_clear(); // Function prototype for gr_init;
79 //added 05/19/99 Matt Mueller - locking stuff
83 SDL_UpdateRect(screen,0,0,0,0);
86 extern int VGA_current_mode; // DPH: kludge - remove at all costs
88 int gr_set_mode(u_int32_t mode)
100 VGA_current_mode = mode;
102 if (screen != NULL) gr_palette_clear();
104 //added on 11/06/98 by Matt Mueller to set the title bar. (moved from below)
105 //sekmu: might wanna copy this litte blurb to one of the text files or something
106 //we want to set it here so that X window manager "Style" type commands work
107 //for example, in fvwm2 or fvwm95:
108 //Style "D1X*" NoTitle, NoHandles, BorderWidth 0
109 //if you can't use -fullscreen like me (crashes X), this is a big help in
110 //getting the window centered correctly (if you use SmartPlacement)
111 SDL_WM_SetCaption(DESCENT_VERSION, "Descent II");
114 //edited 10/05/98 by Matt Mueller - make fullscreen mode optional
115 // changed by adb on 980913: added SDL_HWPALETTE (should be option?)
116 // changed by someone on 980923 to add SDL_FULLSCREEN
117 if(!checkvidmodeok || SDL_VideoModeOK(w,h,8,sdl_video_flags)){
118 screen = SDL_SetVideoMode(w, h, 8, sdl_video_flags);
122 // end changes by someone
123 // end changes by adb
125 if (screen == NULL) {
126 Error("Could not set %dx%dx8 video mode\n",w,h);
129 memset( grd_curscreen, 0, sizeof(grs_screen));
130 grd_curscreen->sc_mode = mode;
131 grd_curscreen->sc_w = w;
132 grd_curscreen->sc_h = h;
133 grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
134 grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
135 grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
136 grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
137 grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
138 grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
139 grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_LINEAR;
140 grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
141 gr_set_current_canvas(NULL);
142 //gr_enable_default_palette_loading();
144 //added on 9/30/98 by Matt Mueller to hide the mouse if its over the game window
147 //--moved up--added on 9/30/98 by Matt Mueller to set the title bar. Woohoo!
148 //--moved up-- SDL_WM_SetCaption(DESCENT_VERSION " " D1X_DATE, NULL);
149 //--moved up--end addition -MM
151 // gamefont_choose_game_font(w,h);
155 int gr_check_fullscreen(void){
156 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
159 int gr_toggle_fullscreen(void){
160 sdl_video_flags^=SDL_FULLSCREEN;
162 grd_curscreen->sc_mode=0;//hack to get it to reset screen mode
164 SDL_WM_ToggleFullScreen(screen);
165 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
171 int mode = SM(640,480);
172 // Only do this function once!
176 if (SDL_Init(SDL_INIT_VIDEO) < 0)
178 Error("SDL library video initialisation failed: %s.",SDL_GetError());
180 MALLOC( grd_curscreen,grs_screen,1 );
181 memset( grd_curscreen, 0, sizeof(grs_screen));
183 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
184 if (FindArg("-fullscreen"))
185 sdl_video_flags|=SDL_FULLSCREEN;
187 //added 05/19/99 Matt Mueller - make HW surface optional
188 if (FindArg("-hwsurface"))
189 sdl_video_flags|=SDL_HWSURFACE;
191 if (FindArg("-nosdlvidmodecheck"))
195 if ((retcode=gr_set_mode(mode)))
199 grd_curscreen->sc_canvas.cv_color = 0;
200 grd_curscreen->sc_canvas.cv_drawmode = 0;
201 grd_curscreen->sc_canvas.cv_font = NULL;
202 grd_curscreen->sc_canvas.cv_font_fg_color = 0;
203 grd_curscreen->sc_canvas.cv_font_bg_color = 0;
204 gr_set_current_canvas( &grd_curscreen->sc_canvas );
207 // added on 980913 by adb to add cleanup
209 // end changes by adb
219 d_free(grd_curscreen);
223 // Palette functions follow.
225 static int last_r=0, last_g=0, last_b=0;
227 void gr_palette_clear()
229 SDL_Palette *palette;
230 SDL_Color colors[256];
233 palette = screen->format->palette;
235 if (palette == NULL) {
236 return; // Display is not palettised
239 ncolors = palette->ncolors;
240 memset(colors, 0, ncolors * sizeof(SDL_Color));
242 SDL_SetColors(screen, colors, 0, 256);
244 gr_palette_faded_out = 1;
248 void gr_palette_step_up( int r, int g, int b )
251 ubyte *p = gr_palette;
254 SDL_Palette *palette;
255 SDL_Color colors[256];
257 if (gr_palette_faded_out) return;
259 if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
265 palette = screen->format->palette;
267 if (palette == NULL) {
268 return; // Display is not palettised
271 for (i=0; i<256; i++) {
272 temp = (int)(*p++) + r + gr_palette_gamma;
274 else if (temp>63) temp=63;
275 colors[i].r = temp * 4;
276 temp = (int)(*p++) + g + gr_palette_gamma;
278 else if (temp>63) temp=63;
279 colors[i].g = temp * 4;
280 temp = (int)(*p++) + b + gr_palette_gamma;
282 else if (temp>63) temp=63;
283 colors[i].b = temp * 4;
286 SDL_SetColors(screen, colors, 0, 256);
289 //added on 980913 by adb to fix palette problems
290 // need a min without side effects...
292 static inline int min(int x, int y) { return x < y ? x : y; }
295 void gr_palette_load( ubyte *pal )
298 SDL_Palette *palette;
299 SDL_Color colors[256];
301 for (i=0; i<768; i++ ) {
302 gr_current_pal[i] = pal[i];
303 if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
306 palette = screen->format->palette;
308 if (palette == NULL) {
309 return; // Display is not palettised
312 for (i = 0, j = 0; j < 256; j++) {
313 //changed on 980913 by adb to fix palette problems
314 colors[j].r = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
315 colors[j].g = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
316 colors[j].b = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
319 SDL_SetColors(screen, colors, 0, 256);
321 gr_palette_faded_out = 0;
322 init_computed_colors();
327 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
331 fix fade_palette[768];
332 fix fade_palette_delta[768];
334 SDL_Palette *palette;
335 SDL_Color fade_colors[256];
337 if (gr_palette_faded_out) return 0;
339 palette = screen->format->palette;
340 if (palette == NULL) {
341 return -1; // Display is not palettised
344 if (pal==NULL) pal=gr_current_pal;
346 for (i=0; i<768; i++ ) {
347 gr_current_pal[i] = pal[i];
348 fade_palette[i] = i2f(pal[i]);
349 fade_palette_delta[i] = fade_palette[i] / nsteps;
351 for (j=0; j<nsteps; j++ ) {
352 for (i=0, k = 0; k<256; k++ ) {
353 fade_palette[i] -= fade_palette_delta[i];
354 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
355 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
356 c = f2i(fade_palette[i]);
358 fade_colors[k].r = c * 4;
361 fade_palette[i] -= fade_palette_delta[i];
362 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
363 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
364 c = f2i(fade_palette[i]);
366 fade_colors[k].g = c * 4;
369 fade_palette[i] -= fade_palette_delta[i];
370 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
371 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
372 c = f2i(fade_palette[i]);
374 fade_colors[k].b = c * 4;
378 SDL_SetColors(screen, fade_colors, 0, 256);
381 gr_palette_faded_out = 1;
387 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
389 int i, j, k, ncolors;
391 fix fade_palette[768];
392 fix fade_palette_delta[768];
394 SDL_Palette *palette;
395 SDL_Color fade_colors[256];
397 if (!gr_palette_faded_out) return 0;
399 palette = screen->format->palette;
401 if (palette == NULL) {
402 return -1; // Display is not palettised
405 ncolors = palette->ncolors;
407 for (i=0; i<768; i++ ) {
408 gr_current_pal[i] = pal[i];
410 fade_palette_delta[i] = i2f(pal[i]) / nsteps;
413 for (j=0; j<nsteps; j++ ) {
414 for (i=0, k = 0; k<256; k++ ) {
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].r = 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].g = c * 4;
431 fade_palette[i] += fade_palette_delta[i];
432 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
433 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
434 c = f2i(fade_palette[i]);
436 fade_colors[k].b = c * 4;
440 SDL_SetColors(screen, fade_colors, 0, 256);
442 //added on 980913 by adb to fix palette problems
443 gr_palette_load(pal);
446 gr_palette_faded_out = 0;
452 void gr_palette_read(ubyte * pal)
454 SDL_Palette *palette;
457 palette = screen->format->palette;
459 if (palette == NULL) {
460 return; // Display is not palettised
463 for (i = 0, j=0; i < 256; i++) {
464 pal[j++] = palette->colors[i].r / 4;
465 pal[j++] = palette->colors[i].g / 4;
466 pal[j++] = palette->colors[i].b / 4;