testaccel.c

来自「linux 下svgalib编的一个界面程序示例」· C语言 代码 · 共 759 行 · 第 1/2 页

C
759
字号
    dy = 1;    clk = clock();    for (i = 0; i < 10000; i++) {	nx = x + dx;	ny = y + dy;	if ((nx + LOGO_WIDTH + 2 > xmax) || (nx < 0)) {	    dx = -dx;	    nx = x + dx;	}	if ((ny + LOGO_HEIGHT + 2 > ymax) || (ny < 0)) {	    dy = -dy;	    ny = y + dy;	}	if (modeinfo->bytesperpixel == 2) {	    vga_bitblt((x + y * (lw >> 1)) << 1,		       (nx + ny * (lw >> 1)) << 1,		       LOGO_WIDTH + 2, LOGO_HEIGHT + 2, lw);	} else {	    vga_bitblt(x + y * lw, nx + ny * lw, LOGO_WIDTH + 2, LOGO_HEIGHT + 2, lw);	}	pels += (LOGO_WIDTH + 2) * (LOGO_HEIGHT + 2);	x = nx;	y = ny;    }    clk = clock() - clk;    printf("  Has BitBlt: %12lu Pixels in %.2f seconds -> %.2f Megapels\n",	   pels, ((double) clk) / CLOCKS_PER_SEC, (pels / 1.0e6) / (((double) clk) / CLOCKS_PER_SEC));    my_wait();}void testimage(vga_modeinfo * modeinfo){    clock_t clk;    unsigned long pels = 0, *blitimage;    int xmax, ymax, lw, i, x, y;    if ((modeinfo->bytesperpixel < 1) || (modeinfo->bytesperpixel > 4)) {	puts("  Has ImageBlt, but no test code for this pixelwidth implemented.");	return;    }    ymax = vga_getydim();    xmax = vga_getxdim();    lw = modeinfo->linewidth;    if ((xmax < 210) || (ymax < 90)) {	puts("  Has ImageBlt, but no test code for this small resolution implemented.");	return;    }/* Prepare a simple test pattern for testuse: */    switch (modeinfo->bytesperpixel) {    default:	{	    /* 8bpp: */	    register unsigned char *ptr, *pt_fish = fish_monster;	    blitimage = alloca(fish_monster_w * fish_monster_h);	    ptr = (unsigned char *) blitimage;	    for (i = 0; i < (fish_monster_w * fish_monster_h); i++)		*ptr++ = fish_monster233[(*pt_fish++) - '`'];	}	break;    case 2:	{	    register unsigned char *pt_fish = fish_monster;	    register unsigned short *ptr, *coltab;	    blitimage = alloca(sizeof(unsigned short) * fish_monster_w * fish_monster_h);	    ptr = (unsigned short *) blitimage;	    if (modeinfo->colors == 32768)		coltab = fish_monster555;	    else		coltab = fish_monster565;	    for (i = 0; i < (fish_monster_w * fish_monster_h); i++)		*ptr++ = coltab[(*pt_fish++) - '`'];	}	break;    case 3:	{	    register unsigned char *pt_fish = fish_monster;	    register unsigned char *ptr, *coltab;	    blitimage = alloca(3 * fish_monster_w * fish_monster_h);	    ptr = (unsigned char *) blitimage;	    coltab = (unsigned char *) fish_monster888;	    for (i = 0; i < (fish_monster_w * fish_monster_h); i++) {		*ptr++ = coltab[(((*pt_fish) - '`') << 2)];		*ptr++ = coltab[(((*pt_fish) - '`') << 2) + 1];		*ptr++ = coltab[(((*pt_fish++) - '`') << 2) + 2];	    }	}	break;    case 4:	{	    register unsigned char *pt_fish = fish_monster;	    register unsigned int *ptr, *coltab;	    blitimage = alloca(sizeof(unsigned int) * fish_monster_w * fish_monster_h);	    ptr = (unsigned int *) blitimage;	    coltab = fish_monster888;	    if (modeinfo->flags & RGB_MISORDERED) {		for (i = 0; i < (fish_monster_w * fish_monster_h); i++)		    *ptr++ = (coltab[(*pt_fish++) - '`'] << 8);	    } else {		for (i = 0; i < (fish_monster_w * fish_monster_h); i++)		    *ptr++ = coltab[(*pt_fish++) - '`'];	    }	}	break;    }    setcol(modeinfo);    vga_clear();    srand(time(0));    for (i = 0; i < 10000; i++) {	x = rand() % (xmax - fish_monster_w);	y = rand() % (ymax - fish_monster_h);	if (modeinfo->bytesperpixel > 1)	    x *= modeinfo->bytesperpixel;	vga_imageblt(blitimage, x + y * lw, fish_monster_w, fish_monster_h, lw);	pels += fish_monster_w * fish_monster_h;    }    pels = 0;    blitimage = alloca(modeinfo->bytesperpixel * LOGO_WIDTH * LOGO_HEIGHT);    getlogo(blitimage, modeinfo);    clk = clock();    for (i = 0; i < 1000; i++) {	x = rand() % (xmax - LOGO_WIDTH);	y = rand() % (ymax - LOGO_HEIGHT);	if (modeinfo->bytesperpixel > 1)	    x *= modeinfo->bytesperpixel;	vga_imageblt(blitimage, x + y * lw, LOGO_WIDTH, LOGO_HEIGHT, lw);	pels += LOGO_WIDTH * LOGO_HEIGHT;    }    clk = clock() - clk;    printf("  Has ImageBlt: %10lu Pixels in %.2f seconds -> %.2f Megapels\n",	   pels, ((double) clk) / CLOCKS_PER_SEC, (pels / 1.0e6) / (((double) clk) / CLOCKS_PER_SEC));    my_wait();}void hlinesquare(hlinelst * des, register int n){    register int *xmin, *xmax;    if (n < 1)	n = 1;    des->n = n;    des->width = n;    des->offset = 0;    des->pels = n * n;    xmin = (int *) ((char *) des + sizeof(hlinelst));    xmax = xmin + n;    while (n--) {	*xmin++ = 0;	*xmax++ = des->width - 1;    }}void hlinedisk(hlinelst * des, register int n){    register int *xmin, *xmax, radsq, rad, x;    if (!(n & 1))	n--;    if (n < 1)	n = 1;    des->n = n;    des->width = n;    des->offset = 0;    des->pels = n * n;    rad = (n >> 1);    radsq = rad * rad;    xmin = (int *) ((char *) des + sizeof(hlinelst));    xmax = xmin + n;    while (n--) {	x = sqrt(radsq - isqr(n - rad));	*xmin++ = rad - x;	*xmax++ = rad + x;    }}void hlinecaro(hlinelst * des, register int n){    register int *xmin, *xmax, i, j;    if (!(n & 1))	n--;    if (n < 1)	n = 1;    des->n = n;    des->width = n;    des->offset = 0;    des->pels = (n * n) / 2;    xmin = (int *) ((char *) des + sizeof(hlinelst));    xmax = xmin + n;    i = 1 + (n >> 1);    j = 0;    while (i--) {	*xmin++ = (n >> 1) - j;	*xmax++ = (n >> 1) + j;	j++;    }    i = (n >> 1);    j -= 2;    while (i--) {	*xmin++ = (n >> 1) - j;	*xmax++ = (n >> 1) + j;	j--;    }}void testhline(vga_modeinfo * modeinfo){#define SHAPES 9    clock_t clk;    unsigned long pels = 0;    hlinelst *shape[SHAPES], *curs;    int xmax, ymax, lw, i, x, y, mask;    setcol(modeinfo);    vga_clear();    ymax = vga_getydim();    xmax = vga_getxdim();    lw = modeinfo->linewidth;    mask = modeinfo->colors - 1;    srand(time(0));    i = 0;    shape[i] = alloca(sizhlilst(ymax / 2));    hlinesquare(shape[i++], ymax / 2);    shape[i] = alloca(sizhlilst(ymax / 4));    hlinesquare(shape[i++], ymax / 4);    shape[i] = alloca(sizhlilst(ymax / 8));    hlinesquare(shape[i++], ymax / 8);    shape[i] = alloca(sizhlilst(ymax / 2));    hlinecaro(shape[i++], ymax / 2);    shape[i] = alloca(sizhlilst(ymax / 4));    hlinecaro(shape[i++], ymax / 4);    shape[i] = alloca(sizhlilst(ymax / 8));    hlinecaro(shape[i++], ymax / 8);    shape[i] = alloca(sizhlilst(ymax / 2));    hlinedisk(shape[i++], ymax / 2);    shape[i] = alloca(sizhlilst(ymax / 4));    hlinedisk(shape[i++], ymax / 4);    shape[i] = alloca(sizhlilst(ymax / 8));    hlinedisk(shape[i++], ymax / 8);    clk = clock();    for (i = 0; i < 1000; i++) {	curs = shape[rand() % SHAPES];	x = rand() % (xmax - (curs->width));	y = rand() % (ymax - (curs->n));	adj_hlilst(curs, x);	pels += curs->pels;	vga_hlinelistblt(y, curs->n,			 (int *) (((char *) curs) + sizeof(hlinelst)),			 (int *) (((char *) curs) + sizeof(hlinelst) + (curs->n) * sizeof(int)),			 lw, mask & rand());    }    clk = clock() - clk;    clk++;    printf("  Has HlineLst: %10lu Pixels in %.2f seconds -> %.2f Megapels\n",	   pels, ((double) clk) / CLOCKS_PER_SEC, (pels / 1.0e6) / (((double) clk) / CLOCKS_PER_SEC));    my_wait();}void testmode(int mode){    vga_modeinfo *modeinfo;    printf("Testing mode %2d: %s...", mode, vga_getmodename(mode));    if (!vga_hasmode(mode)) {	puts(" not available");	return;    }    puts("");    vga_setmode(mode);    modeinfo = vga_getmodeinfo(mode);    if ((modeinfo->colors == 256) && !(modeinfo->flags & IS_MODEX)) {	testwidth(mode, modeinfo);    } else	puts("  Dacwidth test not applicable.");    if (modeinfo->haveblit & HAVE_BITBLIT)	testbit(modeinfo);    if (modeinfo->haveblit & HAVE_FILLBLIT)	testfill(modeinfo);    if (modeinfo->haveblit & HAVE_IMAGEBLIT)	testimage(modeinfo);    if (modeinfo->haveblit & HAVE_HLINELISTBLIT)	testhline(modeinfo);    vga_setmode(TEXT);}void usage(void){    puts("Usage: testaccel [-nowait] {all|[mode [mode [mode...]]]}\n"	 "\ttests accelerated features and extended options in\n"	 "\tall or the given modes.\n"     "\tAll standard svgalib ways of writing modenames are understood.\n"      "\tIf no parameters are given the defaultmode or G640x480x256 is\n"	 "\tused.\n"    "\tIf -nowait is given, don't wait for a keypress after each test.");    exit(2);}int main(int argc, char *argv[]){    int mode;    vga_init();    puts(	    "Note: Timings include main cpu calculations (random numbers and such\n"	    "      things). Thus they are esp. for lower resolutions much too slow!");    if ((argc > 1) && !strcmp(argv[1], "-nowait")) {	argc--;	argv++;	waitmode = 0;    }    if ((argc == 2) && !strcmp(argv[1], "all")) {	int flag = 0;	for (mode = 1; mode <= GLASTMODE; mode++)	    if (vga_hasmode(mode)) {		flag = 1;		testmode(mode);	    }	if (!flag)	    puts("testaccel: Not any graphicsmode available!");    } else if (argc > 1) {	for (mode = 1; mode < argc; mode++)	    if (vga_getmodenumber(argv[mode]) < 0) {		printf("testaccel: Parameter %s is not a valid graphicsmode.\n", argv[mode]);		usage();	    }	for (mode = 1; mode < argc; mode++)	    testmode(vga_getmodenumber(argv[mode]));    } else {	mode = vga_getdefaultmode();	if (mode < 0)	    mode = G640x480x256;	if (vga_hasmode(mode))	    testmode(mode);	else	    puts("testaccel: could not set defaultmode or G640x480x256!");    }    return 0;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?