[SDL] Re: Xlib stuff

Pierre Phaneuf pp at ludusdesign.com
Tue Jan 4 12:50:06 PST 2000


hayward at slothmud.org wrote:

> BTW.  You didn't attach the program, you might try again and I'll take
> your suggestion and try it out.

Oops! Here it is!

-- 
Pierre Phaneuf
Ludus Design, http://ludusdesign.com/
"First they ignore you. Then they laugh at you.
Then they fight you. Then you win." -- Gandhi
-------------- next part --------------
#include <sys/time.h>
#include <stdio.h>
#include <X11/Xlib.h>

#undef TILE_TRICK
#undef GRAB_SERVER

#define WINDOW_X 640
#define WINDOW_Y 480
#define BLIT_X 640
#define BLIT_Y 480

#define DEPTH(display) DefaultDepth(display, DefaultScreen(display))

int main() {
  unsigned int frames;
  int i, j;
  int buf;
  Display* dpy;
  XPixmapFormatValues* pixmapfmt;
  Window win;
  GC gc;
  Pixmap bufs[2];
  struct timeval time1;
  struct timeval time2;
  unsigned int diff;
  unsigned int bytes_per_pixel = 0;
  double mbps;
  XGCValues gcvalues;

  dpy = XOpenDisplay(NULL);

  win = XCreateSimpleWindow(dpy,
			    DefaultRootWindow(dpy),
			    0, 0, /* x, y */
			    WINDOW_X, WINDOW_Y, /* width, height */
			    0, 0, /* border_width, border */
			    None); /* background */

  XSelectInput(dpy, win, ExposureMask | KeyPressMask);

  XMapWindow(dpy, win);

  bufs[0] = XCreatePixmap(dpy,
		       win,
		       BLIT_X, BLIT_Y, /* width, height */
		       DEPTH(dpy)); /* depth */

  bufs[1] = XCreatePixmap(dpy,
		       win,
		       BLIT_X, BLIT_Y, /* width, height */
		       DEPTH(dpy)); /* depth */

  gcvalues.function = GXcopy;
  gcvalues.plane_mask = AllPlanes;
  gcvalues.fill_style = FillSolid;
#ifdef TILE_TRICK
  gcvalues.tile = bufs[0];
  gcvalues.ts_x_origin = 0;
  gcvalues.ts_y_origin = 0;
#endif
  gcvalues.graphics_exposures = False;

  gc = XCreateGC(dpy,
		 win,
		 GCFunction
		 | GCPlaneMask
		 | GCFillStyle
#ifdef TILE_TRICK
		 | GCTile
		 | GCTileStipXOrigin
		 | GCTileStipYOrigin
#endif
		 | GCGraphicsExposures,
		 &gcvalues);

  XSetForeground(dpy,
		 gc,
		 WhitePixel(dpy, DefaultScreen(dpy)));

  XFillRectangle(dpy,
		 bufs[0],
		 gc,
		 0, 0, /* x, y */
		 BLIT_X, BLIT_Y); /* width, height */

  XFillRectangle(dpy,
		 win,
		 gc,
		 0, 0,
		 640, 240);

  XSetForeground(dpy,
		 gc,
		 BlackPixel(dpy, DefaultScreen(dpy)));

  XFillRectangle(dpy,
		 bufs[1],
		 gc,
		 0, 0, /* x, y */
		 BLIT_X, BLIT_Y); /* width, height */

  pixmapfmt = XListPixmapFormats(dpy, &i);

  for(j = 0; j < i; j++) {
    printf("Pixmap format #%i\n", j);
    printf("  depth          = %i\n", pixmapfmt[j].depth);
    printf("  bits_per_pixel = %i\n", pixmapfmt[j].bits_per_pixel);
    printf("  scanline_pad   = %i\n", pixmapfmt[j].scanline_pad);
    if(pixmapfmt[j].depth == DEPTH(dpy)) {
      printf("  is default pixmap format\n");
      bytes_per_pixel = pixmapfmt[j].bits_per_pixel / 8;
    }
  }

  XFree(pixmapfmt);

  while(1) {
    XEvent ev;

    XNextEvent(dpy, &ev);

    if(ev.type == Expose) {
      printf("got Expose\n");
      break;
    }
  }

  frames = 0;
  buf = 0;

  XSetGraphicsExposures(dpy,
			gc,
			False);

  gettimeofday(&time1, NULL);

#ifdef GRAB_SERVER
  XGrabServer(dpy);
#endif

  while(1) {
#ifdef TILE_TRICK
    XSetWindowBackgroundPixmap(dpy, win, bufs[buf]);
    XClearArea(dpy, win, 0, 0, BLIT_X, BLIT_Y, False);
#else
    XCopyArea(dpy,
	      bufs[buf],
	      win,
	      gc,
	      0, 0, /* src_x, src_y */
	      BLIT_X, BLIT_Y, /* width, height */
	      0, 0); /* dest_x, dest_y */
#endif

    XSync(dpy, False);

    if(XPending(dpy)) {
      XEvent ev;

      XNextEvent(dpy, &ev);

      if(ev.type == KeyPress && ev.xkey.keycode == 9)
	break;
    }

    buf = 1 - buf;
    frames++;
  }

#ifdef GRAB_SERVER
  XUngrabServer(dpy);
#endif

  gettimeofday(&time2, NULL);

  diff = (time2.tv_sec * 1000) + (time2.tv_usec / 1000);
  diff -= (time1.tv_sec * 1000) + (time1.tv_usec / 1000);

  printf("frames = %i\n", frames);
  printf("ms = %i\n", diff);
  printf("bpp = %i\n", bytes_per_pixel);
  printf("fps = %i\n", (frames * 1000) / diff);

  mbps = (frames * 1000) / diff;
  mbps = mbps * (BLIT_X * BLIT_Y * bytes_per_pixel);
  mbps = mbps / (1024 * 1024);

  printf("MB/sec = %f\n", mbps);

  XUnmapWindow(dpy, win);
  XDestroyWindow(dpy, win);

  XFreePixmap(dpy, bufs[0]);
  XFreePixmap(dpy, bufs[1]);

  XCloseDisplay(dpy);

  return 0;
}


More information about the SDL mailing list