File: xbench.C

package info (click to toggle)
flying 6.20-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 752 kB
  • ctags: 1,873
  • sloc: cpp: 10,966; makefile: 189
file content (459 lines) | stat: -rw-r--r-- 13,348 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459

#include <X11/Xlib.h>
#include <X11/Xos.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

class XMoveChecker {
	public:
		XMoveChecker( unsigned bsize, unsigned avgmove );
		virtual ~XMoveChecker();
		virtual	void	RollBall() = 0;
		double loop( unsigned long secs );

	protected:
		Display	*dpy;
		int		scr;
		Window	win;

		unsigned bsize;			// Ball-Size
		unsigned avgmove;			// durchschnittssprung
		unsigned dhelp;
};

// ==========================================================================

XMoveChecker::XMoveChecker( unsigned bsize_in, unsigned avg )
: bsize(bsize_in), avgmove(avg)
{
XSetWindowAttributes setwinattr;

	dpy   = XOpenDisplay( "" );
	scr   = DefaultScreen( dpy );

	if (avgmove>bsize-1)		avgmove=bsize-1;
	dhelp = 2*bsize-1;

	win   = XCreateSimpleWindow( dpy, RootWindow( dpy, scr ), 0, 0, dhelp, dhelp,
					0, BlackPixel( dpy,scr ), WhitePixel( dpy,scr ) );
	setwinattr.override_redirect = True;
	XChangeWindowAttributes( dpy, win, CWOverrideRedirect, &setwinattr );

	// map window and wait for it to be mapped.
XEvent	event;
	XSelectInput( dpy, win, ExposureMask );
	XMapRaised( dpy, win );
	XNextEvent( dpy, &event );
}

XMoveChecker::~XMoveChecker() {
	XDestroyWindow( dpy, win );
	XCloseDisplay( dpy );
}

double XMoveChecker::loop( unsigned long secs ) {
struct timeval start;
struct timeval current;
struct timeval	diff;
long	count=-1;	// Vorlauf

	if (secs<=5) {
		XGrabServer(dpy);
		XSync(dpy,0);
	}

	do {
		for (int i=0;i<16;i++)		RollBall();
		XSync( dpy, 0 );
		if (count++<0)	gettimeofday( &start, NULL );
		gettimeofday( &current, NULL );
		diff.tv_sec = current.tv_sec - start.tv_sec;
		if ( current.tv_usec >= start.tv_usec ) {
			diff.tv_usec = current.tv_usec - start.tv_usec;
		}
		else {
			diff.tv_usec = 1000000L + current.tv_usec - start.tv_usec;
			diff.tv_sec--;
		}
	} while( diff.tv_sec < secs );

	if (secs<=5) {
		XUngrabServer(dpy);
	}

	return ( (double)count / ((double)diff.tv_sec + diff.tv_usec/1000000.0)*(double)secs );
}

// ==========================================================================


class XMoveChecker1 : public XMoveChecker {
	public:
		XMoveChecker1( unsigned bsize, unsigned avg );
		virtual ~XMoveChecker1();
		virtual void RollBall();

	protected:
		Pixmap	help;				// Doublebuffer:		Depth 8  (2*bsize-1)
		Pixmap	bpix;				// Ball-Bitmap:		Depth 1     bsize
		Pixmap	spix;				// Shadow-Bitmap:		Depth 1     bsize
		Pixmap	rpix;				// Ring-Bitmap:		Depth 1     bsize
		GC			gc_bclear;		// reset help-pixmap
		GC			gc_ballwhite;	// create white ball
		GC			gc_lay2;			// add shades to ball
		GC			gc_ball;			// add colored rings
		GC			gc_bxor;			// copy help pixmap to screen
		GC			gc_bit;			// initialize bitmaps
};

