[cairo-commit] cairo-demo/gameoflife ChangeLog, 1.2, 1.3 Makefile, 1.5, 1.6 cgolwin.cpp, 1.7, 1.8 cgolwin.h, 1.3, 1.4 ctk.cpp, 1.2, 1.3

Behdad Esfahbod commit at pdx.freedesktop.org
Tue Aug 2 02:06:49 PDT 2005


Committed by: behdad

Update of /cvs/cairo/cairo-demo/gameoflife
In directory gabe:/tmp/cvs-serv2246

Modified Files:
	ChangeLog Makefile cgolwin.cpp cgolwin.h ctk.cpp 
Log Message:
2005-08-02  Behdad Esfahbod  <behdad at behdad.org>

        * cgolwin.h, cgolwin.cpp: Get rid of xlib completely.



Index: ChangeLog
===================================================================
RCS file: /cvs/cairo/cairo-demo/gameoflife/ChangeLog,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- ChangeLog	29 Jul 2005 21:16:12 -0000	1.2
+++ ChangeLog	2 Aug 2005 09:06:47 -0000	1.3
@@ -1,3 +1,7 @@
+2005-08-02  Behdad Esfahbod  <behdad at behdad.org>
+
+	* cgolwin.h, cgolwin.cpp: Get rid of xlib completely.
+
 2005-07-29  Behdad Esfahbod  <behdad at behdad.org>
 
 	* cgolwin.cpp: Fixed bug that was introduced when blindly changing

Index: Makefile
===================================================================
RCS file: /cvs/cairo/cairo-demo/gameoflife/Makefile,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- Makefile	29 Jul 2005 21:16:12 -0000	1.5
+++ Makefile	2 Aug 2005 09:06:47 -0000	1.6
@@ -10,7 +10,7 @@
 
 all: $(APPS)
 
-$(OBJETS): $(HEADERS)
+$(OBJECTS): $(HEADERS)
 $(APPS): $(OBJECTS)
 	$(CXX) $(LDFLAGS) -o $@ $^
 

Index: cgolwin.cpp
===================================================================
RCS file: /cvs/cairo/cairo-demo/gameoflife/cgolwin.cpp,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- cgolwin.cpp	29 Jul 2005 21:16:12 -0000	1.7
+++ cgolwin.cpp	2 Aug 2005 09:06:47 -0000	1.8
@@ -7,19 +7,19 @@
  *  licensed under GPL
  *
  *   This file is part of cgol.
-    cgol is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
-
-    cgol is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with cgol; if not, write to the Free Software
-    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  cgol is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  cgol is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with cgol; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
 
@@ -32,31 +32,80 @@
 using std::cout;
 
 gameoflifeWin::gameoflifeWin(Ctkapp * app, int wi, int h, int r, int c, bool s)
