test/graywin.c
changeset 691 609543e2b3a1
parent 538 d3abe873e3f7
child 945 d33645c36072
     1.1 --- a/test/graywin.c	Sat Aug 23 23:12:19 2003 +0000
     1.2 +++ b/test/graywin.c	Sat Aug 23 23:18:49 2003 +0000
     1.3 @@ -20,6 +20,7 @@
     1.4  	static unsigned int seeded = 0;
     1.5  	SDL_Rect area;
     1.6  	Uint32 color;
     1.7 +        Uint32 randc;
     1.8  
     1.9  	/* Seed the random number generator */
    1.10  	if ( seeded == 0 ) {
    1.11 @@ -32,7 +33,16 @@
    1.12  	area.h = (rand()%height);
    1.13  	area.x = X-(area.w/2);
    1.14  	area.y = Y-(area.h/2);
    1.15 -	color = (rand()%NUM_COLORS);
    1.16 +        randc = (rand()%NUM_COLORS);
    1.17 +
    1.18 +        if (screen->format->BytesPerPixel==1)
    1.19 +        {
    1.20 +            color = randc;
    1.21 +        }
    1.22 +        else
    1.23 +        {
    1.24 +            color = SDL_MapRGB(screen->format, randc, randc, randc);
    1.25 +        }
    1.26  
    1.27  	/* Do it! */
    1.28  	SDL_FillRect(screen, &area, color);
    1.29 @@ -43,12 +53,60 @@
    1.30  	}
    1.31  }
    1.32  
    1.33 +void DrawBackground(SDL_Surface *screen)
    1.34 +{
    1.35 +	int i, j, k;
    1.36 +	Uint8  *buffer;
    1.37 +	Uint16 *buffer16;
    1.38 +        Uint16 color;
    1.39 +        Uint8  gradient;
    1.40 +
    1.41 +	/* Set the surface pixels and refresh! */
    1.42 +	/* Use two loops in case the surface is double-buffered (both sides) */
    1.43 +
    1.44 +	for ( j=0; j<2; ++j ) {
    1.45 +		if ( SDL_LockSurface(screen) < 0 ) {
    1.46 +			fprintf(stderr, "Couldn't lock display surface: %s\n",
    1.47 +								SDL_GetError());
    1.48 +			return;
    1.49 +		}
    1.50 +		buffer = (Uint8 *)screen->pixels;
    1.51 +
    1.52 +		if (screen->format->BytesPerPixel!=2) {
    1.53 +			for ( i=0; i<screen->h; ++i ) {
    1.54 +				memset(buffer,(i*(NUM_COLORS-1))/screen->h, screen->w * screen->format->BytesPerPixel);
    1.55 +				buffer += screen->pitch;
    1.56 +			}
    1.57 +		}
    1.58 +                else
    1.59 +                {
    1.60 +			for ( i=0; i<screen->h; ++i ) {
    1.61 +				gradient=((i*(NUM_COLORS-1))/screen->h);
    1.62 +                                color = SDL_MapRGB(screen->format, gradient, gradient, gradient);
    1.63 +                                buffer16=(Uint16*)buffer;
    1.64 +                                for (k=0; k<screen->w; k++)
    1.65 +                                {
    1.66 +                                   *(buffer16+k)=color;
    1.67 +                                }
    1.68 +				buffer += screen->pitch;
    1.69 +			}
    1.70 +                }
    1.71 +
    1.72 +		SDL_UnlockSurface(screen);
    1.73 +		if ( screen->flags & SDL_DOUBLEBUF ) {
    1.74 +			SDL_Flip(screen);
    1.75 +		} else {
    1.76 +			SDL_UpdateRect(screen, 0, 0, 0, 0);
    1.77 +                        break;
    1.78 +		}
    1.79 +	}
    1.80 +}
    1.81 +
    1.82  SDL_Surface *CreateScreen(Uint16 w, Uint16 h, Uint8 bpp, Uint32 flags)
    1.83  {
    1.84  	SDL_Surface *screen;
    1.85  	int i;
    1.86  	SDL_Color palette[NUM_COLORS];
    1.87 -	Uint8 *buffer;
    1.88  
    1.89  	/* Set the video mode */
    1.90  	screen = SDL_SetVideoMode(w, h, bpp, flags);
    1.91 @@ -60,33 +118,14 @@
    1.92  	fprintf(stderr, "Screen is in %s mode\n",
    1.93  		(screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
    1.94  
    1.95 -	/* Set a gray colormap, reverse order from white to black */
    1.96 -	for ( i=0; i<NUM_COLORS; ++i ) {
    1.97 -		palette[i].r = (NUM_COLORS-1)-i * (256 / NUM_COLORS);
    1.98 -		palette[i].g = (NUM_COLORS-1)-i * (256 / NUM_COLORS);
    1.99 -		palette[i].b = (NUM_COLORS-1)-i * (256 / NUM_COLORS);
   1.100 -	}
   1.101 -	SDL_SetColors(screen, palette, 0, NUM_COLORS);
   1.102 -
   1.103 -	/* Set the surface pixels and refresh! */
   1.104 -	/* Use two loops in case the surface is double-buffered (both sides) */
   1.105 -	for ( i=0; i<2; ++i ) {
   1.106 -		if ( SDL_LockSurface(screen) < 0 ) {
   1.107 -			fprintf(stderr, "Couldn't lock display surface: %s\n",
   1.108 -								SDL_GetError());
   1.109 -			return(NULL);
   1.110 +	if (bpp==8) {
   1.111 +		/* Set a gray colormap, reverse order from white to black */
   1.112 +		for ( i=0; i<NUM_COLORS; ++i ) {
   1.113 +			palette[i].r = (NUM_COLORS-1)-i * (256 / NUM_COLORS);
   1.114 +			palette[i].g = (NUM_COLORS-1)-i * (256 / NUM_COLORS);
   1.115 +			palette[i].b = (NUM_COLORS-1)-i * (256 / NUM_COLORS);
   1.116  		}
   1.117 -		buffer = (Uint8 *)screen->pixels;
   1.118 -		for ( i=0; i<screen->h; ++i ) {
   1.119 -			memset(buffer,(i*(NUM_COLORS-1))/screen->h, screen->w * screen->format->BytesPerPixel);
   1.120 -			buffer += screen->pitch;
   1.121 -		}
   1.122 -		SDL_UnlockSurface(screen);
   1.123 -		if ( screen->flags & SDL_DOUBLEBUF ) {
   1.124 -			SDL_Flip(screen);
   1.125 -		} else {
   1.126 -			SDL_UpdateRect(screen, 0, 0, 0, 0);
   1.127 -		}
   1.128 +		SDL_SetColors(screen, palette, 0, NUM_COLORS);
   1.129  	}
   1.130  
   1.131  	return(screen);
   1.132 @@ -151,6 +190,8 @@
   1.133  	if ( screen == NULL ) {
   1.134  		exit(2);
   1.135  	}
   1.136 +        
   1.137 +        DrawBackground(screen);
   1.138  		
   1.139  	/* Wait for a keystroke */
   1.140  	done = 0;
   1.141 @@ -182,12 +223,16 @@
   1.142  					"Couldn't toggle fullscreen mode\n");
   1.143  						done = 1;
   1.144  					}
   1.145 +                                        DrawBackground(screen);
   1.146  					break;
   1.147  				}
   1.148  				/* Any other key quits the application... */
   1.149  			case SDL_QUIT:
   1.150  				done = 1;
   1.151  				break;
   1.152 +			case SDL_VIDEOEXPOSE:
   1.153 +				DrawBackground(screen);
   1.154 +				break;
   1.155  			default:
   1.156  				break;
   1.157  		}