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