- :Ctkwin(app,wi,h)
+ :Ctkwin(app,wi,h), buffer(NULL), rows(r), cols(c), width(wi), height(h)
 {
-	rows = r;
-	cols = c;
-	width = wi;
-	height = h;
 	use_cairo_show_surface = s;
+
 	game = new gameoflife(rows, cols);
 
-	surface = cairo_xlib_surface_create(dpy, cellpix, 
+	onscreen = cairo_xlib_surface_create(dpy, w, 
 			DefaultVisual(dpy, DefaultScreen(dpy)),
 			wi, h);
-	if (cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS)
-		exit(1);
-	cr = cairo_create(surface);
-	
-	XGCValues xgcv;
-	// Helps prevent *some* choking on opaque resize 
-	xgcv.graphics_exposures = false;
+	onscreen_cr = cairo_create (onscreen);
 	
-	gc = XCreateGC(dpy, w, GCGraphicsExposures, &xgcv);
-	XSetForeground(dpy, gc, WhitePixel(dpy, DefaultScreen(dpy)));
 	// MAX 24 fps, really just means 1/24 s between advancements
 	app->regtimer(0,1000000/24,-1,advanceTimer, (void *)this);
-	buffer = grid = 0;
+}
+
+void gameoflifeWin::initBuffers()
+{
+	buffer = cairo_surface_create_similar (onscreen,
+						CAIRO_CONTENT_COLOR_ALPHA,
+						width, height);
+	grid = cairo_surface_create_similar (onscreen,
+						CAIRO_CONTENT_COLOR_ALPHA,
+						width, height);
+	cellpix = cairo_surface_create_similar (onscreen,
+						CAIRO_CONTENT_ALPHA,
+						width/cols+1, height/rows+1);
+
+	initBoard();
+}
+
+void gameoflifeWin::freeBuffers(void)
+{
+	if (buffer)
+	{
+		cairo_surface_destroy (buffer);
+		cairo_surface_destroy (grid);
+		cairo_surface_destroy (cellpix);
+		buffer = grid = cellpix = NULL;
+	}
+}
+
+void gameoflifeWin::initBoard()
+{
+	cairo_t *cr = cairo_create (grid);
+
+	cairo_scale(cr, (double)width/(double)cols,
+			(double)height/(double)rows); 
+	cairo_get_matrix (cr, &matrix);
+
+	cairo_set_line_width(cr, 0.1);
+	cairo_set_source_rgb(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);
+	cairo_destroy(cr);
+	
+	if (use_cairo_show_surface) // Broken
+	{	//todo : fix mem leak here 
+		cr = cairo_create (cellpix);
+		cairo_set_matrix (cr, &matrix);
+		cairo_arc(cr, .5,  .5, .5, 0, 2*M_PI);
+		cairo_fill(cr);
+		cairo_destroy (cr);
+	}
+	
 }
 
 void gameoflifeWin::event(XEvent * e)
@@ -70,25 +119,12 @@
 					render();
 		}
 		break;
-	case CreateNotify:
-		width = ((XCreateWindowEvent *)e)->width;
-		height = ((XCreateWindowEvent *)e)->height;
-		if (grid != 0)
-		{
-			XFreePixmap(dpy, grid);
-			XFreePixmap(dpy, buffer);
-			buffer = grid = 0;
-		}
-		break;
 	case ConfigureNotify:
+		freeBuffers();
+	case CreateNotify:
 		width = ((XConfigureEvent *)e)->width;
 		height = ((XConfigureEvent *)e)->height;
-		if (grid != 0)
-		{
-			XFreePixmap(dpy, grid);
-			XFreePixmap(dpy, buffer);
-			buffer = grid = 0;
-		}
+		cairo_xlib_surface_set_size (onscreen, width, height);
 		break;
 	default:
 		//cout << "Unhandled Event " << e->type << "\n";
@@ -104,55 +140,28 @@
 }
 
 void gameoflifeWin::render()