XMoveChecker1::XMoveChecker1( unsigned bsize_in, unsigned avg )
: XMoveChecker( bsize_in, avg )
{
	help  = XCreatePixmap(dpy,win,dhelp,dhelp,DefaultDepth(dpy,scr));

	bpix  = XCreatePixmap(dpy,win,bsize,bsize,1);
	spix  = XCreatePixmap(dpy,win,bsize,bsize,1);
	rpix  = XCreatePixmap(dpy,win,bsize,bsize,1);

	gc_bit = XCreateGC( dpy, bpix, 0, 0 );
	XFillRectangle( dpy, bpix, gc_bit, 0, 0, bsize, bsize );
	XFillRectangle( dpy, spix, gc_bit, 0, 0, bsize, bsize );
	XFillRectangle( dpy, rpix, gc_bit, 0, 0, bsize, bsize );

//
// ball_mask:  0x03
// shade_mask: 0x04
//
unsigned long	value_mask;
XGCValues		values;

	value_mask =	GCPlaneMask | GCForeground | GCBackground | GCFunction
					|	GCGraphicsExposures;

	values.graphics_exposures = False;
	values.fill_style	= FillSolid;
	values.background = 0;

	values.foreground = 0x00;
	values.plane_mask = 0x07;		// Arbeiten in lay2
	values.function   = GXclear;
	gc_bclear    = XCreateGC( dpy, win, value_mask, &values );
	values.function   = GXxor;
	gc_bxor      = XCreateGC( dpy, win, value_mask, &values );

	values.function	= GXxor;
	values.foreground = 0x04;
	values.plane_mask = 0x04;
	gc_lay2      = XCreateGC( dpy, win, value_mask, &values );

	values.function	= GXxor;
	values.foreground = 0x01;
	values.plane_mask = 0x03;
	gc_ballwhite      = XCreateGC( dpy, win, value_mask, &values );
	values.foreground = 0x03;
	gc_ball           = XCreateGC( dpy, win, value_mask, &values );
}

XMoveChecker1::~XMoveChecker1() {
	XFreeGC( dpy, gc_ball );
	XFreeGC( dpy, gc_ballwhite );
	XFreeGC( dpy, gc_lay2 );
	XFreeGC( dpy, gc_bxor );
	XFreeGC( dpy, gc_bclear );
	XFreePixmap( dpy, help );
	XFreePixmap( dpy, bpix );
	XFreePixmap( dpy, spix );
	XFreePixmap( dpy, rpix );
}

// XMoveChecker1: simulates the original algorithm that consists of
//	- clearing the double buffer pixmap of depth 8
//	- copying old ball, constisting of 3 layers of depth 1
//	- copying new ball, consisting of 3 layers of depth 1
//	- copying the double buffer to the screen
// - 8 X-calls

void XMoveChecker1::RollBall() {

int	d  = bsize;
int	dx = avgmove;
int	dy = avgmove;

int absx = (dx>0)?dx:-dx;
int absy = (dy>0)?dy:-dy;

	int	width = d+absx;		// tats�chlicher Ausschnittgr��e der Pixmap
	int	height= d+absy;
	int	ox = (dx>0)?0:absx;	// relativer Abstand alte Position
	int	oy = (dy>0)?0:absy;
	int	nx = (dx>0)?absx:0;	// relativer Abstand neue Position
	int	ny = (dy>0)?absy:0;

	XFillRectangle(dpy,help,gc_bclear,0,0,width,height);

	XCopyPlane(dpy,bpix,help,gc_ballwhite,0,0,d,d,ox,oy,1);
	XCopyPlane(dpy,spix,help,gc_lay2,0,0,d,d,ox,oy,1);
	XCopyPlane(dpy,rpix,help,gc_ball,0,0,d,d,ox,oy,1);

	XCopyPlane(dpy,bpix,help,gc_ballwhite,0,0,d,d,nx,ny,1);
	XCopyPlane(dpy,spix,help,gc_lay2,0,0,d,d,nx,ny,1);
	XCopyPlane(dpy,rpix,help,gc_ball,0,0,d,d,nx,ny,1);

	XCopyArea(dpy,help,win,gc_bxor,0,0,width,height,0,0 );
}

// ==========================================================================

class XMoveChecker2 : public XMoveChecker {
	public:
		XMoveChecker2( unsigned bsize, unsigned avg );
		virtual ~XMoveChecker2();
		virtual void RollBall();

	protected:
		Pixmap	help;				// Doublebuffer:			Depth 8  (2*bsize-1)
		Pixmap	bpix;				// Ball/Shade-Bitmap:	Depth 8     bsize
		Pixmap	rpix;				// Ring-Bitmap:			Depth 1     bsize
		GC			gc_bcopy;		// preset help-pixmap
		GC			gc_ball;			// add colored rings
		GC			gc_bxor;			// copy help pixmap to screen
		GC			gc_bit;			// initialize bitmaps
};

