2 * $Source: /cvs/cvsroot/d2x/arch/sdl/gr.c,v $
5 * $Date: 2002-02-14 11:29:31 $
9 * $Log: not supported by cvs2svn $
10 * Revision 1.2 2001/10/31 07:41:54 bradleyb
13 * Revision 1.1 2001/10/25 08:25:34 bradleyb
14 * Finished moving stuff to arch/blah. I know, it's ugly, but It'll be easier to sync with d1x.
16 * Revision 1.5 2001/10/09 08:17:07 bradleyb
17 * changed window caption to include version info
19 * Revision 1.4 2001/01/31 13:59:23 bradleyb
20 * Fullscreen toggle added to screen res menu
22 * Revision 1.3 2001/01/29 13:47:52 bradleyb
23 * Fixed build, some minor cleanups.
41 //added on 9/30/98 by Matt Mueller to set the title bar. Woohoo!
47 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
50 int sdl_video_flags = SDL_SWSURFACE | SDL_HWPALETTE;
51 char checkvidmodeok=0;
58 //added 05/19/99 Matt Mueller - locking stuff
62 int gr_testlocklevel=0;
64 inline void gr_dolock(const char *file,int line) {
66 if ( gr_testlocklevel==1 && SDL_MUSTLOCK(screen) ) {
68 chcksetwritable(screen.pixels,screen->w*screen->h*screen->format->BytesPerPixel);
70 if ( SDL_LockSurface(screen) < 0 )Error("could not lock screen (%s:%i)\n",file,line);
73 inline void gr_dounlock(void) {
75 if (gr_testlocklevel==0 && SDL_MUSTLOCK(screen) ) {
76 SDL_UnlockSurface(screen);
78 chcksetunwritable(screen.pixels,screen->w*screen->h*screen->format->BytesPerPixel);
85 void gr_palette_clear(); // Function prototype for gr_init;
90 //added 05/19/99 Matt Mueller - locking stuff
93 SDL_UpdateRect(screen,0,0,0,0);
96 extern int VGA_current_mode; // DPH: kludge - remove at all costs
98 int gr_set_mode(u_int32_t mode)
111 VGA_current_mode = mode;
113 if (screen != NULL) gr_palette_clear();
115 //added on 11/06/98 by Matt Mueller to set the title bar. (moved from below)
116 //sekmu: might wanna copy this litte blurb to one of the text files or something
117 //we want to set it here so that X window manager "Style" type commands work
118 //for example, in fvwm2 or fvwm95:
119 //Style "D1X*" NoTitle, NoHandles, BorderWidth 0
120 //if you can't use -fullscreen like me (crashes X), this is a big help in
121 //getting the window centered correctly (if you use SmartPlacement)
122 SDL_WM_SetCaption(DESCENT_VERSION, "Descent II");
125 //edited 10/05/98 by Matt Mueller - make fullscreen mode optional
126 // changed by adb on 980913: added SDL_HWPALETTE (should be option?)
127 // changed by someone on 980923 to add SDL_FULLSCREEN
128 if(!checkvidmodeok || SDL_VideoModeOK(w,h,8,sdl_video_flags)){
129 screen = SDL_SetVideoMode(w, h, 8, sdl_video_flags);
133 // end changes by someone
134 // end changes by adb
136 if (screen == NULL) {
137 Error("Could not set %dx%dx8 video mode\n",w,h);
140 memset( grd_curscreen, 0, sizeof(grs_screen));
141 grd_curscreen->sc_mode = mode;
142 grd_curscreen->sc_w = w;
143 grd_curscreen->sc_h = h;
144 grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
145 grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
146 grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
147 grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
148 grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
149 grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
150 grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_LINEAR;
151 grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
152 gr_set_current_canvas(NULL);
153 //gr_enable_default_palette_loading();
155 //added on 9/30/98 by Matt Mueller to hide the mouse if its over the game window
158 //--moved up--added on 9/30/98 by Matt Mueller to set the title bar. Woohoo!
159 //--moved up-- SDL_WM_SetCaption(DESCENT_VERSION " " D1X_DATE, NULL);
160 //--moved up--end addition -MM
162 // gamefont_choose_game_font(w,h);
166 int gr_check_fullscreen(void){
167 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
170 int gr_toggle_fullscreen(void){
171 sdl_video_flags^=SDL_FULLSCREEN;
172 // grd_curscreen->sc_mode=0;//hack to get it to reset screen mode
173 SDL_WM_ToggleFullScreen(screen);
174 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
180 int mode = MenuHires?SM(640,480):SM(320,200);
181 // Only do this function once!
185 if (SDL_Init(SDL_INIT_VIDEO) < 0)
187 Error("SDL library video initialisation failed: %s.",SDL_GetError());
189 MALLOC( grd_curscreen,grs_screen,1 );
190 memset( grd_curscreen, 0, sizeof(grs_screen));
192 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
193 if (FindArg("-fullscreen"))
194 sdl_video_flags|=SDL_FULLSCREEN;
196 //added 05/19/99 Matt Mueller - make HW surface optional
197 if (FindArg("-hwsurface"))
198 sdl_video_flags|=SDL_HWSURFACE;
200 if (FindArg("-nosdlvidmodecheck"))
204 if ((retcode=gr_set_mode(mode)))
208 grd_curscreen->sc_canvas.cv_color = 0;
209 grd_curscreen->sc_canvas.cv_drawmode = 0;
210 grd_curscreen->sc_canvas.cv_font = NULL;
211 grd_curscreen->sc_canvas.cv_font_fg_color = 0;
212 grd_curscreen->sc_canvas.cv_font_bg_color = 0;
213 gr_set_current_canvas( &grd_curscreen->sc_canvas );
216 // added on 980913 by adb to add cleanup
218 // end changes by adb
228 d_free(grd_curscreen);
232 // Palette functions follow.
234 static int last_r=0, last_g=0, last_b=0;
236 void gr_palette_clear()
238 SDL_Palette *palette;
239 SDL_Color colors[256];
242 palette = screen->format->palette;
244 if (palette == NULL) {
245 return; // Display is not palettised
248 ncolors = palette->ncolors;
249 memset(colors, 0, ncolors * sizeof(SDL_Color));
251 SDL_SetColors(screen, colors, 0, 256);
253 gr_palette_faded_out = 1;
257 void gr_palette_step_up( int r, int g, int b )
260 ubyte *p = gr_palette;
263 SDL_Palette *palette;
264 SDL_Color colors[256];
266 if (gr_palette_faded_out) return;
268 if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
274 palette = screen->format->palette;
276 if (palette == NULL) {
277 return; // Display is not palettised
280 for (i=0; i<256; i++) {
281 temp = (int)(*p++) + r + gr_palette_gamma;
283 else if (temp>63) temp=63;
284 colors[i].r = temp * 4;
285 temp = (int)(*p++) + g + gr_palette_gamma;
287 else if (temp>63) temp=63;
288 colors[i].g = temp * 4;
289 temp = (int)(*p++) + b + gr_palette_gamma;
291 else if (temp>63) temp=63;
292 colors[i].b = temp * 4;
295 SDL_SetColors(screen, colors, 0, 256);
298 //added on 980913 by adb to fix palette problems
299 // need a min without side effects...
301 static inline int min(int x, int y) { return x < y ? x : y; }
304 void gr_palette_load( ubyte *pal )
307 SDL_Palette *palette;
308 SDL_Color colors[256];
310 for (i=0; i<768; i++ ) {
311 gr_current_pal[i] = pal[i];
312 if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
315 palette = screen->format->palette;
317 if (palette == NULL) {
318 return; // Display is not palettised
321 for (i = 0, j = 0; j < 256; j++) {
322 //changed on 980913 by adb to fix palette problems
323 colors[j].r = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
324 colors[j].g = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
325 colors[j].b = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
328 SDL_SetColors(screen, colors, 0, 256);
330 gr_palette_faded_out = 0;
331 init_computed_colors();
336 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
340 fix fade_palette[768];
341 fix fade_palette_delta[768];
343 SDL_Palette *palette;
344 SDL_Color fade_colors[256];
346 if (gr_palette_faded_out) return 0;
348 palette = screen->format->palette;
349 if (palette == NULL) {
350 return -1; // Display is not palettised
353 if (pal==NULL) pal=gr_current_pal;
355 for (i=0; i<768; i++ ) {
356 gr_current_pal[i] = pal[i];
357 fade_palette[i] = i2f(pal[i]);
358 fade_palette_delta[i] = fade_palette[i] / nsteps;
360 for (j=0; j<nsteps; j++ ) {
361 for (i=0, k = 0; k<256; k++ ) {
362 fade_palette[i] -= fade_palette_delta[i];
363 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
364 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
365 c = f2i(fade_palette[i]);
367 fade_colors[k].r = c * 4;
370 fade_palette[i] -= fade_palette_delta[i];
371 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
372 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
373 c = f2i(fade_palette[i]);
375 fade_colors[k].g = c * 4;
378 fade_palette[i] -= fade_palette_delta[i];
379 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
380 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
381 c = f2i(fade_palette[i]);
383 fade_colors[k].b = c * 4;
387 SDL_SetColors(screen, fade_colors, 0, 256);
390 gr_palette_faded_out = 1;
396 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
398 int i, j, k, ncolors;
400 fix fade_palette[768];
401 fix fade_palette_delta[768];
403 SDL_Palette *palette;
404 SDL_Color fade_colors[256];
406 if (!gr_palette_faded_out) return 0;
408 palette = screen->format->palette;
410 if (palette == NULL) {
411 return -1; // Display is not palettised
414 ncolors = palette->ncolors;
416 for (i=0; i<768; i++ ) {
417 gr_current_pal[i] = pal[i];
419 fade_palette_delta[i] = i2f(pal[i]) / nsteps;
422 for (j=0; j<nsteps; j++ ) {
423 for (i=0, k = 0; k<256; k++ ) {
424 fade_palette[i] += fade_palette_delta[i];
425 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
426 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
427 c = f2i(fade_palette[i]);
429 fade_colors[k].r = c * 4;
432 fade_palette[i] += fade_palette_delta[i];
433 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
434 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
435 c = f2i(fade_palette[i]);
437 fade_colors[k].g = c * 4;
440 fade_palette[i] += fade_palette_delta[i];
441 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
442 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
443 c = f2i(fade_palette[i]);
445 fade_colors[k].b = c * 4;
449 SDL_SetColors(screen, fade_colors, 0, 256);
451 //added on 980913 by adb to fix palette problems
452 gr_palette_load(pal);
455 gr_palette_faded_out = 0;
461 void gr_palette_read(ubyte * pal)
463 SDL_Palette *palette;
466 palette = screen->format->palette;
468 if (palette == NULL) {
469 return; // Display is not palettised
472 for (i = 0, j=0; i < 256; i++) {
473 pal[j++] = palette->colors[i].r / 4;
474 pal[j++] = palette->colors[i].g / 4;
475 pal[j++] = palette->colors[i].b / 4;