2 * $Source: /cvs/cvsroot/d2x/arch/sdl/gr.c,v $
5 * $Date: 2001-10-31 07:41:54 $
9 * $Log: not supported by cvs2svn $
10 * Revision 1.1 2001/10/25 08:25:34 bradleyb
11 * Finished moving stuff to arch/blah. I know, it's ugly, but It'll be easier to sync with d1x.
13 * Revision 1.5 2001/10/09 08:17:07 bradleyb
14 * changed window caption to include version info
16 * Revision 1.4 2001/01/31 13:59:23 bradleyb
17 * Fullscreen toggle added to screen res menu
19 * Revision 1.3 2001/01/29 13:47:52 bradleyb
20 * Fixed build, some minor cleanups.
36 //added on 9/30/98 by Matt Mueller to set the title bar. Woohoo!
42 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
45 int sdl_video_flags = SDL_SWSURFACE | SDL_HWPALETTE;
46 char checkvidmodeok=0;
53 //added 05/19/99 Matt Mueller - locking stuff
57 int gr_testlocklevel=0;
59 inline void gr_dolock(const char *file,int line) {
61 if ( gr_testlocklevel==1 && SDL_MUSTLOCK(screen) ) {
63 chcksetwritable(screen.pixels,screen->w*screen->h*screen->format->BytesPerPixel);
65 if ( SDL_LockSurface(screen) < 0 )Error("could not lock screen (%s:%i)\n",file,line);
68 inline void gr_dounlock(void) {
70 if (gr_testlocklevel==0 && SDL_MUSTLOCK(screen) ) {
71 SDL_UnlockSurface(screen);
73 chcksetunwritable(screen.pixels,screen->w*screen->h*screen->format->BytesPerPixel);
80 void gr_palette_clear(); // Function prototype for gr_init;
85 //added 05/19/99 Matt Mueller - locking stuff
88 SDL_UpdateRect(screen,0,0,0,0);
91 extern int VGA_current_mode; // DPH: kludge - remove at all costs
93 int gr_set_mode(u_int32_t mode)
106 VGA_current_mode = mode;
108 if (screen != NULL) gr_palette_clear();
110 //added on 11/06/98 by Matt Mueller to set the title bar. (moved from below)
111 //sekmu: might wanna copy this litte blurb to one of the text files or something
112 //we want to set it here so that X window manager "Style" type commands work
113 //for example, in fvwm2 or fvwm95:
114 //Style "D1X*" NoTitle, NoHandles, BorderWidth 0
115 //if you can't use -fullscreen like me (crashes X), this is a big help in
116 //getting the window centered correctly (if you use SmartPlacement)
117 SDL_WM_SetCaption(DESCENT_VERSION, "Descent II");
120 //edited 10/05/98 by Matt Mueller - make fullscreen mode optional
121 // changed by adb on 980913: added SDL_HWPALETTE (should be option?)
122 // changed by someone on 980923 to add SDL_FULLSCREEN
123 if(!checkvidmodeok || SDL_VideoModeOK(w,h,8,sdl_video_flags)){
124 screen = SDL_SetVideoMode(w, h, 8, sdl_video_flags);
128 // end changes by someone
129 // end changes by adb
131 if (screen == NULL) {
132 Error("Could not set %dx%dx8 video mode\n",w,h);
135 memset( grd_curscreen, 0, sizeof(grs_screen));
136 grd_curscreen->sc_mode = mode;
137 grd_curscreen->sc_w = w;
138 grd_curscreen->sc_h = h;
139 grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
140 grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
141 grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
142 grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
143 grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
144 grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
145 grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_LINEAR;
146 grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
147 gr_set_current_canvas(NULL);
148 //gr_enable_default_palette_loading();
150 //added on 9/30/98 by Matt Mueller to hide the mouse if its over the game window
153 //--moved up--added on 9/30/98 by Matt Mueller to set the title bar. Woohoo!
154 //--moved up-- SDL_WM_SetCaption(DESCENT_VERSION " " D1X_DATE, NULL);
155 //--moved up--end addition -MM
157 // gamefont_choose_game_font(w,h);
161 int gr_check_fullscreen(void){
162 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
165 int gr_toggle_fullscreen(void){
166 sdl_video_flags^=SDL_FULLSCREEN;
167 // grd_curscreen->sc_mode=0;//hack to get it to reset screen mode
168 SDL_WM_ToggleFullScreen(screen);
169 return (sdl_video_flags & SDL_FULLSCREEN)?1:0;
175 int mode = SM(640,480);
176 // Only do this function once!
180 if (SDL_Init(SDL_INIT_VIDEO) < 0)
182 Error("SDL library video initialisation failed: %s.",SDL_GetError());
184 MALLOC( grd_curscreen,grs_screen,1 );
185 memset( grd_curscreen, 0, sizeof(grs_screen));
187 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
188 if (FindArg("-fullscreen"))
189 sdl_video_flags|=SDL_FULLSCREEN;
191 //added 05/19/99 Matt Mueller - make HW surface optional
192 if (FindArg("-hwsurface"))
193 sdl_video_flags|=SDL_HWSURFACE;
195 if (FindArg("-nosdlvidmodecheck"))
199 if ((retcode=gr_set_mode(mode)))
203 grd_curscreen->sc_canvas.cv_color = 0;
204 grd_curscreen->sc_canvas.cv_drawmode = 0;
205 grd_curscreen->sc_canvas.cv_font = NULL;
206 grd_curscreen->sc_canvas.cv_font_fg_color = 0;
207 grd_curscreen->sc_canvas.cv_font_bg_color = 0;
208 gr_set_current_canvas( &grd_curscreen->sc_canvas );
211 // added on 980913 by adb to add cleanup
213 // end changes by adb
223 d_free(grd_curscreen);
227 // Palette functions follow.
229 static int last_r=0, last_g=0, last_b=0;
231 void gr_palette_clear()
233 SDL_Palette *palette;
234 SDL_Color colors[256];
237 palette = screen->format->palette;
239 if (palette == NULL) {
240 return; // Display is not palettised
243 ncolors = palette->ncolors;
244 memset(colors, 0, ncolors * sizeof(SDL_Color));
246 SDL_SetColors(screen, colors, 0, 256);
248 gr_palette_faded_out = 1;
252 void gr_palette_step_up( int r, int g, int b )
255 ubyte *p = gr_palette;
258 SDL_Palette *palette;
259 SDL_Color colors[256];
261 if (gr_palette_faded_out) return;
263 if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
269 palette = screen->format->palette;
271 if (palette == NULL) {
272 return; // Display is not palettised
275 for (i=0; i<256; i++) {
276 temp = (int)(*p++) + r + gr_palette_gamma;
278 else if (temp>63) temp=63;
279 colors[i].r = temp * 4;
280 temp = (int)(*p++) + g + gr_palette_gamma;
282 else if (temp>63) temp=63;
283 colors[i].g = temp * 4;
284 temp = (int)(*p++) + b + gr_palette_gamma;
286 else if (temp>63) temp=63;
287 colors[i].b = temp * 4;
290 SDL_SetColors(screen, colors, 0, 256);
293 //added on 980913 by adb to fix palette problems
294 // need a min without side effects...
296 static inline int min(int x, int y) { return x < y ? x : y; }
299 void gr_palette_load( ubyte *pal )
302 SDL_Palette *palette;
303 SDL_Color colors[256];
305 for (i=0; i<768; i++ ) {
306 gr_current_pal[i] = pal[i];
307 if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
310 palette = screen->format->palette;
312 if (palette == NULL) {
313 return; // Display is not palettised
316 for (i = 0, j = 0; j < 256; j++) {
317 //changed on 980913 by adb to fix palette problems
318 colors[j].r = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
319 colors[j].g = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
320 colors[j].b = (min(gr_current_pal[i++] + gr_palette_gamma, 63)) * 4;
323 SDL_SetColors(screen, colors, 0, 256);
325 gr_palette_faded_out = 0;
326 init_computed_colors();
331 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
335 fix fade_palette[768];
336 fix fade_palette_delta[768];
338 SDL_Palette *palette;
339 SDL_Color fade_colors[256];
341 if (gr_palette_faded_out) return 0;
343 palette = screen->format->palette;
344 if (palette == NULL) {
345 return -1; // Display is not palettised
348 if (pal==NULL) pal=gr_current_pal;
350 for (i=0; i<768; i++ ) {
351 gr_current_pal[i] = pal[i];
352 fade_palette[i] = i2f(pal[i]);
353 fade_palette_delta[i] = fade_palette[i] / nsteps;
355 for (j=0; j<nsteps; j++ ) {
356 for (i=0, k = 0; k<256; k++ ) {
357 fade_palette[i] -= fade_palette_delta[i];
358 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
359 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
360 c = f2i(fade_palette[i]);
362 fade_colors[k].r = c * 4;
365 fade_palette[i] -= fade_palette_delta[i];
366 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
367 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
368 c = f2i(fade_palette[i]);
370 fade_colors[k].g = c * 4;
373 fade_palette[i] -= fade_palette_delta[i];
374 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
375 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
376 c = f2i(fade_palette[i]);
378 fade_colors[k].b = c * 4;
382 SDL_SetColors(screen, fade_colors, 0, 256);
385 gr_palette_faded_out = 1;
391 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
393 int i, j, k, ncolors;
395 fix fade_palette[768];
396 fix fade_palette_delta[768];
398 SDL_Palette *palette;
399 SDL_Color fade_colors[256];
401 if (!gr_palette_faded_out) return 0;
403 palette = screen->format->palette;
405 if (palette == NULL) {
406 return -1; // Display is not palettised
409 ncolors = palette->ncolors;
411 for (i=0; i<768; i++ ) {
412 gr_current_pal[i] = pal[i];
414 fade_palette_delta[i] = i2f(pal[i]) / nsteps;
417 for (j=0; j<nsteps; j++ ) {
418 for (i=0, k = 0; k<256; k++ ) {
419 fade_palette[i] += fade_palette_delta[i];
420 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
421 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
422 c = f2i(fade_palette[i]);
424 fade_colors[k].r = c * 4;
427 fade_palette[i] += fade_palette_delta[i];
428 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
429 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
430 c = f2i(fade_palette[i]);
432 fade_colors[k].g = c * 4;
435 fade_palette[i] += fade_palette_delta[i];
436 if (fade_palette[i] > i2f(pal[i] + gr_palette_gamma) )
437 fade_palette[i] = i2f(pal[i] + gr_palette_gamma);
438 c = f2i(fade_palette[i]);
440 fade_colors[k].b = c * 4;
444 SDL_SetColors(screen, fade_colors, 0, 256);
446 //added on 980913 by adb to fix palette problems
447 gr_palette_load(pal);
450 gr_palette_faded_out = 0;
456 void gr_palette_read(ubyte * pal)
458 SDL_Palette *palette;
461 palette = screen->format->palette;
463 if (palette == NULL) {
464 return; // Display is not palettised
467 for (i = 0, j=0; i < 256; i++) {
468 pal[j++] = palette->colors[i].r / 4;
469 pal[j++] = palette->colors[i].g / 4;
470 pal[j++] = palette->colors[i].b / 4;