test/graywin.c
changeset 691 609543e2b3a1
parent 538 d3abe873e3f7
child 945 d33645c36072
equal deleted inserted replaced
690:b87d8d4c205d 691:609543e2b3a1
    18 void DrawBox(SDL_Surface *screen, int X, int Y, int width, int height)
    18 void DrawBox(SDL_Surface *screen, int X, int Y, int width, int height)
    19 {
    19 {
    20 	static unsigned int seeded = 0;
    20 	static unsigned int seeded = 0;
    21 	SDL_Rect area;
    21 	SDL_Rect area;
    22 	Uint32 color;
    22 	Uint32 color;
       
    23         Uint32 randc;
    23 
    24 
    24 	/* Seed the random number generator */
    25 	/* Seed the random number generator */
    25 	if ( seeded == 0 ) {
    26 	if ( seeded == 0 ) {
    26 		srand(time(NULL));
    27 		srand(time(NULL));
    27 		seeded = 1;
    28 		seeded = 1;
    30 	/* Get the bounds of the rectangle */
    31 	/* Get the bounds of the rectangle */
    31 	area.w = (rand()%width);
    32 	area.w = (rand()%width);
    32 	area.h = (rand()%height);
    33 	area.h = (rand()%height);
    33 	area.x = X-(area.w/2);
    34 	area.x = X-(area.w/2);
    34 	area.y = Y-(area.h/2);
    35 	area.y = Y-(area.h/2);
    35 	color = (rand()%NUM_COLORS);
    36         randc = (rand()%NUM_COLORS);
       
    37 
       
    38         if (screen->format->BytesPerPixel==1)
       
    39         {
       
    40             color = randc;
       
    41         }
       
    42         else
       
    43         {
       
    44             color = SDL_MapRGB(screen->format, randc, randc, randc);
       
    45         }
    36 
    46 
    37 	/* Do it! */
    47 	/* Do it! */
    38 	SDL_FillRect(screen, &area, color);
    48 	SDL_FillRect(screen, &area, color);
    39 	if ( screen->flags & SDL_DOUBLEBUF ) {
    49 	if ( screen->flags & SDL_DOUBLEBUF ) {
    40 		SDL_Flip(screen);
    50 		SDL_Flip(screen);
    41 	} else {
    51 	} else {
    42 		SDL_UpdateRects(screen, 1, &area);
    52 		SDL_UpdateRects(screen, 1, &area);
    43 	}
    53 	}
    44 }
    54 }
    45 
    55 
       
    56 void DrawBackground(SDL_Surface *screen)
       
    57 {
       
    58 	int i, j, k;
       
    59 	Uint8  *buffer;
       
    60 	Uint16 *buffer16;
       
    61         Uint16 color;
       
    62         Uint8  gradient;
       
    63 
       
    64 	/* Set the surface pixels and refresh! */
       
    65 	/* Use two loops in case the surface is double-buffered (both sides) */
       
    66 
       
    67 	for ( j=0; j<2; ++j ) {
       
    68 		if ( SDL_LockSurface(screen) < 0 ) {
       
    69 			fprintf(stderr, "Couldn't lock display surface: %s\n",
       
    70 								SDL_GetError());
       
    71 			return;
       
    72 		}
       
    73 		buffer = (Uint8 *)screen->pixels;
       
    74 
       
    75 		if (screen->format->BytesPerPixel!=2) {
       
    76 			for ( i=0; i<screen->h; ++i ) {
       
    77 				memset(buffer,(i*(NUM_COLORS-1))/screen->h, screen->w * screen->format->BytesPerPixel);
       
    78 				buffer += screen->pitch;
       
    79 			}
       
    80 		}
       
    81                 else
       
    82                 {
       
    83 			for ( i=0; i<screen->h; ++i ) {
       
    84 				gradient=((i*(NUM_COLORS-1))/screen->h);
       
    85                                 color = SDL_MapRGB(screen->format, gradient, gradient, gradient);
       
    86                                 buffer16=(Uint16*)buffer;
       
    87                                 for (k=0; k<screen->w; k++)
       
    88                                 {
       
    89                                    *(buffer16+k)=color;
       
    90                                 }
       
    91 				buffer += screen->pitch;
       
    92 			}
       
    93                 }
       
    94 
       
    95 		SDL_UnlockSurface(screen);
       
    96 		if ( screen->flags & SDL_DOUBLEBUF ) {
       
    97 			SDL_Flip(screen);
       
    98 		} else {
       
    99 			SDL_UpdateRect(screen, 0, 0, 0, 0);
       
   100                         break;
       
   101 		}
       
   102 	}
       
   103 }
       
   104 
    46 SDL_Surface *CreateScreen(Uint16 w, Uint16 h, Uint8 bpp, Uint32 flags)
   105 SDL_Surface *CreateScreen(Uint16 w, Uint16 h, Uint8 bpp, Uint32 flags)
    47 {
   106 {
    48 	SDL_Surface *screen;
   107 	SDL_Surface *screen;
    49 	int i;
   108 	int i;
    50 	SDL_Color palette[NUM_COLORS];
   109 	SDL_Color palette[NUM_COLORS];
    51 	Uint8 *buffer;
       
    52 
   110 
    53 	/* Set the video mode */
   111 	/* Set the video mode */
    54 	screen = SDL_SetVideoMode(w, h, bpp, flags);
   112 	screen = SDL_SetVideoMode(w, h, bpp, flags);
    55 	if ( screen == NULL ) {
   113 	if ( screen == NULL ) {
    56 		fprintf(stderr, "Couldn't set display mode: %s\n",
   114 		fprintf(stderr, "Couldn't set display mode: %s\n",
    58 		return(NULL);
   116 		return(NULL);
    59 	}
   117 	}
    60 	fprintf(stderr, "Screen is in %s mode\n",
   118 	fprintf(stderr, "Screen is in %s mode\n",
    61 		(screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
   119 		(screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
    62 
   120 
    63 	/* Set a gray colormap, reverse order from white to black */
   121 	if (bpp==8) {
    64 	for ( i=0; i<NUM_COLORS; ++i ) {
   122 		/* Set a gray colormap, reverse order from white to black */
    65 		palette[i].r = (NUM_COLORS-1)-i * (256 / NUM_COLORS);
   123 		for ( i=0; i<NUM_COLORS; ++i ) {
    66 		palette[i].g = (NUM_COLORS-1)-i * (256 / NUM_COLORS);
   124 			palette[i].r = (NUM_COLORS-1)-i * (256 / NUM_COLORS);
    67 		palette[i].b = (NUM_COLORS-1)-i * (256 / NUM_COLORS);
   125 			palette[i].g = (NUM_COLORS-1)-i * (256 / NUM_COLORS);
    68 	}
   126 			palette[i].b = (NUM_COLORS-1)-i * (256 / NUM_COLORS);
    69 	SDL_SetColors(screen, palette, 0, NUM_COLORS);
   127 		}
    70 
   128 		SDL_SetColors(screen, palette, 0, NUM_COLORS);
    71 	/* Set the surface pixels and refresh! */
       
    72 	/* Use two loops in case the surface is double-buffered (both sides) */
       
    73 	for ( i=0; i<2; ++i ) {
       
    74 		if ( SDL_LockSurface(screen) < 0 ) {
       
    75 			fprintf(stderr, "Couldn't lock display surface: %s\n",
       
    76 								SDL_GetError());
       
    77 			return(NULL);
       
    78 		}
       
    79 		buffer = (Uint8 *)screen->pixels;
       
    80 		for ( i=0; i<screen->h; ++i ) {
       
    81 			memset(buffer,(i*(NUM_COLORS-1))/screen->h, screen->w * screen->format->BytesPerPixel);
       
    82 			buffer += screen->pitch;
       
    83 		}
       
    84 		SDL_UnlockSurface(screen);
       
    85 		if ( screen->flags & SDL_DOUBLEBUF ) {
       
    86 			SDL_Flip(screen);
       
    87 		} else {
       
    88 			SDL_UpdateRect(screen, 0, 0, 0, 0);
       
    89 		}
       
    90 	}
   129 	}
    91 
   130 
    92 	return(screen);
   131 	return(screen);
    93 }
   132 }
    94 
   133 
   149 	/* Set a video mode */
   188 	/* Set a video mode */
   150 	screen = CreateScreen(width, height, bpp, videoflags);
   189 	screen = CreateScreen(width, height, bpp, videoflags);
   151 	if ( screen == NULL ) {
   190 	if ( screen == NULL ) {
   152 		exit(2);
   191 		exit(2);
   153 	}
   192 	}
       
   193         
       
   194         DrawBackground(screen);
   154 		
   195 		
   155 	/* Wait for a keystroke */
   196 	/* Wait for a keystroke */
   156 	done = 0;
   197 	done = 0;
   157 	while ( !done && SDL_WaitEvent(&event) ) {
   198 	while ( !done && SDL_WaitEvent(&event) ) {
   158 		switch (event.type) {
   199 		switch (event.type) {
   180 					if ( screen == NULL ) {
   221 					if ( screen == NULL ) {
   181 						fprintf(stderr,
   222 						fprintf(stderr,
   182 					"Couldn't toggle fullscreen mode\n");
   223 					"Couldn't toggle fullscreen mode\n");
   183 						done = 1;
   224 						done = 1;
   184 					}
   225 					}
       
   226                                         DrawBackground(screen);
   185 					break;
   227 					break;
   186 				}
   228 				}
   187 				/* Any other key quits the application... */
   229 				/* Any other key quits the application... */
   188 			case SDL_QUIT:
   230 			case SDL_QUIT:
   189 				done = 1;
   231 				done = 1;
   190 				break;
   232 				break;
       
   233 			case SDL_VIDEOEXPOSE:
       
   234 				DrawBackground(screen);
       
   235 				break;
   191 			default:
   236 			default:
   192 				break;
   237 				break;
   193 		}
   238 		}
   194 	}
   239 	}
   195 	SDL_Quit();
   240 	SDL_Quit();