test/testalpha.c
changeset 1293 23b1ef7d003b
parent 1152 51a8702d8ecd
child 1465 8dfa9a6d69a5
     1.1 --- a/test/testalpha.c	Sun Jan 29 22:23:54 2006 +0000
     1.2 +++ b/test/testalpha.c	Sun Jan 29 23:06:05 2006 +0000
     1.3 @@ -19,6 +19,45 @@
     1.4  	exit(rc);
     1.5  }
     1.6  
     1.7 +/* Fill the screen with a gradient */
     1.8 +static void FillBackground(SDL_Surface *screen)
     1.9 +{
    1.10 +	Uint8 *buffer;
    1.11 +	Uint16 *buffer16;
    1.12 +        Uint16 color;
    1.13 +        Uint8  gradient;
    1.14 +	int    i, k;
    1.15 +
    1.16 +	/* Set the surface pixels and refresh! */
    1.17 +	if ( SDL_LockSurface(screen) < 0 ) {
    1.18 +		fprintf(stderr, "Couldn't lock the display surface: %s\n",
    1.19 +							SDL_GetError());
    1.20 +		quit(2);
    1.21 +	}
    1.22 +	buffer=(Uint8 *)screen->pixels;
    1.23 +	if (screen->format->BytesPerPixel!=2) {
    1.24 +		for ( i=0; i<screen->h; ++i ) {
    1.25 +			memset(buffer,(i*255)/screen->h, screen->w*screen->format->BytesPerPixel);
    1.26 +			buffer += screen->pitch;
    1.27 +		}
    1.28 +	}
    1.29 +        else
    1.30 +        {
    1.31 +		for ( i=0; i<screen->h; ++i ) {
    1.32 +			gradient=((i*255)/screen->h);
    1.33 +                        color = SDL_MapRGB(screen->format, gradient, gradient, gradient);
    1.34 +                        buffer16=(Uint16*)buffer;
    1.35 +                        for (k=0; k<screen->w; k++)
    1.36 +                        {
    1.37 +                            *(buffer16+k)=color;
    1.38 +                        }
    1.39 +			buffer += screen->pitch;
    1.40 +		}
    1.41 +        }
    1.42 +
    1.43 +	SDL_UnlockSurface(screen);
    1.44 +	SDL_UpdateRect(screen, 0, 0, 0, 0);
    1.45 +}
    1.46  
    1.47  /* Create a "light" -- a yellowish surface with variable alpha */
    1.48  SDL_Surface *CreateLight(SDL_Surface *screen, int radius)
    1.49 @@ -284,17 +323,14 @@
    1.50  {
    1.51  	const SDL_VideoInfo *info;
    1.52  	SDL_Surface *screen;
    1.53 +	int    w, h;
    1.54  	Uint8  video_bpp;
    1.55  	Uint32 videoflags;
    1.56 -	Uint8 *buffer;
    1.57 -	int    i, k, done;
    1.58 +	int    i, done;
    1.59  	SDL_Event event;
    1.60  	SDL_Surface *light;
    1.61  	int mouse_pressed;
    1.62  	Uint32 ticks, lastticks;
    1.63 -	Uint16 *buffer16;
    1.64 -        Uint16 color;
    1.65 -        Uint8  gradient;
    1.66  
    1.67  
    1.68  	/* Initialize SDL */
    1.69 @@ -304,6 +340,8 @@
    1.70  	}
    1.71  
    1.72  	/* Alpha blending doesn't work well at 8-bit color */
    1.73 +	w = 640;
    1.74 +	h = 480;
    1.75  	info = SDL_GetVideoInfo();
    1.76  	if ( info->vfmt->BitsPerPixel > 8 ) {
    1.77  		video_bpp = info->vfmt->BitsPerPixel;
    1.78 @@ -312,27 +350,37 @@
    1.79                  fprintf(stderr, "forced 16 bpp mode\n");
    1.80  	}
    1.81  	videoflags = SDL_SWSURFACE;
    1.82 -	while ( argc > 1 ) {
    1.83 -		--argc;
    1.84 -		if ( strcmp(argv[argc-1], "-bpp") == 0 ) {
    1.85 -			video_bpp = atoi(argv[argc]);
    1.86 +	for ( i = 1; argv[i]; ++i ) {
    1.87 +		if ( strcmp(argv[i], "-bpp") == 0 ) {
    1.88 +			video_bpp = atoi(argv[++i]);
    1.89                          if (video_bpp<=8) {
    1.90                              video_bpp=16;
    1.91                              fprintf(stderr, "forced 16 bpp mode\n");
    1.92                          }
    1.93 -			--argc;
    1.94  		} else
    1.95 -		if ( strcmp(argv[argc], "-hw") == 0 ) {
    1.96 +		if ( strcmp(argv[i], "-hw") == 0 ) {
    1.97  			videoflags |= SDL_HWSURFACE;
    1.98  		} else
    1.99 -		if ( strcmp(argv[argc], "-warp") == 0 ) {
   1.100 +		if ( strcmp(argv[i], "-warp") == 0 ) {
   1.101  			videoflags |= SDL_HWPALETTE;
   1.102  		} else
   1.103 -		if ( strcmp(argv[argc], "-fullscreen") == 0 ) {
   1.104 +		if ( strcmp(argv[i], "-width") == 0 && argv[i+1] ) {
   1.105 +			w = atoi(argv[++i]);
   1.106 +		} else
   1.107 +		if ( strcmp(argv[i], "-height") == 0 && argv[i+1] ) {
   1.108 +			h = atoi(argv[++i]);
   1.109 +		} else
   1.110 +		if ( strcmp(argv[i], "-resize") == 0 ) {
   1.111 +			videoflags |= SDL_RESIZABLE;
   1.112 +		} else
   1.113 +		if ( strcmp(argv[i], "-noframe") == 0 ) {
   1.114 +			videoflags |= SDL_NOFRAME;
   1.115 +		} else
   1.116 +		if ( strcmp(argv[i], "-fullscreen") == 0 ) {
   1.117  			videoflags |= SDL_FULLSCREEN;
   1.118  		} else {
   1.119  			fprintf(stderr, 
   1.120 -			"Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
   1.121 +			"Usage: %s [-width N] [-height N] [-bpp N] [-warp] [-hw] [-fullscreen]\n",
   1.122  								argv[0]);
   1.123  			quit(1);
   1.124  		}
   1.125 @@ -340,7 +388,7 @@
   1.126  
   1.127  	/* Set 640x480 video mode */
   1.128  #ifndef _WIN32_WCE
   1.129 -	if ( (screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) {
   1.130 +	if ( (screen=SDL_SetVideoMode(w,h,video_bpp,videoflags)) == NULL ) {
   1.131  		fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
   1.132  						video_bpp, SDL_GetError());
   1.133  		quit(2);
   1.134 @@ -353,35 +401,7 @@
   1.135  		quit(2);
   1.136  	}
   1.137  #endif
   1.138 -	/* Set the surface pixels and refresh! */
   1.139 -	if ( SDL_LockSurface(screen) < 0 ) {
   1.140 -		fprintf(stderr, "Couldn't lock the display surface: %s\n",
   1.141 -							SDL_GetError());
   1.142 -		quit(2);
   1.143 -	}
   1.144 -	buffer=(Uint8 *)screen->pixels;
   1.145 -	if (screen->format->BytesPerPixel!=2) {
   1.146 -		for ( i=0; i<screen->h; ++i ) {
   1.147 -			memset(buffer,(i*255)/screen->h, screen->pitch);
   1.148 -			buffer += screen->pitch;
   1.149 -		}
   1.150 -	}
   1.151 -        else
   1.152 -        {
   1.153 -		for ( i=0; i<screen->h; ++i ) {
   1.154 -			gradient=((i*255)/screen->h);
   1.155 -                        color = SDL_MapRGB(screen->format, gradient, gradient, gradient);
   1.156 -                        buffer16=(Uint16*)buffer;
   1.157 -                        for (k=0; k<screen->w; k++)
   1.158 -                        {
   1.159 -                            *(buffer16+k)=color;
   1.160 -                        }
   1.161 -			buffer += screen->pitch;
   1.162 -		}
   1.163 -        }
   1.164 -
   1.165 -	SDL_UnlockSurface(screen);
   1.166 -	SDL_UpdateRect(screen, 0, 0, 0, 0);
   1.167 +	FillBackground(screen);
   1.168  
   1.169  	/* Create the light */
   1.170  	light = CreateLight(screen, 82);
   1.171 @@ -412,14 +432,7 @@
   1.172  	}
   1.173  
   1.174  	/* Run a sample blit to trigger blit acceleration */
   1.175 -	{ SDL_Rect dst;
   1.176 -		dst.x = 0;
   1.177 -		dst.y = 0;
   1.178 -		dst.w = sprite->w;
   1.179 -		dst.h = sprite->h;
   1.180 -		SDL_BlitSurface(sprite, NULL, screen, &dst);
   1.181 -		SDL_FillRect(screen, &dst, 0);
   1.182 -	}
   1.183 +	MoveSprite(screen, NULL);
   1.184  	if ( (sprite->flags & SDL_HWACCEL) == SDL_HWACCEL ) {
   1.185  		printf("Sprite blit uses hardware alpha acceleration\n");
   1.186  	} else {
   1.187 @@ -464,6 +477,12 @@
   1.188  		/* Check for events */
   1.189  		while ( SDL_PollEvent(&event) ) {
   1.190  			switch (event.type) {
   1.191 +				case SDL_VIDEORESIZE:
   1.192 +					screen = SDL_SetVideoMode(event.resize.w, event.resize.h, video_bpp, videoflags);
   1.193 +					if ( screen ) {
   1.194 +						FillBackground(screen);
   1.195 +					}
   1.196 +					break;
   1.197  				/* Attract sprite while mouse is held down */
   1.198  				case SDL_MOUSEMOTION:
   1.199  					if (event.motion.state != 0) {