test/testvidinfo.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 08 Oct 2002 16:29:01 +0000
changeset 524 1b8ea19e9ee4
parent 483 32190cba6c40
child 821 30168104389f
permissions -rw-r--r--
*** empty log message ***
     1 
     2 /* Simple program -- figure out what kind of video display we have */
     3 
     4 #include <stdlib.h>
     5 #include <stdio.h>
     6 #include <stdlib.h>
     7 #include <string.h>
     8 
     9 #include "SDL.h"
    10 
    11 #define NUM_BLITS	10
    12 #define NUM_UPDATES	500
    13 
    14 #define FLAG_MASK	(SDL_HWSURFACE | SDL_FULLSCREEN | SDL_DOUBLEBUF)
    15 
    16 void PrintFlags(Uint32 flags)
    17 {
    18 	printf("0x%8.8x", (flags & FLAG_MASK));
    19 	if ( flags & SDL_HWSURFACE ) {
    20 		printf(" SDL_HWSURFACE");
    21 	} else {
    22 		printf(" SDL_SWSURFACE");
    23 	}
    24 	if ( flags & SDL_FULLSCREEN ) {
    25 		printf(" | SDL_FULLSCREEN");
    26 	}
    27 	if ( flags & SDL_DOUBLEBUF ) {
    28 		printf(" | SDL_DOUBLEBUF");
    29 	}
    30 }
    31 
    32 int RunBlitTests(SDL_Surface *screen, SDL_Surface *bmp, int blitcount)
    33 {
    34 	int i, j;
    35 	int maxx;
    36 	int maxy;
    37 	SDL_Rect dst;
    38 
    39 	maxx = (int)screen->w - bmp->w + 1;
    40 	maxy = (int)screen->h - bmp->h + 1;
    41 	for ( i = 0; i < NUM_UPDATES; ++i ) {
    42 		for ( j = 0; j < blitcount; ++j ) {
    43 			if ( maxx ) {
    44 				dst.x = rand() % maxx;
    45 			} else {
    46 				dst.x = 0;
    47 			}
    48 			if ( maxy ) {
    49 				dst.y = rand() % maxy;
    50 			} else {
    51 				dst.y = 0;
    52 			}
    53 			dst.w = bmp->w;
    54 			dst.h = bmp->h;
    55 			SDL_BlitSurface(bmp, NULL, screen, &dst);
    56 		}
    57 		SDL_Flip(screen);
    58 	}
    59 
    60 	return i;
    61 }
    62 
    63 int RunModeTests(SDL_Surface *screen)
    64 {
    65 	Uint32 then, now;
    66 	Uint32 frames;
    67 	float seconds;
    68 	int i;
    69 	Uint8 r, g, b;
    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 	}
    77 
    78 	/* First test fills and screen update speed */
    79 	printf("Running color fill and fullscreen update test\n");
    80 	then = SDL_GetTicks();
    81 	frames = 0;
    82 	for ( i = 0; i < 256; ++i ) {
    83 		r = i;
    84 		g = 0;
    85 		b = 0;
    86 		SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
    87 		SDL_Flip(screen);
    88 		++frames;
    89 	}
    90 	for ( i = 0; i < 256; ++i ) {
    91 		r = 0;
    92 		g = i;
    93 		b = 0;
    94 		SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
    95 		SDL_Flip(screen);
    96 		++frames;
    97 	}
    98 	for ( i = 0; i < 256; ++i ) {
    99 		r = 0;
   100 		g = 0;
   101 		b = i;
   102 		SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
   103 		SDL_Flip(screen);
   104 		++frames;
   105 	}
   106 	now = SDL_GetTicks();
   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 	}
   118 
   119 	bmp = SDL_LoadBMP("sample.bmp");
   120 	if ( ! bmp ) {
   121 		printf("Couldn't load sample.bmp: %s\n", SDL_GetError());
   122 		return 0;
   123 	}
   124 	printf("Running freshly loaded blit test: %dx%d at %d bpp, flags: ",
   125 		bmp->w, bmp->h, bmp->format->BitsPerPixel);
   126 	PrintFlags(bmp->flags);
   127 	printf("\n");
   128 	then = SDL_GetTicks();
   129 	frames = RunBlitTests(screen, bmp, NUM_BLITS);
   130 	now = SDL_GetTicks();
   131 	seconds = (float)(now - then) / 1000.0f;
   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);
   136 	}
   137 
   138 	tmp = bmp;
   139 	bmp = SDL_DisplayFormat(bmp);
   140 	SDL_FreeSurface(tmp);
   141 	if ( ! bmp ) {
   142 		printf("Couldn't convert sample.bmp: %s\n", SDL_GetError());
   143 		return 0;
   144 	}
   145 	printf("Running display format blit test: %dx%d at %d bpp, flags: ",
   146 		bmp->w, bmp->h, bmp->format->BitsPerPixel);
   147 	PrintFlags(bmp->flags);
   148 	printf("\n");
   149 	then = SDL_GetTicks();
   150 	frames = RunBlitTests(screen, bmp, NUM_BLITS);
   151 	now = SDL_GetTicks();
   152 	seconds = (float)(now - then) / 1000.0f;
   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);
   157 	}
   158 	SDL_FreeSurface(bmp);
   159 
   160 	while ( SDL_PollEvent(&event) ) {
   161 		if ( event.type == SDL_KEYDOWN )
   162 			return 0;
   163 	}
   164 	return 1;
   165 }
   166 
   167 void RunVideoTests()
   168 {
   169 	static const struct {
   170 		int w, h, bpp;
   171 	} mode_list[] = {
   172 		{ 640, 480, 8 }, { 640, 480, 16 }, { 640, 480, 32 },
   173 		{ 800, 600, 8 }, { 800, 600, 16 }, { 800, 600, 32 },
   174 		{ 1024, 768, 8 }, { 1024, 768, 16 }, { 1024, 768, 32 }
   175 	};
   176 	static const Uint32 flags[] = {
   177 		(SDL_SWSURFACE),
   178 		(SDL_SWSURFACE | SDL_FULLSCREEN),
   179 		(SDL_HWSURFACE | SDL_FULLSCREEN),
   180 		(SDL_HWSURFACE | SDL_FULLSCREEN | SDL_DOUBLEBUF)
   181 	};
   182 	int i, j;
   183 	SDL_Surface *screen;
   184 
   185 	/* Test out several different video mode combinations */
   186 	SDL_WM_SetCaption("SDL Video Benchmark", "vidtest");
   187 	SDL_ShowCursor(0);
   188 	for ( i = 0; i < SDL_TABLESIZE(mode_list); ++i ) {
   189 		for ( j = 0; j < SDL_TABLESIZE(flags); ++j ) {
   190 			printf("===================================\n");
   191 			printf("Setting video mode: %dx%d at %d bpp, flags: ",
   192 			                          mode_list[i].w,
   193 			                          mode_list[i].h,
   194 			                          mode_list[i].bpp);
   195 			PrintFlags(flags[j]);
   196 			printf("\n");
   197 			screen = SDL_SetVideoMode(mode_list[i].w,
   198 			                          mode_list[i].h,
   199 			                          mode_list[i].bpp,
   200 			                          flags[j]);
   201 			if ( ! screen ) {
   202 				printf("Setting video mode failed: %s\n", SDL_GetError());
   203 				continue;
   204 			}
   205 			if ( (screen->flags & FLAG_MASK) != flags[j] ) {
   206 				printf("Flags didn't match: ");
   207 				PrintFlags(screen->flags);
   208 				printf("\n");
   209 				continue;
   210 			}
   211 			if ( ! RunModeTests(screen) ) {
   212 				return;
   213 			}
   214 		}
   215 	}
   216 }
   217 
   218 int main(int argc, char *argv[])
   219 {
   220 	const SDL_VideoInfo *info;
   221 	int i;
   222 	SDL_Rect **modes;
   223 	char driver[128];
   224 
   225 	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
   226 		fprintf(stderr,
   227 			"Couldn't initialize SDL: %s\n", SDL_GetError());
   228 		exit(1);
   229 	}
   230 	if ( SDL_VideoDriverName(driver, sizeof(driver)) ) {
   231 		printf("Video driver: %s\n", driver);
   232 	}
   233 	info = SDL_GetVideoInfo();
   234 	printf(
   235 "Current display: %d bits-per-pixel\n",info->vfmt->BitsPerPixel);
   236 	if ( info->vfmt->palette == NULL ) {
   237 		printf("	Red Mask = 0x%.8x\n", info->vfmt->Rmask);
   238 		printf("	Green Mask = 0x%.8x\n", info->vfmt->Gmask);
   239 		printf("	Blue Mask = 0x%.8x\n", info->vfmt->Bmask);
   240 	}
   241 	/* Print available fullscreen video modes */
   242 	modes = SDL_ListModes(NULL, SDL_FULLSCREEN);
   243 	if ( modes == (SDL_Rect **)0 ) {
   244 		printf("No available fullscreen video modes\n");
   245 	} else
   246 	if ( modes == (SDL_Rect **)-1 ) {
   247 		printf("No special fullscreen video modes\n");
   248 	} else {
   249 		printf("Fullscreen video modes:\n");
   250 		for ( i=0; modes[i]; ++i ) {
   251 			printf("\t%dx%dx%d\n", modes[i]->w, modes[i]->h, info->vfmt->BitsPerPixel);
   252 		}
   253 	}
   254 	if ( info->wm_available ) {
   255 		printf("A window manager is available\n");
   256 	}
   257 	if ( info->hw_available ) {
   258 		printf("Hardware surfaces are available (%dK video memory)\n",
   259 			info->video_mem);
   260 	}
   261 	if ( info->blit_hw ) {
   262 		printf(
   263 "Copy blits between hardware surfaces are accelerated\n");
   264 	}
   265 	if ( info->blit_hw_CC ) {
   266 		printf(
   267 "Colorkey blits between hardware surfaces are accelerated\n");
   268 	}
   269 	if ( info->blit_hw_A ) {
   270 		printf(
   271 "Alpha blits between hardware surfaces are accelerated\n");
   272 	}
   273 	if ( info->blit_sw ) {
   274 		printf(
   275 "Copy blits from software surfaces to hardware surfaces are accelerated\n");
   276 	}
   277 	if ( info->blit_sw_CC ) {
   278 		printf(
   279 "Colorkey blits from software surfaces to hardware surfaces are accelerated\n");
   280 	}
   281 	if ( info->blit_sw_A ) {
   282 		printf(
   283 "Alpha blits from software surfaces to hardware surfaces are accelerated\n");
   284 	}
   285 	if ( info->blit_fill ) {
   286 		printf(
   287 "Color fills on hardware surfaces are accelerated\n");
   288 	}
   289 
   290 	if ( argv[1] && (strcmp(argv[1], "-benchmark") == 0) ) {
   291 		RunVideoTests();
   292 	}
   293 
   294 	SDL_Quit();
   295 	return(0);
   296 }