2 * $Source: /cvs/cvsroot/d2x/video/sdl_gr.c,v $
5 * $Date: 2001-01-31 13:59:23 $
9 * $Log: not supported by cvs2svn $
10 * Revision 1.3 2001/01/29 13:47:52 bradleyb
11 * Fixed build, some minor cleanups.
27 //added on 9/30/98 by Matt Mueller to set the title bar. Woohoo!
33 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
36 int sdl_video_flags = SDL_SWSURFACE | SDL_HWPALETTE;
37 char checkvidmodeok=0;
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);
71 void gr_palette_clear(); // Function prototype for gr_init;
76 //added 05/19/99 Matt Mueller - locking stuff
80 SDL_UpdateRect(screen,0,0,0,0);
83 extern int VGA_current_mode; // DPH: kludge - remove at all costs
85 int gr_set_mode(u_int32_t mode)
97 VGA_current_mode = mode;
99 if (screen != NULL) gr_palette_clear();
101 //added on 11/06/98 by Matt Mueller to set the title bar. (moved from below)
102 //sekmu: might wanna copy this litte blurb to one of the text files or something
103 //we want to set it here so that X window manager "Style" type commands work
104 //for example, in fvwm2 or fvwm95:
105 //Style "D1X*" NoTitle, NoHandles, BorderWidth 0
106 //if you can't use -fullscreen like me (crashes X), this is a big help in
107 //getting the window centered correctly (if you use SmartPlacement)
108 SDL_WM_SetCaption("D2x", "Descent II");
111 //edited 10/05/98 by Matt Mueller - make fullscreen mode optional
112 // changed by adb on 980913: added SDL_HWPALETTE (should be option?)
113 // changed by someone on 980923 to add SDL_FULLSCREEN
114 if(!checkvidmodeok || SDL_VideoModeOK(w,h,8,sdl_video_flags)){
115 screen = SDL_SetVideoMode(w, h, 8, sdl_video_flags);
119 // end changes by someone
120 // end changes by adb
122 if (screen == NULL) {
123 Error("Could not set %dx%dx8 video mode\n",w,h);
126 memset( grd_curscreen, 0, sizeof(grs_screen));
127 grd_curscreen->sc_mode = mode;
128 grd_curscreen->sc_w = w;
129 grd_curscreen->sc_h = h;
130 grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
131 grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
132 grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
133 grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
134 grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
135 grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
136 grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_LINEAR;
137 grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
138 gr_set_current_canvas(NULL);
139 //gr_enable_default_palette_loading();
141 //added on 9/30/98 by Matt Mueller to hide the mouse if its over the game window
144 //--moved up--added on 9/30/98 by Matt Mueller to set the title bar. Woohoo!
145 //--moved up-- SDL_WM_SetCaption(DESCENT_VERSION " " D1X_DATE, NULL);
146 //--moved up--end addition -MM
148 // gamefont_choose_game_font(w,h);
152 int gr_check_fullscreen(void){
153 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
156 int gr_toggle_fullscreen(void){
157 sdl_video_flags^=SDL_FULLSCREEN;
159 grd_curscreen->sc_mode=0;//hack to get it to reset screen mode
161 SDL_WM_ToggleFullScreen(screen);
162 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
168 int mode = SM(640,480);
169 // Only do this function once!
173 if (SDL_Init(SDL_INIT_VIDEO) < 0)
175 Error("SDL library video initialisation failed: %s.",SDL_GetError());
177 MALLOC( grd_curscreen,grs_screen,1 );
178 memset( grd_curscreen, 0, sizeof(grs_screen));
180 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
181 if (FindArg("-fullscreen"))
182 sdl_video_flags|=SDL_FULLSCREEN;
184 //added 05/19/99 Matt Mueller - make HW surface optional
185 if (FindArg("-hwsurface"))
186 sdl_video_flags|=SDL_HWSURFACE;
188 if (FindArg("-nosdlvidmodecheck"))
192 if ((retcode=gr_set_mode(mode)))
196 grd_curscreen->sc_canvas.cv_color = 0;
197 grd_curscreen->sc_canvas.cv_drawmode = 0;
198 grd_curscreen->sc_canvas.cv_font = NULL;
199 grd_curscreen->sc_canvas.cv_font_fg_color = 0;
200 grd_curscreen->sc_canvas.cv_font_bg_color = 0;
201 gr_set_current_canvas( &grd_curscreen->sc_canvas );
204 // added on 980913 by adb to add cleanup
206 // end changes by adb
216 d_free(grd_curscreen);
220 // Palette functions follow.
222 static int last_r=0, last_g=0, last_b=0;
224 void gr_palette_clear()
226 SDL_Palette *palette;
227 SDL_Color colors[256];
230 palette = screen->format->palette;
232 if (palette == NULL) {
233 return; // Display is not palettised
236 ncolors = palette->ncolors;
237 memset(colors, 0, ncolors * sizeof(SDL_Color));
239 SDL_SetColors(screen, colors, 0, 256);
241 gr_palette_faded_out = 1;
245 void gr_palette_step_up( int r, int g, int b )
248 ubyte *p = gr_palette;
251 SDL_Palette *palette;
252 SDL_Color colors[256];
254 if (gr_palette_faded_out) return;
256 if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
262 palette = screen->format->palette;
264 if (palette == NULL) {
265 return; // Display is not palettised
268 for (i=0; i<256; i++) {
269 temp = (int)(*p++) + r + gr_palette_gamma;
271 else if (temp>63) temp=63;
272 colors[i].r = temp * 4;
273 temp = (int)(*p++) + g + gr_palette_gamma;
275 else if (temp>63) temp=63;
276 colors[i].g = temp * 4;
277 temp = (int)(*p++) + b + gr_palette_gamma;
279 else if (temp>63) temp=63;
280 colors[i].b = temp * 4;
283 SDL_SetColors(screen, colors, 0, 256);
286 //added on 980913 by adb to fix palette problems
287 // need a min without side effects...
289 static inline int min(int x, int y) { return x < y ? x : y; }
292 void gr_palette_load( ubyte *pal )
295 SDL_Palette *palette;
296 SDL_Color colors[256];
298 for (i=0; i<768; i++ ) {
299 gr_current_pal[i] = pal[i];
300 if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
303 palette = screen->format->palette;
305 if (palette == NULL) {
306 return; // Display is not palettised
309 for (i = 0, j = 0; j < 256; j++) {
310 //changed on 980913 by adb to fix palette problems
311 colors[j].r = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
312 colors[j].g = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
313 colors[j].b = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
316 SDL_SetColors(screen, colors, 0, 256);
318 gr_palette_faded_out = 0;
319 init_computed_colors();
324 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
328 fix fade_palette[768];
329 fix fade_palette_delta[768];
331 SDL_Palette *palette;
332 SDL_Color fade_colors[256];
334 if (gr_palette_faded_out) return 0;
336 palette = screen->format->palette;
337 if (palette == NULL) {
338 return -1; // Display is not palettised
341 if (pal==NULL) pal=gr_current_pal;
343 for (i=0; i<768; i++ ) {
344 gr_current_pal[i] = pal[i];
345 fade_palette[i] = i2f(pal[i]);
346 fade_palette_delta[i] = fade_palette[i] / nsteps;
348 for (j=0; j<nsteps; j++ ) {
349 for (i=0, k = 0; k<256; k++ ) {
350 fade_palette[i] -= fade_palette_delta[i];
351 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
352 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
353 c = f2i(fade_palette[i]);
355 fade_colors[k].r = c * 4;
358 fade_palette[i] -= fade_palette_delta[i];
359 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
360 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
361 c = f2i(fade_palette[i]);
363 fade_colors[k].g = c * 4;
366 fade_palette[i] -= fade_palette_delta[i];
367 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
368 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
369 c = f2i(fade_palette[i]);
371 fade_colors[k].b = c * 4;
375 SDL_SetColors(screen, fade_colors, 0, 256);
378 gr_palette_faded_out = 1;
384 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
386 int i, j, k, ncolors;
388 fix fade_palette[768];
389 fix fade_palette_delta[768];
391 SDL_Palette *palette;
392 SDL_Color fade_colors[256];
394 if (!gr_palette_faded_out) return 0;
396 palette = screen->format->palette;
398 if (palette == NULL) {
399 return -1; // Display is not palettised
402 ncolors = palette->ncolors;
404 for (i=0; i<768; i++ ) {
405 gr_current_pal[i] = pal[i];
407 fade_palette_delta[i] = i2f(pal[i]) / nsteps;
410 for (j=0; j<nsteps; j++ ) {
411 for (i=0, k = 0; k<256; k++ ) {
412 fade_palette[i] += fade_palette_delta[i];
413 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
414 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
415 c = f2i(fade_palette[i]);
417 fade_colors[k].r = c * 4;
420 fade_palette[i] += fade_palette_delta[i];
421 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
422 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
423 c = f2i(fade_palette[i]);
425 fade_colors[k].g = c * 4;
428 fade_palette[i] += fade_palette_delta[i];
429 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
430 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
431 c = f2i(fade_palette[i]);
433 fade_colors[k].b = c * 4;
437 SDL_SetColors(screen, fade_colors, 0, 256);
439 //added on 980913 by adb to fix palette problems
440 gr_palette_load(pal);
443 gr_palette_faded_out = 0;
449 void gr_palette_read(ubyte * pal)
451 SDL_Palette *palette;
454 palette = screen->format->palette;
456 if (palette == NULL) {
457 return; // Display is not palettised
460 for (i = 0, j=0; i < 256; i++) {
461 pal[j++] = palette->colors[i].r / 4;
462 pal[j++] = palette->colors[i].g / 4;
463 pal[j++] = palette->colors[i].b / 4;