2 * $Source: /cvs/cvsroot/d2x/video/sdl_gr.c,v $
5 * $Date: 2001-01-29 13:47:52 $
9 * $Log: not supported by cvs2svn $
24 //added on 9/30/98 by Matt Mueller to set the title bar. Woohoo!
30 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
33 int sdl_video_flags = SDL_SWSURFACE | SDL_HWPALETTE;
34 char checkvidmodeok=0;
41 //added 05/19/99 Matt Mueller - locking stuff
45 int gr_testlocklevel=0;
47 inline void gr_dolock(const char *file,int line) {
49 if ( gr_testlocklevel==1 && SDL_MUSTLOCK(screen) ) {
51 chcksetwritable(screen.pixels,screen->w*screen->h*screen->format->BytesPerPixel);
53 if ( SDL_LockSurface(screen) < 0 )Error("could not lock screen (%s:%i)\n",file,line);
56 inline void gr_dounlock(void) {
58 if (gr_testlocklevel==0 && SDL_MUSTLOCK(screen) ) {
59 SDL_UnlockSurface(screen);
61 chcksetunwritable(screen.pixels,screen->w*screen->h*screen->format->BytesPerPixel);
68 void gr_palette_clear(); // Function prototype for gr_init;
73 //added 05/19/99 Matt Mueller - locking stuff
77 SDL_UpdateRect(screen,0,0,0,0);
80 extern int VGA_current_mode; // DPH: kludge - remove at all costs
82 int gr_set_mode(u_int32_t mode)
94 VGA_current_mode = mode;
96 if (screen != NULL) gr_palette_clear();
98 //added on 11/06/98 by Matt Mueller to set the title bar. (moved from below)
99 //sekmu: might wanna copy this litte blurb to one of the text files or something
100 //we want to set it here so that X window manager "Style" type commands work
101 //for example, in fvwm2 or fvwm95:
102 //Style "D1X*" NoTitle, NoHandles, BorderWidth 0
103 //if you can't use -fullscreen like me (crashes X), this is a big help in
104 //getting the window centered correctly (if you use SmartPlacement)
105 SDL_WM_SetCaption("D2x", "Descent II");
108 //edited 10/05/98 by Matt Mueller - make fullscreen mode optional
109 // changed by adb on 980913: added SDL_HWPALETTE (should be option?)
110 // changed by someone on 980923 to add SDL_FULLSCREEN
111 if(!checkvidmodeok || SDL_VideoModeOK(w,h,8,sdl_video_flags)){
112 screen = SDL_SetVideoMode(w, h, 8, sdl_video_flags);
116 // end changes by someone
117 // end changes by adb
119 if (screen == NULL) {
120 Error("Could not set %dx%dx8 video mode\n",w,h);
123 memset( grd_curscreen, 0, sizeof(grs_screen));
124 grd_curscreen->sc_mode = mode;
125 grd_curscreen->sc_w = w;
126 grd_curscreen->sc_h = h;
127 grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
128 grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
129 grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
130 grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
131 grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
132 grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
133 grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_LINEAR;
134 grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
135 gr_set_current_canvas(NULL);
136 //gr_enable_default_palette_loading();
138 //added on 9/30/98 by Matt Mueller to hide the mouse if its over the game window
141 //--moved up--added on 9/30/98 by Matt Mueller to set the title bar. Woohoo!
142 //--moved up-- SDL_WM_SetCaption(DESCENT_VERSION " " D1X_DATE, NULL);
143 //--moved up--end addition -MM
145 // gamefont_choose_game_font(w,h);
151 int gr_check_fullscreen(void){
152 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
154 int gr_toggle_fullscreen(void){
155 sdl_video_flags^=SDL_FULLSCREEN;
156 grd_curscreen->sc_mode=0;//hack to get it to reset screen mode
157 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
163 int mode = SM(640,480);
164 // Only do this function once!
168 if (SDL_Init(SDL_INIT_VIDEO) < 0)
170 Error("SDL library video initialisation failed: %s.",SDL_GetError());
172 MALLOC( grd_curscreen,grs_screen,1 );
173 memset( grd_curscreen, 0, sizeof(grs_screen));
175 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
176 if (FindArg("-fullscreen"))
177 sdl_video_flags|=SDL_FULLSCREEN;
179 //added 05/19/99 Matt Mueller - make HW surface optional
180 if (FindArg("-hwsurface"))
181 sdl_video_flags|=SDL_HWSURFACE;
183 if (FindArg("-nosdlvidmodecheck"))
187 if ((retcode=gr_set_mode(mode)))
191 grd_curscreen->sc_canvas.cv_color = 0;
192 grd_curscreen->sc_canvas.cv_drawmode = 0;
193 grd_curscreen->sc_canvas.cv_font = NULL;
194 grd_curscreen->sc_canvas.cv_font_fg_color = 0;
195 grd_curscreen->sc_canvas.cv_font_bg_color = 0;
196 gr_set_current_canvas( &grd_curscreen->sc_canvas );
199 // added on 980913 by adb to add cleanup
201 // end changes by adb
211 d_free(grd_curscreen);
215 // Palette functions follow.
217 static int last_r=0, last_g=0, last_b=0;
219 void gr_palette_clear()
221 SDL_Palette *palette;
222 SDL_Color colors[256];
225 palette = screen->format->palette;
227 if (palette == NULL) {
228 return; // Display is not palettised
231 ncolors = palette->ncolors;
232 memset(colors, 0, ncolors * sizeof(SDL_Color));
234 SDL_SetColors(screen, colors, 0, 256);
236 gr_palette_faded_out = 1;
240 void gr_palette_step_up( int r, int g, int b )
243 ubyte *p = gr_palette;
246 SDL_Palette *palette;
247 SDL_Color colors[256];
249 if (gr_palette_faded_out) return;
251 if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
257 palette = screen->format->palette;
259 if (palette == NULL) {
260 return; // Display is not palettised
263 for (i=0; i<256; i++) {
264 temp = (int)(*p++) + r + gr_palette_gamma;
266 else if (temp>63) temp=63;
267 colors[i].r = temp * 4;
268 temp = (int)(*p++) + g + gr_palette_gamma;
270 else if (temp>63) temp=63;
271 colors[i].g = temp * 4;
272 temp = (int)(*p++) + b + gr_palette_gamma;
274 else if (temp>63) temp=63;
275 colors[i].b = temp * 4;
278 SDL_SetColors(screen, colors, 0, 256);
281 //added on 980913 by adb to fix palette problems
282 // need a min without side effects...
284 static inline int min(int x, int y) { return x < y ? x : y; }
287 void gr_palette_load( ubyte *pal )
290 SDL_Palette *palette;
291 SDL_Color colors[256];
293 for (i=0; i<768; i++ ) {
294 gr_current_pal[i] = pal[i];
295 if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
298 palette = screen->format->palette;
300 if (palette == NULL) {
301 return; // Display is not palettised
304 for (i = 0, j = 0; j < 256; j++) {
305 //changed on 980913 by adb to fix palette problems
306 colors[j].r = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
307 colors[j].g = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
308 colors[j].b = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
311 SDL_SetColors(screen, colors, 0, 256);
313 gr_palette_faded_out = 0;
314 init_computed_colors();
319 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
323 fix fade_palette[768];
324 fix fade_palette_delta[768];
326 SDL_Palette *palette;
327 SDL_Color fade_colors[256];
329 if (gr_palette_faded_out) return 0;
331 palette = screen->format->palette;
332 if (palette == NULL) {
333 return -1; // Display is not palettised
336 if (pal==NULL) pal=gr_current_pal;
338 for (i=0; i<768; i++ ) {
339 gr_current_pal[i] = pal[i];
340 fade_palette[i] = i2f(pal[i]);
341 fade_palette_delta[i] = fade_palette[i] / nsteps;
343 for (j=0; j<nsteps; j++ ) {
344 for (i=0, k = 0; k<256; k++ ) {
345 fade_palette[i] -= fade_palette_delta[i];
346 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
347 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
348 c = f2i(fade_palette[i]);
350 fade_colors[k].r = c * 4;
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].g = 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].b = c * 4;
370 SDL_SetColors(screen, fade_colors, 0, 256);
373 gr_palette_faded_out = 1;
379 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
381 int i, j, k, ncolors;
383 fix fade_palette[768];
384 fix fade_palette_delta[768];
386 SDL_Palette *palette;
387 SDL_Color fade_colors[256];
389 if (!gr_palette_faded_out) return 0;
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;