XMoveChecker2::XMoveChecker2( unsigned bsize_in, unsigned avg )
: XMoveChecker( bsize_in, avg )
{
	help  = XCreatePixmap(dpy,win,dhelp,dhelp,DefaultDepth(dpy,scr));
	bpix  = XCreatePixmap(dpy,win,dhelp,dhelp,DefaultDepth(dpy,scr));
	XFillRectangle( dpy, bpix, DefaultGC(dpy,scr), 0, 0, dhelp, dhelp );

	rpix  = XCreatePixmap(dpy,win,bsize,bsize,1);

	gc_bit = XCreateGC( dpy, rpix, 0, 0 );
	XFillRectangle( dpy, rpix, gc_bit, 0, 0, bsize, bsize );

//
// ball_mask:  0x03
// shade_mask: 0x04
//
unsigned long	value_mask;
XGCValues		values;

	value_mask =	GCPlaneMask | GCForeground | GCBackground | GCFunction
					|	GCGraphicsExposures;

	values.graphics_exposures = False;
	values.fill_style	= FillSolid;
	values.background = 0;

	values.foreground = 0x00;
	values.plane_mask = 0x07;
	values.function   = GXcopy;
	gc_bcopy     = XCreateGC( dpy, win, value_mask, &values );
	values.function   = GXxor;
	gc_bxor      = XCreateGC( dpy, win, value_mask, &values );

	values.function	= GXxor;
	values.plane_mask = 0x03;
	values.foreground = 0x03;
	gc_ball           = XCreateGC( dpy, win, value_mask, &values );
}

XMoveChecker2::~XMoveChecker2() {
	XFreeGC( dpy, gc_ball );
	XFreeGC( dpy, gc_bxor );
	XFreeGC( dpy, gc_bcopy );
	XFreePixmap( dpy, help );
	XFreePixmap( dpy, bpix );
	XFreePixmap( dpy, rpix );
}


// XMoveChecker2: simulates the enhanced algorithm that consists of
//	- initialisation the double buffer with an oversized 8 bit shadowed image of the old ball
//   (a lot more memory is need in this approach than in the next similare one)
//	- copying the 8 bit shadow image of the new ball
//	- copying 2 bitmaps with the rings of the ball with 1 bit each
//	- copying the double buffer to the screen
//	- 5 X-calls

void XMoveChecker2::RollBall() {

int	d  = bsize;
int	dx = avgmove;
int	dy = avgmove;

int absx = (dx>0)?dx:-dx;
int absy = (dy>0)?dy:-dy;

	int	width = d+absx;		// tats�chlicher Ausschnittgr��e der Pixmap
	int	height= d+absy;
	int	ox = (dx>0)?0:absx;	// relativer Abstand alte Position
	int	oy = (dy>0)?0:absy;
	int	nx = (dx>0)?absx:0;	// relativer Abstand neue Position
	int	ny = (dy>0)?absy:0;

	XCopyArea(dpy,bpix,help,gc_bcopy,0,0,width,height,0,0);
	XCopyArea(dpy,bpix,help,gc_bxor,0,0,d,d,nx,ny);

	XCopyPlane(dpy,rpix,help,gc_ball,0,0,d,d,ox,oy,1);
	XCopyPlane(dpy,rpix,help,gc_ball,0,0,d,d,nx,ny,1);

	XCopyArea(dpy,help,win,gc_bxor,0,0,width,height,0,0 );
}

// ==========================================================================

class XMoveChecker3 : public XMoveChecker2 {
	public:
		XMoveChecker3( unsigned bsize, unsigned avg );
		virtual ~XMoveChecker3();
		virtual void RollBall();
};

XMoveChecker3::XMoveChecker3( unsigned bsize_in, unsigned avg )
: XMoveChecker2( bsize_in, avg ) {
}

XMoveChecker3::~XMoveChecker3() {
}

// XMoveChecker3: simulates the enhanced algorithm that consists of
//	- clearing the double buffer pixmap of depth 8
//	- copying the 8 bit shadow image of the balls with 8 bits each
//	- copying 2 bitmaps with the rings of the ball with 1 bit each
//	- copying the double buffer to the screen
//	- 6 X-calls

void XMoveChecker3::RollBall() {

int	d  = bsize;
int	dx = avgmove;
int	dy = avgmove;

int absx = (dx>0)?dx:-dx;
int absy = (dy>0)?dy:-dy;

	int	width = d+absx;		// tats�chlicher Ausschnittgr��e der Pixmap
	int	height= d+absy;
	int	ox = (dx>0)?0:absx;	// relativer Abstand alte Position
	int	oy = (dy>0)?0:absy;
	int	nx = (dx>0)?absx:0;	// relativer Abstand neue Position
	int	ny = (dy>0)?absy:0;

	XFillRectangle(dpy,help,gc_bcopy,0,0,width,height);

	XCopyArea(dpy,bpix,help,gc_bxor,0,0,d,d,ox,oy);
	XCopyArea(dpy,bpix,help,gc_bxor,0,0,d,d,nx,ny);

	XCopyPlane(dpy,rpix,help,gc_ball,0,0,d,d,ox,oy,1);
	XCopyPlane(dpy,rpix,help,gc_ball,0,0,d,d,nx,ny,1);

	XCopyArea(dpy,help,win,gc_bxor,0,0,width,height,0,0 );
}

