test/testvidinfo.c
changeset 480 92596bfe8446
parent 479 c0a1744bc2cf
child 482 6417071ba2e5
equal deleted inserted replaced
479:c0a1744bc2cf 480:92596bfe8446
     5 #include <stdio.h>
     5 #include <stdio.h>
     6 #include <stdlib.h>
     6 #include <stdlib.h>
     7 
     7 
     8 #include "SDL.h"
     8 #include "SDL.h"
     9 
     9 
       
    10 #define NUM_BLITS	10
       
    11 #define NUM_UPDATES	500
       
    12 
    10 #define FLAG_MASK	(SDL_HWSURFACE | SDL_FULLSCREEN | SDL_DOUBLEBUF)
    13 #define FLAG_MASK	(SDL_HWSURFACE | SDL_FULLSCREEN | SDL_DOUBLEBUF)
    11 
    14 
    12 void PrintFlags(Uint32 flags)
    15 void PrintFlags(Uint32 flags)
    13 {
    16 {
    14 	printf("0x%8.8x", (flags & FLAG_MASK));
    17 	printf("0x%8.8x", (flags & FLAG_MASK));
    28 int RunBlitTests(SDL_Surface *screen, SDL_Surface *bmp, int blitcount)
    31 int RunBlitTests(SDL_Surface *screen, SDL_Surface *bmp, int blitcount)
    29 {
    32 {
    30 	int i, j;
    33 	int i, j;
    31 	int maxx;
    34 	int maxx;
    32 	int maxy;
    35 	int maxy;
    33 	SDL_Rect *rects;
    36 	SDL_Rect dst;
    34 
    37 
    35 	rects = (SDL_Rect *)malloc(blitcount * sizeof(*rects));
    38 	maxx = (int)screen->w - bmp->w + 1;
    36 	if ( ! rects ) {
    39 	maxy = (int)screen->h - bmp->h + 1;
    37 		return 0;
    40 	for ( i = 0; i < NUM_UPDATES; ++i ) {
    38 	}
       
    39 	maxx = (int)screen->w - bmp->w;
       
    40 	maxy = (int)screen->h - bmp->h;
       
    41 	for ( i = 0; i < 100; ++i ) {
       
    42 		for ( j = 0; j < blitcount; ++j ) {
    41 		for ( j = 0; j < blitcount; ++j ) {
    43 			if ( maxx ) {
    42 			if ( maxx ) {
    44 				rects[j].x = rand() % maxx;
    43 				dst.x = rand() % maxx;
    45 			} else {
    44 			} else {
    46 				rects[j].x = 0;
    45 				dst.x = 0;
    47 			}
    46 			}
    48 			if ( maxy ) {
    47 			if ( maxy ) {
    49 				rects[j].y = rand() % maxy;
    48 				dst.y = rand() % maxy;
    50 			} else {
    49 			} else {
    51 				rects[j].y = 0;
    50 				dst.y = 0;
    52 			}
    51 			}
    53 			rects[j].w = bmp->w;
    52 			dst.w = bmp->w;
    54 			rects[j].h = bmp->h;
    53 			dst.h = bmp->h;
    55 			SDL_BlitSurface(bmp, NULL, screen, &rects[j]);
    54 			SDL_BlitSurface(bmp, NULL, screen, &dst);
    56 		}
    55 		}
    57 		if ( screen->flags & SDL_DOUBLEBUF ) {
    56 		SDL_Flip(screen);
    58 			SDL_Flip(screen);
    57 	}
    59 		} else {
       
    60 			SDL_UpdateRects(screen, blitcount, rects);
       
    61 		}
       
    62 	}
       
    63 	free(rects);
       
    64 
    58 
    65 	return i;
    59 	return i;
    66 }
    60 }
    67 
    61 
    68 void RunModeTests(SDL_Surface *screen)
    62 int RunModeTests(SDL_Surface *screen)
    69 {
    63 {
    70 	Uint32 then, now;
    64 	Uint32 then, now;
    71 	Uint32 frames;
    65 	Uint32 frames;
       
    66 	float seconds;
    72 	int i;
    67 	int i;
    73 	Uint8 r, g, b;
    68 	Uint8 r, g, b;
    74 	Uint32 pixel;
    69 	Uint32 pixel;
    75 	SDL_Surface *bmp, *tmp;
    70 	SDL_Surface *bmp, *tmp;
       
    71 	SDL_Event event;
       
    72 
       
    73 	while ( SDL_PollEvent(&event) ) {
       
    74 		if ( event.type == SDL_KEYDOWN )
       
    75 			return 0;
       
    76 	}
    76 
    77 
    77 	/* First test fills and screen update speed */
    78 	/* First test fills and screen update speed */
    78 	printf("Running color fill and fullscreen update test\n");
    79 	printf("Running color fill and fullscreen update test\n");
    79 	then = SDL_GetTicks();
    80 	then = SDL_GetTicks();
    80 	frames = 0;
    81 	frames = 0;
   101 		SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
   102 		SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
   102 		SDL_Flip(screen);
   103 		SDL_Flip(screen);
   103 		++frames;
   104 		++frames;
   104 	}
   105 	}
   105 	now = SDL_GetTicks();
   106 	now = SDL_GetTicks();
   106 	printf("%d fills and flips, %f FPS\n", frames, (float)(now - then) / frames);
   107 	seconds = (float)(now - then) / 1000.0f;
       
   108 	if ( seconds > 0.0f ) {
       
   109 		printf("%d fills and flips in %2.2f seconds, %2.2f FPS\n", frames, seconds, (float)frames / seconds);
       
   110 	} else {
       
   111 		printf("%d fills and flips in zero seconds!n", frames);
       
   112 	}
       
   113 
       
   114 	while ( SDL_PollEvent(&event) ) {
       
   115 		if ( event.type == SDL_KEYDOWN )
       
   116 			return 0;
       
   117 	}
   107 
   118 
   108 	bmp = SDL_LoadBMP("sample.bmp");
   119 	bmp = SDL_LoadBMP("sample.bmp");
   109 	if ( ! bmp ) {
   120 	if ( ! bmp ) {
   110 		printf("Couldn't load sample.bmp: %s\n", SDL_GetError());
   121 		printf("Couldn't load sample.bmp: %s\n", SDL_GetError());
   111 		return;
   122 		return;
   113 	printf("Running freshly loaded blit test: %dx%d at %d bpp, flags: ",
   124 	printf("Running freshly loaded blit test: %dx%d at %d bpp, flags: ",
   114 		bmp->w, bmp->h, bmp->format->BitsPerPixel);
   125 		bmp->w, bmp->h, bmp->format->BitsPerPixel);
   115 	PrintFlags(bmp->flags);
   126 	PrintFlags(bmp->flags);
   116 	printf("\n");
   127 	printf("\n");
   117 	then = SDL_GetTicks();
   128 	then = SDL_GetTicks();
   118 	frames = RunBlitTests(screen, bmp, 10);
   129 	frames = RunBlitTests(screen, bmp, NUM_BLITS);
   119 	now = SDL_GetTicks();
   130 	now = SDL_GetTicks();
   120 	if ( frames ) {
   131 	seconds = (float)(now - then) / 1000.0f;
   121 		printf("%d blits, %d updates, %f FPS\n", 10*frames, frames, (float)(now - then) / frames);
   132 	if ( seconds > 0.0f ) {
       
   133 		printf("%d blits / %d updates in %2.2f seconds, %2.2f FPS\n", NUM_BLITS*frames, frames, seconds, (float)frames / seconds);
       
   134 	} else {
       
   135 		printf("%d blits / %d updates in zero seconds!\n", NUM_BLITS*frames, frames);
   122 	}
   136 	}
   123 
   137 
   124 	tmp = bmp;
   138 	tmp = bmp;
   125 	bmp = SDL_DisplayFormat(bmp);
   139 	bmp = SDL_DisplayFormat(bmp);
   126 	SDL_FreeSurface(tmp);
   140 	SDL_FreeSurface(tmp);
   131 	printf("Running display format blit test: %dx%d at %d bpp, flags: ",
   145 	printf("Running display format blit test: %dx%d at %d bpp, flags: ",
   132 		bmp->w, bmp->h, bmp->format->BitsPerPixel);
   146 		bmp->w, bmp->h, bmp->format->BitsPerPixel);
   133 	PrintFlags(bmp->flags);
   147 	PrintFlags(bmp->flags);
   134 	printf("\n");
   148 	printf("\n");
   135 	then = SDL_GetTicks();
   149 	then = SDL_GetTicks();
   136 	frames = RunBlitTests(screen, bmp, 10);
   150 	frames = RunBlitTests(screen, bmp, NUM_BLITS);
   137 	now = SDL_GetTicks();
   151 	now = SDL_GetTicks();
   138 	if ( frames ) {
   152 	seconds = (float)(now - then) / 1000.0f;
   139 		printf("%d blits, %d updates, %f FPS\n", 10*frames, frames, (float)(now - then) / frames);
   153 	if ( seconds > 0.0f ) {
       
   154 		printf("%d blits / %d updates in %2.2f seconds, %2.2f FPS\n", NUM_BLITS*frames, frames, seconds, (float)frames / seconds);
       
   155 	} else {
       
   156 		printf("%d blits / %d updates in zero seconds!\n", NUM_BLITS*frames, frames);
   140 	}
   157 	}
   141 	SDL_FreeSurface(bmp);
   158 	SDL_FreeSurface(bmp);
       
   159 
       
   160 	while ( SDL_PollEvent(&event) ) {
       
   161 		if ( event.type == SDL_KEYDOWN )
       
   162 			return 0;
       
   163 	}
       
   164 	return 1;
   142 }
   165 }
   143 
   166 
   144 void RunVideoTests()
   167 void RunVideoTests()
   145 {
   168 {
   146 	static const struct {
   169 	static const struct {
   158 	};
   181 	};
   159 	int i, j;
   182 	int i, j;
   160 	SDL_Surface *screen;
   183 	SDL_Surface *screen;
   161 
   184 
   162 	/* Test out several different video mode combinations */
   185 	/* Test out several different video mode combinations */
       
   186 	SDL_WM_SetCaption("SDL Video Benchmark", "vidtest");
       
   187 	SDL_ShowCursor(0);
   163 	for ( i = 0; i < SDL_TABLESIZE(mode_list); ++i ) {
   188 	for ( i = 0; i < SDL_TABLESIZE(mode_list); ++i ) {
   164 		for ( j = 0; j < SDL_TABLESIZE(flags); ++j ) {
   189 		for ( j = 0; j < SDL_TABLESIZE(flags); ++j ) {
   165 			printf("===================================\n");
   190 			printf("===================================\n");
   166 			printf("Setting video mode: %dx%d at %d bpp, flags: ",
   191 			printf("Setting video mode: %dx%d at %d bpp, flags: ",
   167 			                          mode_list[i].w,
   192 			                          mode_list[i].w,
   181 				printf("Flags didn't match: ");
   206 				printf("Flags didn't match: ");
   182 				PrintFlags(screen->flags);
   207 				PrintFlags(screen->flags);
   183 				printf("\n");
   208 				printf("\n");
   184 				continue;
   209 				continue;
   185 			}
   210 			}
   186 			RunModeTests(screen);
   211 			if ( ! RunModeTests(screen) ) {
       
   212 				return;
       
   213 			}
   187 		}
   214 		}
   188 	}
   215 	}
   189 }
   216 }
   190 
   217 
   191 int main(int argc, char *argv[])
   218 int main(int argc, char *argv[])
   192 {
   219 {
   193 	const SDL_VideoInfo *info;
   220 	const SDL_VideoInfo *info;
   194 	int i;
   221 	int i;
   195 	SDL_Rect **modes;
   222 	SDL_Rect **modes;
       
   223 	char driver[128];
   196 
   224 
   197 	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
   225 	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
   198 		fprintf(stderr,
   226 		fprintf(stderr,
   199 			"Couldn't initialize SDL: %s\n", SDL_GetError());
   227 			"Couldn't initialize SDL: %s\n", SDL_GetError());
   200 		exit(1);
   228 		exit(1);
       
   229 	}
       
   230 	if ( SDL_VideoDriverName(driver, sizeof(driver)) ) {
       
   231 		printf("Video driver: %s\n", driver);
   201 	}
   232 	}
   202 	info = SDL_GetVideoInfo();
   233 	info = SDL_GetVideoInfo();
   203 	printf(
   234 	printf(
   204 "Current display: %d bits-per-pixel\n",info->vfmt->BitsPerPixel);
   235 "Current display: %d bits-per-pixel\n",info->vfmt->BitsPerPixel);
   205 	if ( info->vfmt->palette == NULL ) {
   236 	if ( info->vfmt->palette == NULL ) {