-{ 	/* note: all cairo ops operate on a rowxcol coordinate system
-	   	 the cairo transform matrix handles resizing etc */
-	if (grid == 0)
-	{	  
-		cairo_identity_matrix(cr);
-		cairo_scale(cr, (double)width/(double)cols,(double)height/(double)rows); 
-		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);
-		
-		if (use_cairo_show_surface) // Broken
-		{	//todo : fix mem leak here 
-			cairo_save(cr);
-			cairo_identity_matrix(cr);
-			cairo_scale(cr,50,50);
-			cellpix = XCreatePixmap(dpy, w, 50, 
-					50, DefaultDepth(dpy, DefaultScreen(dpy)));
-			XFillRectangle(dpy, cellpix, gc, 0, 0, 50, 50);
-			cairo_set_source_rgb(cr,0,0,0);
-			cairo_arc(cr, .5,  .5, .5, 0, 2*M_PI);
-			cairo_fill(cr);
-			cairo_restore(cr);
-		}
-		
-		
-                cairo_xlib_surface_set_drawable (surface, grid, width, height);
-		cairo_set_source_rgb(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);
-		cairo_set_source_rgb(cr, 0, 0, 0);
-	}
+{
+	cairo_t *cr;
+
+	if (!buffer)
+		initBuffers();
+
 	
 	cells = game->getboard();
+
+	cr = cairo_create (buffer);
+
+	cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
+	cairo_paint (cr);
+
 	// Copy gen'd grid to buffer
-	XCopyArea(dpy, grid, buffer, gc, 0, 0, width, height, 0, 0);
-        cairo_xlib_surface_set_drawable (surface, buffer, width, height);
+	cairo_set_source_surface (cr, grid, 0, 0);
+	cairo_paint (cr);
+
+	cairo_set_matrix (cr, &matrix);
+	/* note: all cairo ops operate on a rowxcol coordinate system
+	   	 the cairo transform matrix handles resizing etc */
+
 	for (list<struct cell>::const_iterator i = cells->begin();
 		i != cells->end(); i++)
 	{
@@ -169,18 +178,25 @@
 		if (use_cairo_show_surface)
 		{
 			//Note: broken 
+			cairo_save (cr);
+			cairo_identity_matrix (cr);
+			cairo_set_source_rgb (cr, red, green, blue);
+			cairo_set_source_surface (cr, cellpix, 0, 0);
+			cairo_set_matrix (cr, &matrix);
 			cairo_move_to(cr, i->x, i->y);
-			cairo_set_source_surface(cr, surface, 1, 1);
-			cairo_paint(cr);
+			cairo_paint (cr);
+			cairo_restore (cr);
 		}
 		else 
 		{ 
-			cairo_set_source_rgb(cr, red, green, blue);
-			cairo_arc(cr, i->x + .5, i->y + .5, radius, 0, 2*M_PI);
-			cairo_fill(cr);
+			cairo_set_source_rgb (cr, red, green, blue);
+			cairo_arc (cr, i->x + .5, i->y + .5, .5, 0, 2*M_PI);
+			cairo_fill (cr);
 		}
 	}
-	cairo_set_source_rgb(cr, 0, 0, 0);
-	XCopyArea(dpy, buffer, w, gc, 0, 0, width, height, 0, 0);
-	XFlush(dpy);
+
+	cairo_destroy (cr);
+
+	cairo_set_source_surface (onscreen_cr, buffer, 0, 0);
+	cairo_paint (onscreen_cr);
 }

Index: cgolwin.h
===================================================================
RCS file: /cvs/cairo/cairo-demo/gameoflife/cgolwin.h,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- cgolwin.h	29 Jul 2005 05:40:44 -0000	1.3
+++ cgolwin.h	2 Aug 2005 09:06:47 -0000	1.4
@@ -37,13 +37,13 @@
 	list<struct cell> * cells;
 	gameoflife * game;
 private:
-	Pixmap grid, buffer, cellpix;
-	cairo_t * cr;
-	cairo_surface_t * surface;
+	void initBuffers(void);
+	void freeBuffers(void);
+	void initBoard(void);
+	cairo_t *onscreen_cr;
+	cairo_surface_t *onscreen, *buffer, *grid, *cellpix;
+	cairo_matrix_t matrix;
 	bool use_cairo_show_surface;
 	int rows, cols, width, height;
-	double cellheight, cellwidth;
-	double radius;
-	GC gc;
 };
 

Index: ctk.cpp
===================================================================
RCS file: /cvs/cairo/cairo-demo/gameoflife/ctk.cpp,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- ctk.cpp	25 Nov 2003 07:13:17 -0000	1.2
+++ ctk.cpp	2 Aug 2005 09:06:47 -0000	1.3
@@ -5,19 +5,19 @@
  *  licensed under GPL
  *
  *   This file is part of cgol.
-    cgol is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
-
-    cgol is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with cgol; if not, write to the Free Software
-    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  cgol is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  cgol is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with cgol; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
 /* C++ Xlib skeleton */




More information about the cairo-commit mailing list