1 /* $Id: gr.c,v 1.9 2003-01-15 02:42:41 btb Exp $ */
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
76 SDL_UpdateRect(screen,0,0,0,0);
79 extern int VGA_current_mode; // DPH: kludge - remove at all costs
81 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(DESCENT_VERSION, "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);
149 int gr_check_fullscreen(void){
150 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
153 int gr_toggle_fullscreen(void){
154 sdl_video_flags^=SDL_FULLSCREEN;
155 SDL_WM_ToggleFullScreen(screen);
156 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
161 // Only do this function once!
165 if (SDL_Init(SDL_INIT_VIDEO) < 0)
167 Error("SDL library video initialisation failed: %s.",SDL_GetError());
169 MALLOC( grd_curscreen,grs_screen,1 );
170 memset( grd_curscreen, 0, sizeof(grs_screen));
172 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
173 if (FindArg("-fullscreen"))
174 sdl_video_flags|=SDL_FULLSCREEN;
176 //added 05/19/99 Matt Mueller - make HW surface optional
177 if (FindArg("-hwsurface"))
178 sdl_video_flags|=SDL_HWSURFACE;
180 if (FindArg("-nosdlvidmodecheck"))
183 grd_curscreen->sc_canvas.cv_color = 0;
184 grd_curscreen->sc_canvas.cv_drawmode = 0;
185 grd_curscreen->sc_canvas.cv_font = NULL;
186 grd_curscreen->sc_canvas.cv_font_fg_color = 0;
187 grd_curscreen->sc_canvas.cv_font_bg_color = 0;
188 gr_set_current_canvas( &grd_curscreen->sc_canvas );
191 // added on 980913 by adb to add cleanup
193 // end changes by adb
203 d_free(grd_curscreen);
207 // Palette functions follow.
209 static int last_r=0, last_g=0, last_b=0;
211 void gr_palette_clear()
213 SDL_Palette *palette;
214 SDL_Color colors[256];
217 palette = screen->format->palette;
219 if (palette == NULL) {
220 return; // Display is not palettised
223 ncolors = palette->ncolors;
224 memset(colors, 0, ncolors * sizeof(SDL_Color));
226 SDL_SetColors(screen, colors, 0, 256);
228 gr_palette_faded_out = 1;
232 void gr_palette_step_up( int r, int g, int b )
235 ubyte *p = gr_palette;
238 SDL_Palette *palette;
239 SDL_Color colors[256];
241 if (gr_palette_faded_out) return;
243 if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
249 palette = screen->format->palette;
251 if (palette == NULL) {
252 return; // Display is not palettised
255 for (i=0; i<256; i++) {
256 temp = (int)(*p++) + r + gr_palette_gamma;
258 else if (temp>63) temp=63;
259 colors[i].r = temp * 4;
260 temp = (int)(*p++) + g + gr_palette_gamma;
262 else if (temp>63) temp=63;
263 colors[i].g = temp * 4;
264 temp = (int)(*p++) + b + gr_palette_gamma;
266 else if (temp>63) temp=63;
267 colors[i].b = temp * 4;
270 SDL_SetColors(screen, colors, 0, 256);
273 //added on 980913 by adb to fix palette problems
274 // need a min without side effects...
276 static inline int min(int x, int y) { return x < y ? x : y; }
279 void gr_palette_load( ubyte *pal )
282 SDL_Palette *palette;
283 SDL_Color colors[256];
285 for (i=0; i<768; i++ ) {
286 gr_current_pal[i] = pal[i];
287 if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
290 palette = screen->format->palette;
292 if (palette == NULL) {
293 return; // Display is not palettised
296 for (i = 0, j = 0; j < 256; j++) {
297 //changed on 980913 by adb to fix palette problems
298 colors[j].r = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
299 colors[j].g = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
300 colors[j].b = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
303 SDL_SetColors(screen, colors, 0, 256);
305 gr_palette_faded_out = 0;
306 init_computed_colors();
311 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
315 fix fade_palette[768];
316 fix fade_palette_delta[768];
318 SDL_Palette *palette;
319 SDL_Color fade_colors[256];
321 if (gr_palette_faded_out) return 0;
323 #if 1 //ifndef NDEBUG
324 if (grd_fades_disabled) {
330 palette = screen->format->palette;
331 if (palette == NULL) {
332 return -1; // Display is not palettised
335 if (pal==NULL) pal=gr_current_pal;
337 for (i=0; i<768; i++ ) {
338 gr_current_pal[i] = pal[i];
339 fade_palette[i] = i2f(pal[i]);
340 fade_palette_delta[i] = fade_palette[i] / nsteps;
342 for (j=0; j<nsteps; j++ ) {
343 for (i=0, k = 0; k<256; k++ ) {
344 fade_palette[i] -= fade_palette_delta[i];
345 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
346 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
347 c = f2i(fade_palette[i]);
349 fade_colors[k].r = c * 4;
352 fade_palette[i] -= fade_palette_delta[i];
353 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
354 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
355 c = f2i(fade_palette[i]);
357 fade_colors[k].g = c * 4;
360 fade_palette[i] -= fade_palette_delta[i];
361 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
362 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
363 c = f2i(fade_palette[i]);
365 fade_colors[k].b = c * 4;
369 SDL_SetColors(screen, fade_colors, 0, 256);
372 gr_palette_faded_out = 1;
378 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
380 int i, j, k, ncolors;
382 fix fade_palette[768];
383 fix fade_palette_delta[768];
385 SDL_Palette *palette;
386 SDL_Color fade_colors[256];
388 if (!gr_palette_faded_out) return 0;
390 #if 1 //ifndef NDEBUG
391 if (grd_fades_disabled) {
392 gr_palette_load(pal);
397 palette = screen->format->palette;
399 if (palette == NULL) {
400 return -1; // Display is not palettised
403 ncolors = palette->ncolors;
405 for (i=0; i<768; i++ ) {
406 gr_current_pal[i] = pal[i];
408 fade_palette_delta[i] = i2f(pal[i]) / nsteps;
411 for (j=0; j<nsteps; j++ ) {
412 for (i=0, k = 0; k<256; k++ ) {
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].r = c * 4;
421 fade_palette[i] += fade_palette_delta[i];
422 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
423 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
424 c = f2i(fade_palette[i]);
426 fade_colors[k].g = c * 4;
429 fade_palette[i] += fade_palette_delta[i];
430 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
431 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
432 c = f2i(fade_palette[i]);
434 fade_colors[k].b = c * 4;
438 SDL_SetColors(screen, fade_colors, 0, 256);
440 //added on 980913 by adb to fix palette problems
441 gr_palette_load(pal);
444 gr_palette_faded_out = 0;
450 void gr_palette_read(ubyte * pal)
452 SDL_Palette *palette;
455 palette = screen->format->palette;
457 if (palette == NULL) {
458 return; // Display is not palettised
461 for (i = 0, j=0; i < 256; i++) {
462 pal[j++] = palette->colors[i].r / 4;
463 pal[j++] = palette->colors[i].g / 4;
464 pal[j++] = palette->colors[i].b / 4;