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