[cairo-commit] cairo-demo/gameoflife cgolwin.cpp,1.2,1.3

Andrew Chant commit at pdx.freedesktop.org
Mon Nov 24 20:22:05 PST 2003


Committed by: chant

Update of /cvs/cairo/cairo-demo/gameoflife
In directory pdx:/tmp/cvs-serv11000

Modified Files:
	cgolwin.cpp 
Log Message:
Use cairo's transform matrix instead of doing my own scaling.
Works much much better.


Index: cgolwin.cpp
===================================================================
RCS file: /cvs/cairo/cairo-demo/gameoflife/cgolwin.cpp,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** cgolwin.cpp	25 Nov 2003 02:25:04 -0000	1.2
--- cgolwin.cpp	25 Nov 2003 04:22:03 -0000	1.3
***************
*** 98,127 ****
  
  void gameoflifeWin::render()
! {
  	if (grid == 0)
! 	{	// new or resized
! 		cellwidth = (double)width / (double)cols;
! 		cellheight = (double) height / (double) rows;
! 		cairo_set_line_width(cr, (cellwidth + cellheight) / 20.0);
! 		radius = (cellwidth > cellheight) ? cellheight : cellwidth;
! 		radius /= 2.0;
! 		radius -= .5;
! 		grid = XCreatePixmap(dpy, w, width, height, DefaultDepth(dpy, DefaultScreen(dpy)));
! 		buffer = XCreatePixmap(dpy, w, width, height, DefaultDepth(dpy, DefaultScreen(dpy)));
  		
  		/* draw the grid into a pixmap which can be copied over the buffer each cycle */
  		XFillRectangle(dpy, grid, gc, 0, 0, width, height);
  		
  		cairo_set_target_drawable(cr, dpy, grid);
! 		cairo_set_rgb_color(cr, .7, .7, .7);
  		for (int col = 0; col <= cols; col++)
  		{
! 			cairo_move_to(cr, col*cellwidth, 0);
! 			cairo_rel_line_to(cr, 0, cellheight * rows);
  		}
  		for (int row = 0; row <= rows; row++)
  		{
! 			cairo_move_to(cr, 0, row*cellheight);
! 			cairo_rel_line_to(cr, cellwidth*cols, 0);
  		}
  		cairo_stroke(cr);
--- 98,139 ----
  
  void gameoflifeWin::render()
! { 	/* note: all cairo ops operate on a rowxcol coordinate system
! 	   	 the cairo transform matrix handles resizing etc */
  	if (grid == 0)
! 	{	  
! 		cairo_default_matrix(cr);
! 		cairo_scale(cr, (double)width/(double)cols,(double)height/(double)rows); // I hope this goes the right way..
! 		cairo_set_line_width(cr, 0.1); // set to one tenth of a unit... will that be row or col?
! 		radius = 0.5; // so, the edges of circles in connecting cells should just touch.
  		
+ 		buffer = XCreatePixmap(dpy, w, width, height, DefaultDepth(dpy, DefaultScreen(dpy))); // cairo co-ords
  		/* draw the grid into a pixmap which can be copied over the buffer each cycle */
+ 		grid = XCreatePixmap(dpy, w, width, height, DefaultDepth(dpy, DefaultScreen(dpy))); // these are in pixels, not
  		XFillRectangle(dpy, grid, gc, 0, 0, width, height);
  		
+ 		/* needs work
+ 		if (use_cairo_show_surface)
+ 		{
+ 			cellpix = XCreatePixmap(dpy, w, ceil(cellwidth), 
+ 					ceil(cellheight), DefaultDepth(dpy, DefaultScreen(dpy)));
+ 			surface = cairo_xlib_surface_create(dpy, cellpix, 
+ 					DefaultVisual(dpy, DefaultScreen(dpy)),
+ 					0, DefaultColormap(dpy, DefaultScreen(dpy)));
+ 			if (surface == NULL)
+ 				exit(1); // no mem
+ 		}
+ 		*/
+ 		
  		cairo_set_target_drawable(cr, dpy, grid);
! 		cairo_set_rgb_color(cr, .8, .8, .8);
  		for (int col = 0; col <= cols; col++)
  		{
! 			cairo_move_to(cr, col, 0);
! 			cairo_rel_line_to(cr, 0, rows);
  		}
  		for (int row = 0; row <= rows; row++)
  		{
! 			cairo_move_to(cr, 0, row);
! 			cairo_rel_line_to(cr, cols, 0);
  		}
  		cairo_stroke(cr);
***************
*** 130,134 ****
  	
  	cells = game->getboard();
! 	
  	XCopyArea(dpy, grid, buffer, gc, 0, 0, width, height, 0, 0);
  	cairo_set_target_drawable(cr, dpy, buffer);
--- 142,146 ----
  	
  	cells = game->getboard();
! 	// Copy gen'd grid to buffer
  	XCopyArea(dpy, grid, buffer, gc, 0, 0, width, height, 0, 0);
  	cairo_set_target_drawable(cr, dpy, buffer);
***************
*** 145,150 ****
  		if (i->dna & G1) green += .50;
  		if (i->dna & G2) green += .25;
  		cairo_set_rgb_color(cr, red, green, blue);
! 		cairo_arc(cr, cellwidth*(i->x + .5), cellheight * (i->y + .5), radius, 0, 2*M_PI);
  		cairo_fill(cr);
  	}
--- 157,170 ----
  		if (i->dna & G1) green += .50;
  		if (i->dna & G2) green += .25;
+ 		/* needs work... 
+ 		if (use_cairo_show_surface)
+ 		{
+ 			// .. ok , gen surface
+ 			cairo_show_surface(cr, surface, cellwidth, cellheight);
+ 		}
+ 		else 
+ 		{ */
  		cairo_set_rgb_color(cr, red, green, blue);
! 		cairo_arc(cr, i->x + .5, i->y + .5, radius, 0, 2*M_PI);
  		cairo_fill(cr);
  	}





More information about the cairo-commit mailing list