// ==========================================================================

class XMoveChecker4 : public XMoveChecker2 {
	public:
		XMoveChecker4( unsigned bsize, unsigned avg );
		virtual ~XMoveChecker4();
		virtual void RollBall();
};

XMoveChecker4::XMoveChecker4( unsigned bsize_in, unsigned avg )
: XMoveChecker2( bsize_in, avg ) {
}

XMoveChecker4::~XMoveChecker4() {
}

// XMoveChecker4: simulates the enhanced algorithm that consists of
//	- initialisation of double-buffer parts, not covered from old ball
//	- initialisaion of the double-buffer part containing old ball (8 bit)
//	- copying the 8 bit shadow image of the new ball (8 bit)
//	- copying 2 bitmaps with the rings of the ball with 1 bit each
//	- copying the double buffer to the screen
//	- 6 X-calls

void XMoveChecker4::RollBall() {

int	d  = bsize;
int	dx = avgmove;
int	dy = avgmove;

int absx = (dx>0)?dx:-dx;
int absy = (dy>0)?dy:-dy;

	int	width = d+absx;		// tats�chlicher Ausschnittgr��e der Pixmap
	int	height= d+absy;
	int	ox = (dx>0)?0:absx;	// relativer Abstand alte Position
	int	oy = (dy>0)?0:absy;
	int	nx = (dx>0)?absx:0;	// relativer Abstand neue Position
	int	ny = (dy>0)?absy:0;

	XFillRectangle(dpy,help,gc_bcopy,0,d,width-d,height-d);
	XFillRectangle(dpy,help,gc_bcopy,d,0,width-d,height);

	XCopyArea(dpy,bpix,help,gc_bcopy,0,0,d,d,ox,oy);
	XCopyArea(dpy,bpix,help,gc_bxor,0,0,d,d,nx,ny);

	XCopyPlane(dpy,rpix,help,gc_ball,0,0,d,d,ox,oy,1);
	XCopyPlane(dpy,rpix,help,gc_ball,0,0,d,d,nx,ny,1);

	XCopyArea(dpy,help,win,gc_bxor,0,0,width,height,0,0 );
}

// ==========================================================================


void main_xbench(int argc, char **argv)
{
XMoveChecker	*xm=0;
unsigned	bsize   = 24;
unsigned avgmove = 8;
unsigned count = 3;
unsigned secs  = 3;

	for (int i=1;i<argc;i++) {
		if (!strncmp(argv[i],"-n",2))				count = atoi(argv[i]+2);
		else if (!strncmp(argv[i],"-s",2))		secs  = atoi(argv[i]+2);
		else if (!strncmp(argv[i],"-b",2))		bsize = atoi(argv[i]+2);
		else if (!strncmp(argv[i],"-m",2))		avgmove = atoi(argv[i]+2);
		else {
			printf( "usage: bench [-options]\n" );
			printf( "       -n<n>  number of tests       (def: %d)\n", count );
			printf( "       -s<n>  seconds of test       (def: %d)\n", secs );
			printf( "       -b<n>  size of pixmap        (def: %d)\n", bsize );
			printf( "       -m<n>  average move distance (def: %d)\n", avgmove );
			exit(0);
		}
	}

	printf( "\nBenchmark for different animation modes of flying -deluxe\n" );
	printf( "Mode 2 usually turns out to be the fastest and\n" );
	printf( "therefore is turned on by default in the game.\n\n" );
	printf( "    Mode 1        Mode 2       Mode 3\n" );
	printf( "------------------------------------------\n" );
	fflush( stdout );
	while( count-- > 0 ) {
		for (int j=1;j<=3;j++) {
			switch(j) {
			case 1: xm = new XMoveChecker1( bsize, avgmove );	break;
			case 2: xm = new XMoveChecker2( bsize, avgmove );	break;
			case 3: xm = new XMoveChecker3( bsize, avgmove );	break;
			case 4: xm = new XMoveChecker4( bsize, avgmove );	break;
			}

			printf( "%6.2f pics/s ", xm->loop(secs)/(double)secs );
			fflush(stdout);
			delete xm;
		}
		printf( "\n" );
	}
	printf("\nThe benchmark computes the number frames per second by\n" );
	printf( "assuming that all 16 balls are moving at constant speed.\n" );

	printf("\nby Helmut H�nig, May 1995\n\n" );
}