1 // SDL video functions.
14 //added on 9/30/98 by Matt Mueller to set the title bar. Woohoo!
20 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
23 int sdl_video_flags = SDL_SWSURFACE | SDL_HWPALETTE;
24 char checkvidmodeok=0;
31 //added 05/19/99 Matt Mueller - locking stuff
35 int gr_testlocklevel=0;
37 inline void gr_dolock(const char *file,int line) {
39 if ( gr_testlocklevel==1 && SDL_MUSTLOCK(screen) ) {
41 chcksetwritable(screen.pixels,screen->w*screen->h*screen->format->BytesPerPixel);
43 if ( SDL_LockSurface(screen) < 0 )Error("could not lock screen (%s:%i)\n",file,line);
46 inline void gr_dounlock(void) {
48 if (gr_testlocklevel==0 && SDL_MUSTLOCK(screen) ) {
49 SDL_UnlockSurface(screen);
51 chcksetunwritable(screen.pixels,screen->w*screen->h*screen->format->BytesPerPixel);
58 void gr_palette_clear(); // Function prototype for gr_init;
63 //added 05/19/99 Matt Mueller - locking stuff
67 SDL_UpdateRect(screen,0,0,0,0);
70 extern int VGA_current_mode; // DPH: kludge - remove at all costs
72 int gr_set_mode(u_int32_t mode)
84 VGA_current_mode = mode;
86 if (screen != NULL) gr_palette_clear();
88 //added on 11/06/98 by Matt Mueller to set the title bar. (moved from below)
89 //sekmu: might wanna copy this litte blurb to one of the text files or something
90 //we want to set it here so that X window manager "Style" type commands work
91 //for example, in fvwm2 or fvwm95:
92 //Style "D1X*" NoTitle, NoHandles, BorderWidth 0
93 //if you can't use -fullscreen like me (crashes X), this is a big help in
94 //getting the window centered correctly (if you use SmartPlacement)
95 SDL_WM_SetCaption("D2x", "Descent II");
98 //edited 10/05/98 by Matt Mueller - make fullscreen mode optional
99 // changed by adb on 980913: added SDL_HWPALETTE (should be option?)
100 // changed by someone on 980923 to add SDL_FULLSCREEN
101 if(!checkvidmodeok || SDL_VideoModeOK(w,h,8,sdl_video_flags)){
102 screen = SDL_SetVideoMode(w, h, 8, sdl_video_flags);
106 // end changes by someone
107 // end changes by adb
109 if (screen == NULL) {
110 Error("Could not set %dx%dx8 video mode\n",w,h);
113 memset( grd_curscreen, 0, sizeof(grs_screen));
114 grd_curscreen->sc_mode = mode;
115 grd_curscreen->sc_w = w;
116 grd_curscreen->sc_h = h;
117 grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
118 grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
119 grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
120 grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
121 grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
122 grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
123 grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_LINEAR;
124 grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
125 gr_set_current_canvas(NULL);
126 //gr_enable_default_palette_loading();
128 //added on 9/30/98 by Matt Mueller to hide the mouse if its over the game window
131 //--moved up--added on 9/30/98 by Matt Mueller to set the title bar. Woohoo!
132 //--moved up-- SDL_WM_SetCaption(DESCENT_VERSION " " D1X_DATE, NULL);
133 //--moved up--end addition -MM
135 // gamefont_choose_game_font(w,h);
141 int gr_check_fullscreen(void){
142 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
144 int gr_toggle_fullscreen(void){
145 sdl_video_flags^=SDL_FULLSCREEN;
146 grd_curscreen->sc_mode=0;//hack to get it to reset screen mode
147 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
153 int mode = SM(640,480);
154 // Only do this function once!
158 if (SDL_Init(SDL_INIT_VIDEO) < 0)
160 Error("SDL library video initialisation failed: %s.",SDL_GetError());
162 MALLOC( grd_curscreen,grs_screen,1 );
163 memset( grd_curscreen, 0, sizeof(grs_screen));
165 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
166 if (args_find("-fullscreen"))
167 sdl_video_flags|=SDL_FULLSCREEN;
169 //added 05/19/99 Matt Mueller - make HW surface optional
170 if (args_find("-hwsurface"))
171 sdl_video_flags|=SDL_HWSURFACE;
173 if (args_find("-nosdlvidmodecheck"))
177 if ((retcode=gr_set_mode(mode)))
181 grd_curscreen->sc_canvas.cv_color = 0;
182 grd_curscreen->sc_canvas.cv_drawmode = 0;
183 grd_curscreen->sc_canvas.cv_font = NULL;
184 grd_curscreen->sc_canvas.cv_font_fg_color = 0;
185 grd_curscreen->sc_canvas.cv_font_bg_color = 0;
186 gr_set_current_canvas( &grd_curscreen->sc_canvas );
189 // added on 980913 by adb to add cleanup
191 // end changes by adb
201 d_free(grd_curscreen);
205 // Palette functions follow.
207 static int last_r=0, last_g=0, last_b=0;
209 void gr_palette_clear()
211 SDL_Palette *palette;
212 SDL_Color colors[256];
215 palette = screen->format->palette;
217 if (palette == NULL) {
218 return; // Display is not palettised
221 ncolors = palette->ncolors;
222 memset(colors, 0, ncolors * sizeof(SDL_Color));
224 SDL_SetColors(screen, colors, 0, 256);
226 gr_palette_faded_out = 1;
230 void gr_palette_step_up( int r, int g, int b )
233 ubyte *p = gr_palette;
236 SDL_Palette *palette;
237 SDL_Color colors[256];
239 if (gr_palette_faded_out) return;
241 if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
247 palette = screen->format->palette;
249 if (palette == NULL) {
250 return; // Display is not palettised
253 for (i=0; i<256; i++) {
254 temp = (int)(*p++) + r + gr_palette_gamma;
256 else if (temp>63) temp=63;
257 colors[i].r = temp * 4;
258 temp = (int)(*p++) + g + gr_palette_gamma;
260 else if (temp>63) temp=63;
261 colors[i].g = temp * 4;
262 temp = (int)(*p++) + b + gr_palette_gamma;
264 else if (temp>63) temp=63;
265 colors[i].b = temp * 4;
268 SDL_SetColors(screen, colors, 0, 256);
271 //added on 980913 by adb to fix palette problems
272 // need a min without side effects...
274 static inline int min(int x, int y) { return x < y ? x : y; }
277 void gr_palette_load( ubyte *pal )
280 SDL_Palette *palette;
281 SDL_Color colors[256];
283 for (i=0; i<768; i++ ) {
284 gr_current_pal[i] = pal[i];
285 if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
288 palette = screen->format->palette;
290 if (palette == NULL) {
291 return; // Display is not palettised
294 for (i = 0, j = 0; j < 256; j++) {
295 //changed on 980913 by adb to fix palette problems
296 colors[j].r = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
297 colors[j].g = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
298 colors[j].b = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
301 SDL_SetColors(screen, colors, 0, 256);
303 gr_palette_faded_out = 0;
304 init_computed_colors();
309 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
313 fix fade_palette[768];
314 fix fade_palette_delta[768];
316 SDL_Palette *palette;
317 SDL_Color fade_colors[256];
319 if (gr_palette_faded_out) return 0;
321 palette = screen->format->palette;
322 if (palette == NULL) {
323 return -1; // Display is not palettised
326 if (pal==NULL) pal=gr_current_pal;
328 for (i=0; i<768; i++ ) {
329 gr_current_pal[i] = pal[i];
330 fade_palette[i] = i2f(pal[i]);
331 fade_palette_delta[i] = fade_palette[i] / nsteps;
333 for (j=0; j<nsteps; j++ ) {
334 for (i=0, k = 0; k<256; k++ ) {
335 fade_palette[i] -= fade_palette_delta[i];
336 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
337 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
338 c = f2i(fade_palette[i]);
340 fade_colors[k].r = c * 4;
343 fade_palette[i] -= fade_palette_delta[i];
344 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
345 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
346 c = f2i(fade_palette[i]);
348 fade_colors[k].g = c * 4;
351 fade_palette[i] -= fade_palette_delta[i];
352 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
353 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
354 c = f2i(fade_palette[i]);
356 fade_colors[k].b = c * 4;
360 SDL_SetColors(screen, fade_colors, 0, 256);
363 gr_palette_faded_out = 1;
369 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
371 int i, j, k, ncolors;
373 fix fade_palette[768];
374 fix fade_palette_delta[768];
376 SDL_Palette *palette;
377 SDL_Color fade_colors[256];
379 if (!gr_palette_faded_out) return 0;
381 palette = screen->format->palette;
383 if (palette == NULL) {
384 return -1; // Display is not palettised
387 ncolors = palette->ncolors;
389 for (i=0; i<768; i++ ) {
390 gr_current_pal[i] = pal[i];
392 fade_palette_delta[i] = i2f(pal[i]) / nsteps;
395 for (j=0; j<nsteps; j++ ) {
396 for (i=0, k = 0; k<256; k++ ) {
397 fade_palette[i] += fade_palette_delta[i];
398 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
399 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
400 c = f2i(fade_palette[i]);
402 fade_colors[k].r = c * 4;
405 fade_palette[i] += fade_palette_delta[i];
406 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
407 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
408 c = f2i(fade_palette[i]);
410 fade_colors[k].g = c * 4;
413 fade_palette[i] += fade_palette_delta[i];
414 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
415 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
416 c = f2i(fade_palette[i]);
418 fade_colors[k].b = c * 4;
422 SDL_SetColors(screen, fade_colors, 0, 256);
424 //added on 980913 by adb to fix palette problems
425 gr_palette_load(pal);
428 gr_palette_faded_out = 0;
434 void gr_palette_read(ubyte * pal)
436 SDL_Palette *palette;
439 palette = screen->format->palette;
441 if (palette == NULL) {
442 return; // Display is not palettised
445 for (i = 0, j=0; i < 256; i++) {
446 pal[j++] = palette->colors[i].r / 4;
447 pal[j++] = palette->colors[i].g / 4;
448 pal[j++] = palette->colors[i].b / 4;