2 * $Source: /cvs/cvsroot/d2x/arch/sdl/gr.c,v $
5 * $Date: 2002-02-23 21:38:15 $
9 * $Log: not supported by cvs2svn $
10 * Revision 1.4 2002/02/16 02:08:31 bradleyb
11 * allow older sdl versions
13 * Revision 1.3 2002/02/14 11:29:31 bradleyb
14 * allow gr_init lowres
16 * Revision 1.2 2001/10/31 07:41:54 bradleyb
19 * Revision 1.1 2001/10/25 08:25:34 bradleyb
20 * Finished moving stuff to arch/blah. I know, it's ugly, but It'll be easier to sync with d1x.
22 * Revision 1.5 2001/10/09 08:17:07 bradleyb
23 * changed window caption to include version info
25 * Revision 1.4 2001/01/31 13:59:23 bradleyb
26 * Fullscreen toggle added to screen res menu
28 * Revision 1.3 2001/01/29 13:47:52 bradleyb
29 * Fixed build, some minor cleanups.
41 #ifndef SDL_VERSION_ATLEAST
51 //added on 9/30/98 by Matt Mueller to set the title bar. Woohoo!
57 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
60 int sdl_video_flags = SDL_SWSURFACE | SDL_HWPALETTE;
61 char checkvidmodeok=0;
68 //added 05/19/99 Matt Mueller - locking stuff
72 int gr_testlocklevel=0;
74 inline void gr_dolock(const char *file,int line) {
76 if ( gr_testlocklevel==1 && SDL_MUSTLOCK(screen) ) {
78 chcksetwritable(screen.pixels,screen->w*screen->h*screen->format->BytesPerPixel);
80 if ( SDL_LockSurface(screen) < 0 )Error("could not lock screen (%s:%i)\n",file,line);
83 inline void gr_dounlock(void) {
85 if (gr_testlocklevel==0 && SDL_MUSTLOCK(screen) ) {
86 SDL_UnlockSurface(screen);
88 chcksetunwritable(screen.pixels,screen->w*screen->h*screen->format->BytesPerPixel);
95 void gr_palette_clear(); // Function prototype for gr_init;
100 //added 05/19/99 Matt Mueller - locking stuff
103 SDL_UpdateRect(screen,0,0,0,0);
106 extern int VGA_current_mode; // DPH: kludge - remove at all costs
108 int gr_set_mode(u_int32_t mode)
121 VGA_current_mode = mode;
123 if (screen != NULL) gr_palette_clear();
125 //added on 11/06/98 by Matt Mueller to set the title bar. (moved from below)
126 //sekmu: might wanna copy this litte blurb to one of the text files or something
127 //we want to set it here so that X window manager "Style" type commands work
128 //for example, in fvwm2 or fvwm95:
129 //Style "D1X*" NoTitle, NoHandles, BorderWidth 0
130 //if you can't use -fullscreen like me (crashes X), this is a big help in
131 //getting the window centered correctly (if you use SmartPlacement)
132 SDL_WM_SetCaption(DESCENT_VERSION, "Descent II");
135 //edited 10/05/98 by Matt Mueller - make fullscreen mode optional
136 // changed by adb on 980913: added SDL_HWPALETTE (should be option?)
137 // changed by someone on 980923 to add SDL_FULLSCREEN
138 if(!checkvidmodeok || SDL_VideoModeOK(w,h,8,sdl_video_flags)){
139 screen = SDL_SetVideoMode(w, h, 8, sdl_video_flags);
143 // end changes by someone
144 // end changes by adb
146 if (screen == NULL) {
147 Error("Could not set %dx%dx8 video mode\n",w,h);
150 memset( grd_curscreen, 0, sizeof(grs_screen));
151 grd_curscreen->sc_mode = mode;
152 grd_curscreen->sc_w = w;
153 grd_curscreen->sc_h = h;
154 grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
155 grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
156 grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
157 grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
158 grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
159 grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
160 grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_LINEAR;
161 grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
162 gr_set_current_canvas(NULL);
163 //gr_enable_default_palette_loading();
165 //added on 9/30/98 by Matt Mueller to hide the mouse if its over the game window
168 //--moved up--added on 9/30/98 by Matt Mueller to set the title bar. Woohoo!
169 //--moved up-- SDL_WM_SetCaption(DESCENT_VERSION " " D1X_DATE, NULL);
170 //--moved up--end addition -MM
172 // gamefont_choose_game_font(w,h);
176 int gr_check_fullscreen(void){
177 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
180 int gr_toggle_fullscreen(void){
181 sdl_video_flags^=SDL_FULLSCREEN;
182 #if SDL_VERSION_ATLEAST(1,0,5)
183 SDL_WM_ToggleFullScreen(screen);
185 grd_curscreen->sc_mode=0;//hack to get it to reset screen mode
187 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
193 int mode = MenuHires?SM(640,480):SM(320,200);
194 // Only do this function once!
198 if (SDL_Init(SDL_INIT_VIDEO) < 0)
200 Error("SDL library video initialisation failed: %s.",SDL_GetError());
202 MALLOC( grd_curscreen,grs_screen,1 );
203 memset( grd_curscreen, 0, sizeof(grs_screen));
205 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
206 if (FindArg("-fullscreen"))
207 sdl_video_flags|=SDL_FULLSCREEN;
209 //added 05/19/99 Matt Mueller - make HW surface optional
210 if (FindArg("-hwsurface"))
211 sdl_video_flags|=SDL_HWSURFACE;
213 if (FindArg("-nosdlvidmodecheck"))
217 if ((retcode=gr_set_mode(mode)))
221 grd_curscreen->sc_canvas.cv_color = 0;
222 grd_curscreen->sc_canvas.cv_drawmode = 0;
223 grd_curscreen->sc_canvas.cv_font = NULL;
224 grd_curscreen->sc_canvas.cv_font_fg_color = 0;
225 grd_curscreen->sc_canvas.cv_font_bg_color = 0;
226 gr_set_current_canvas( &grd_curscreen->sc_canvas );
229 // added on 980913 by adb to add cleanup
231 // end changes by adb
241 d_free(grd_curscreen);
245 // Palette functions follow.
247 static int last_r=0, last_g=0, last_b=0;
249 void gr_palette_clear()
251 SDL_Palette *palette;
252 SDL_Color colors[256];
255 palette = screen->format->palette;
257 if (palette == NULL) {
258 return; // Display is not palettised
261 ncolors = palette->ncolors;
262 memset(colors, 0, ncolors * sizeof(SDL_Color));
264 SDL_SetColors(screen, colors, 0, 256);
266 gr_palette_faded_out = 1;
270 void gr_palette_step_up( int r, int g, int b )
273 ubyte *p = gr_palette;
276 SDL_Palette *palette;
277 SDL_Color colors[256];
279 if (gr_palette_faded_out) return;
281 if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
287 palette = screen->format->palette;
289 if (palette == NULL) {
290 return; // Display is not palettised
293 for (i=0; i<256; i++) {
294 temp = (int)(*p++) + r + gr_palette_gamma;
296 else if (temp>63) temp=63;
297 colors[i].r = temp * 4;
298 temp = (int)(*p++) + g + gr_palette_gamma;
300 else if (temp>63) temp=63;
301 colors[i].g = temp * 4;
302 temp = (int)(*p++) + b + gr_palette_gamma;
304 else if (temp>63) temp=63;
305 colors[i].b = temp * 4;
308 SDL_SetColors(screen, colors, 0, 256);
311 //added on 980913 by adb to fix palette problems
312 // need a min without side effects...
314 static inline int min(int x, int y) { return x < y ? x : y; }
317 void gr_palette_load( ubyte *pal )
320 SDL_Palette *palette;
321 SDL_Color colors[256];
323 for (i=0; i<768; i++ ) {
324 gr_current_pal[i] = pal[i];
325 if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
328 palette = screen->format->palette;
330 if (palette == NULL) {
331 return; // Display is not palettised
334 for (i = 0, j = 0; j < 256; j++) {
335 //changed on 980913 by adb to fix palette problems
336 colors[j].r = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
337 colors[j].g = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
338 colors[j].b = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
341 SDL_SetColors(screen, colors, 0, 256);
343 gr_palette_faded_out = 0;
344 init_computed_colors();
349 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
353 fix fade_palette[768];
354 fix fade_palette_delta[768];
356 SDL_Palette *palette;
357 SDL_Color fade_colors[256];
359 if (gr_palette_faded_out) return 0;
361 palette = screen->format->palette;
362 if (palette == NULL) {
363 return -1; // Display is not palettised
366 if (pal==NULL) pal=gr_current_pal;
368 for (i=0; i<768; i++ ) {
369 gr_current_pal[i] = pal[i];
370 fade_palette[i] = i2f(pal[i]);
371 fade_palette_delta[i] = fade_palette[i] / nsteps;
373 for (j=0; j<nsteps; j++ ) {
374 for (i=0, k = 0; k<256; k++ ) {
375 fade_palette[i] -= fade_palette_delta[i];
376 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
377 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
378 c = f2i(fade_palette[i]);
380 fade_colors[k].r = c * 4;
383 fade_palette[i] -= fade_palette_delta[i];
384 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
385 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
386 c = f2i(fade_palette[i]);
388 fade_colors[k].g = c * 4;
391 fade_palette[i] -= fade_palette_delta[i];
392 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
393 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
394 c = f2i(fade_palette[i]);
396 fade_colors[k].b = c * 4;
400 SDL_SetColors(screen, fade_colors, 0, 256);
403 gr_palette_faded_out = 1;
409 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
411 int i, j, k, ncolors;
413 fix fade_palette[768];
414 fix fade_palette_delta[768];
416 SDL_Palette *palette;
417 SDL_Color fade_colors[256];
419 if (!gr_palette_faded_out) return 0;
421 palette = screen->format->palette;
423 if (palette == NULL) {
424 return -1; // Display is not palettised
427 ncolors = palette->ncolors;
429 for (i=0; i<768; i++ ) {
430 gr_current_pal[i] = pal[i];
432 fade_palette_delta[i] = i2f(pal[i]) / nsteps;
435 for (j=0; j<nsteps; j++ ) {
436 for (i=0, k = 0; k<256; k++ ) {
437 fade_palette[i] += fade_palette_delta[i];
438 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
439 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
440 c = f2i(fade_palette[i]);
442 fade_colors[k].r = c * 4;
445 fade_palette[i] += fade_palette_delta[i];
446 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
447 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
448 c = f2i(fade_palette[i]);
450 fade_colors[k].g = c * 4;
453 fade_palette[i] += fade_palette_delta[i];
454 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
455 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
456 c = f2i(fade_palette[i]);
458 fade_colors[k].b = c * 4;
462 SDL_SetColors(screen, fade_colors, 0, 256);
464 //added on 980913 by adb to fix palette problems
465 gr_palette_load(pal);
468 gr_palette_faded_out = 0;
474 void gr_palette_read(ubyte * pal)
476 SDL_Palette *palette;
479 palette = screen->format->palette;
481 if (palette == NULL) {
482 return; // Display is not palettised
485 for (i = 0, j=0; i < 256; i++) {
486 pal[j++] = palette->colors[i].r / 4;
487 pal[j++] = palette->colors[i].g / 4;
488 pal[j++] = palette->colors[i].b / 4;