[Commit] grrobot/src grr_board_view.c, 1.11, 1.12 grr_board_view.h, 1.8, 1.9 grr_icon.c, 1.2, 1.3 grr_icon.h, 1.2, 1.3 grrobot.c, 1.14, 1.15

Carl Worth commit at keithp.com
Wed Jul 23 15:34:37 PDT 2003


Committed by: cworth

Update of /local/src/CVS/grrobot/src
In directory home.keithp.com:/tmp/cvs-serv27287

Modified Files:
	grr_board_view.c grr_board_view.h grr_icon.c grr_icon.h 
	grrobot.c 
Log Message:
Fixed select handling. Added two of Richard's fancy logos.
Upgraded to do incremental rendering --- much faster now.

Index: grr_board_view.c
===================================================================
RCS file: /local/src/CVS/grrobot/src/grr_board_view.c,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -r1.11 -r1.12
--- grr_board_view.c	11 Jul 2003 01:11:41 -0000	1.11
+++ grr_board_view.c	23 Jul 2003 21:34:34 -0000	1.12
@@ -29,7 +29,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include <Xr.h>
+#include <cairo.h>
 #include <xsvg.h>
 
 #include <gtk/gtkmain.h>
@@ -66,7 +66,6 @@
 						     GdkEventMotion   *event);
 
 static void grr_board_view_update_mouse             (grr_board_view_t *view, gint x, gint y);
-static void grr_board_view_update                   (grr_board_view_t *view);
 
 /* Local data */
 
@@ -154,12 +153,22 @@
 grr_board_view_new (rr_board_t *board)
 {
     grr_board_view_t *view;
+    int x, y;
     
     view = g_object_new (grr_board_view_get_type (), NULL);
 
     view->board = board;
+    rr_board_get_size (board, &view->board_width, &view->board_height);
 
-    grr_board_view_update (view);
+    view->damaged = malloc (view->board_height * sizeof (int *));
+    for (y = 0; y < view->board_height; y++) {
+	view->damaged[y] = malloc (view->board_width * sizeof (int));
+	for (x = 0; x< view->board_width; x++) {
+	    view->damaged[y][x] = 1;
+	}
+    }
+
+    gtk_widget_queue_draw (GTK_WIDGET (view));
 
     return view;
 }
@@ -285,7 +294,7 @@
 
 static void
 grr_board_view_draw_walls (grr_board_view_t	*view,
-			   XrState		*xrs,
+			   cairo_t		*xrs,
 			   rr_wall_t		wall)
 {
     if (wall == RR_WALL_NONE)
@@ -295,90 +304,24 @@
 	grr_icon_draw (view->wall_icon, xrs);
     }
     if (wall & RR_WALL_LEFT) {
-	XrSave (xrs);
-	XrRotate (xrs, M_PI_2);
+	cairo_save (xrs);
+	cairo_rotate (xrs, M_PI_2);
 	grr_icon_draw (view->wall_icon, xrs);
-	XrRestore (xrs);
+	cairo_restore (xrs);
     }
     if (wall & RR_WALL_RIGHT) {
-	XrSave (xrs);
-	XrTranslate (xrs, GRR_SVG_ASSUMED_WIDTH, 0);
-	XrRotate (xrs, M_PI_2);
+	cairo_save (xrs);
+	cairo_translate (xrs, GRR_SVG_ASSUMED_WIDTH, 0);
+	cairo_rotate (xrs, M_PI_2);
 	grr_icon_draw (view->wall_icon, xrs);
-	XrRestore (xrs);
+	cairo_restore (xrs);
     }
     if (wall & RR_WALL_BELOW) {
-	XrSave (xrs);
-	XrTranslate (xrs, 0, GRR_SVG_ASSUMED_HEIGHT);
+	cairo_save (xrs);
+	cairo_translate (xrs, 0, GRR_SVG_ASSUMED_HEIGHT);
 	grr_icon_draw (view->wall_icon, xrs);
-	XrRestore (xrs);
-    }
-}
-
-static void
-grr_board_view_draw_cell (grr_board_view_t	*view,
-			  XrState		*xrs,
-			  rr_cell_t		cell,
-			  rr_target_t		goal,
-			  int			width,
-			  int			height)
-{
-    int xpad, ypad;
-    rr_robot_t robot;
-    rr_target_t target;
-
-    XrSave (xrs);
-    XrScale (xrs,
-	     width / GRR_SVG_ASSUMED_WIDTH,
-	     height / GRR_SVG_ASSUMED_HEIGHT);
-    grr_icon_draw (view->cell_icon, xrs);
-    grr_board_view_draw_walls (view, xrs, RR_CELL_GET_WALLS (cell));
-    XrRestore (xrs);
-
-    xpad = width / 10;
-    ypad = width / 10;
-
-    XrSave (xrs);
-    XrTranslate (xrs, xpad, ypad);
-    XrScale (xrs,
-	     (width - 2*xpad)  / GRR_SVG_ASSUMED_WIDTH,
-	     (height - 2*ypad) / GRR_SVG_ASSUMED_HEIGHT);
-
-    target = RR_CELL_GET_TARGET (cell);
-    if (target)
-	grr_icon_draw (view->target_icon[rr_target_idx (target)], xrs);
-
-    XrRestore (xrs);
-
-    /* XXX: This is a kludge, (it obscures the cell background in
-       addition to the target). The real way to do this is to draw the
-       target itself with less opacity. */
-    if (target && target != goal) {
-	XrSave (xrs);
-	XrScale (xrs,
-		 width / GRR_SVG_ASSUMED_WIDTH,
-		 height / GRR_SVG_ASSUMED_HEIGHT);
-	XrRectangle (xrs, 1, 1,
-		     GRR_SVG_ASSUMED_WIDTH - 1,
-		     GRR_SVG_ASSUMED_HEIGHT - 1);
-	XrSetRGBColor (xrs, 1, 1, 1);
-	XrSetAlpha (xrs, 0.75);
-	XrFill (xrs);
-	XrRestore (xrs);
+	cairo_restore (xrs);
     }
-
-    XrSave (xrs);
-    XrTranslate (xrs, xpad, ypad);
-    XrScale (xrs,
-	     (width - 2*xpad)  / GRR_SVG_ASSUMED_WIDTH,
-	     (height - 2*ypad) / GRR_SVG_ASSUMED_HEIGHT);
-
-    robot = RR_CELL_GET_ROBOT (cell);
-    if (robot)
-	grr_icon_draw (view->robot_icon[rr_robot_idx (robot)], xrs);
-
-    XrRestore (xrs);
-
 }
 
 static gint
@@ -389,7 +332,7 @@
     rr_board_t *board;
     Display *dpy;
     Drawable drawable;
-    XrState *xrs;
+    cairo_t *xrs;
     GdkDrawable *real_drawable;
     gint x_off, y_off;
     int i, j;
@@ -400,9 +343,6 @@
     g_return_val_if_fail (GRR_IS_BOARD_VIEW (widget), FALSE);
     g_return_val_if_fail (event != NULL, FALSE);
 
-    if (event->count > 0)
-	return FALSE;
-
     view = GRR_BOARD_VIEW (widget);
     board = view->board;
 
@@ -411,11 +351,9 @@
     dpy = gdk_x11_drawable_get_xdisplay (real_drawable);
     drawable = gdk_x11_drawable_get_xid (real_drawable);
 
-    /* Ignore GTK+ and use Xr for drawing. */
-    xrs = XrCreate ();
-    XrSetTargetDrawable (xrs, dpy, drawable);
-
-    rr_board_get_size (board, &view->board_width, &view->board_height);
+    /* Ignore GTK+ and use Cairo for drawing. */
+    xrs = cairo_create ();
+    cairo_set_target_drawable (xrs, dpy, drawable);
 
     new_cell_width = widget->allocation.width / view->board_width;
     if (new_cell_width == 0)
@@ -424,11 +362,6 @@
     if (new_cell_height == 0)
 	new_cell_height = 1;
 
-    view->cell_width = new_cell_width;
-    view->cell_height = new_cell_height;
-
-    /* Pre-render icons if the size has changed */
-    /* XXX: This code isn't fully baked yet
     if (new_cell_width != view->cell_width
 	|| new_cell_height != view->cell_height) {
 	int i;
@@ -438,8 +371,8 @@
 	view->cell_width = new_cell_width;
 	view->cell_height = new_cell_height;
 
-	XrSave (xrs);
-	XrScale (xrs,
+	cairo_save (xrs);
+	cairo_scale (xrs,
 		 view->cell_width / GRR_SVG_ASSUMED_WIDTH,
 		 view->cell_height / GRR_SVG_ASSUMED_HEIGHT);
 
@@ -461,69 +394,134 @@
 			      view->cell_width, view->cell_height);
 	}
 
-	XrRestore (xrs);
+	cairo_restore (xrs);
     }
-    */
 
-    XrTranslate (xrs, -x_off, -y_off);
+    view->cell_width = new_cell_width;
+    view->cell_height = new_cell_height;
+
+    if (event->area.width) {
+	int x_start, y_start;
+	int x, y;
+	int x_stop, y_stop;
+	grr_board_view_transform_pixel_to_cell (view,
+						event->area.x, event->area.y,
+						&x_start, &y_start);
+	grr_board_view_transform_pixel_to_cell (view,
+						event->area.x + event->area.width - 1,
+						event->area.y + event->area.height - 1,
+						&x_stop, &y_stop);
+	for (y = y_start; y <= y_stop; y++)
+	    for (x = x_start; x <= x_stop; x++)
+		view->damaged[y][x] = 1;
+    }
+
+    if (event->count > 0)
+	return FALSE;
+
+    cairo_translate (xrs, -x_off, -y_off);
 
     view->board_pad_x = (widget->allocation.width - view->board_width * view->cell_width) / 2;
     view->board_pad_y = (widget->allocation.height - view->board_height * view->cell_height) / 2;
 
-    XrTranslate (xrs, view->board_pad_x, view->board_pad_y);
+    cairo_translate (xrs, view->board_pad_x, view->board_pad_y);
 
     goal_target = rr_board_get_goal_target (board);
 
-    /* Draw cell targets */
+    /* Draw cell contents */
     for (j=0; j < view->board_height; j++) {
 	for (i=0; i < view->board_width; i++) {
-	    XrSave (xrs);
-	    XrTranslate (xrs, i * view->cell_width, j * view->cell_height);
-	    grr_board_view_draw_cell (view, xrs,
-				      rr_board_get_cell (board, i, j),
-				      goal_target,
-				      view->cell_width, view->cell_height);
-	    XrRestore (xrs);
+	    if (! view->damaged[j][i])
+		continue;
+	    rr_cell_t cell = rr_board_get_cell (board, i, j);
+	    rr_target_t target = RR_CELL_GET_TARGET (cell);
+	    rr_robot_t robot = RR_CELL_GET_ROBOT (cell);
+
+	    cairo_save (xrs);
+	    cairo_translate (xrs, i * view->cell_width, j * view->cell_height);
+
+	    grr_icon_draw_predrawn (view->cell_icon, xrs);
+
+	    if (target) {
+		if (target && target != goal_target) {
+		    cairo_save (xrs);
+		    cairo_set_alpha (xrs, 0.25);
+		    grr_icon_draw_predrawn (view->target_icon[rr_target_idx (target)], xrs);
+		    cairo_restore (xrs);
+		} else {
+		    grr_icon_draw_predrawn (view->target_icon[rr_target_idx (target)], xrs);
+		}
+	    }
+
+	    if (robot)
+		grr_icon_draw_predrawn (view->robot_icon[rr_robot_idx (robot)], xrs);
+
+	    cairo_restore (xrs);
 	}
     }
 
     /* Draw goal target in center of board */
     /* XXX: Not a perfect heuristic. Should check for the vacant box too. */
     if (view->board_width == 16 && view->board_height == 16) {
-	XrSave (xrs);
-	XrTranslate (xrs,
+	cairo_save (xrs);
+	cairo_translate (xrs,
 		     (view->board_width / 2 - 1) * view->cell_width,
 		     (view->board_height / 2 - 1) * view->cell_height);
-	grr_board_view_draw_cell (view, xrs, goal_target, goal_target,
-				  view->cell_width * 2, view->cell_height * 2);
-	XrRestore (xrs);
+	cairo_scale (xrs,
+		     2 * view->cell_width / GRR_SVG_ASSUMED_WIDTH,
+		     2 * view->cell_height / GRR_SVG_ASSUMED_HEIGHT);
+	grr_icon_draw (view->target_icon[rr_target_idx (goal_target)], xrs);
+	cairo_restore (xrs);
     }
 
     /* Draw walls */
     for (j=0; j < view->board_height; j++) {
 	for (i=0; i < view->board_width; i++) {
-	    XrSave (xrs);
-	    XrTranslate (xrs, i * view->cell_width, j * view->cell_height);
-	    XrScale (xrs,
+	    if (! view->damaged[j][i])
+		continue;
+	    view->damaged[j][i] = 0;
+	    cairo_save (xrs);
+	    cairo_translate (xrs, i * view->cell_width, j * view->cell_height);
+	    cairo_scale (xrs,
 		     view->cell_width / GRR_SVG_ASSUMED_WIDTH,
 		     view->cell_height / GRR_SVG_ASSUMED_HEIGHT);
 	    grr_board_view_draw_walls (view, xrs, RR_CELL_GET_WALLS (rr_board_get_cell(board, i, j)));
-	    XrRestore (xrs);
+	    cairo_restore (xrs);
 	}
     }
     
-    XrDestroy (xrs);
+    cairo_destroy (xrs);
 
     return FALSE;
 }
 
-static void
-grr_board_view_pointer_coords_to_grid (grr_board_view_t *view,
-				       int pointer_x, int pointer_y,
-				       int *grid_x, int *grid_y)
+void
+grr_board_view_transform_pixel_to_cell (grr_board_view_t *view,
+					int pixel_x, int pixel_y,
+					int *cell_x, int *cell_y)
 {
-    *grid_x = (pointer_x - view->board_pad_x) / view->cell_width;
-    *grid_y = (pointer_y - view->board_pad_y) / view->cell_height;
+    int x, y;
+    x = (pixel_x - view->board_pad_x) / view->cell_width;
+    y = (pixel_y - view->board_pad_y) / view->cell_height;
+    if (x < 0)
+	x = 0;
+    if (x >= view->board_width)
+	x = view->board_width - 1;
+    if (y < 0)
+	y = 0;
+    if (y >= view->board_height)
+	y = view->board_height - 1;
+    *cell_x = x;
+    *cell_y = y;
+}
+
+void
+grr_board_view_transform_cell_to_pixel (grr_board_view_t *view,
+					int cell_x, int cell_y,
+					int *pixel_x, int *pixel_y)
+{
+    *pixel_x = view->cell_width * cell_x + view->board_pad_x;
+    *pixel_y = view->cell_height * cell_y + view->board_pad_y;
 }
 
 static gint
@@ -540,9 +538,9 @@
     
     view = GRR_BOARD_VIEW (widget);
 
-    grr_board_view_pointer_coords_to_grid (view,
-					   event->x, event->y,
-					   &x, &y);
+    grr_board_view_transform_pixel_to_cell (view,
+					    event->x, event->y,
+					    &x, &y);
   
     cell = rr_board_get_cell (view->board, x, y);
     if (!view->button && RR_CELL_GET_ROBOT (cell)) {
@@ -579,7 +577,7 @@
       view->button = 0;
     }
 
-    grr_board_view_pointer_coords_to_grid (view, event->x, event->y, &x, &y);
+    grr_board_view_transform_pixel_to_cell (view, event->x, event->y, &x, &y);
 
     rr_board_find_robot (view->board, view->active_robot, &robot_x, &robot_y);
     dx = x - robot_x;
@@ -655,12 +653,11 @@
     /* XXX: Perhaps should draw a robot here */
 }
 
-static void
-grr_board_view_update (grr_board_view_t *view)
+void
+grr_board_view_mark_damage (grr_board_view_t *view, int i, int j)
 {
-    g_return_if_fail (view != NULL);
-    g_return_if_fail (GRR_IS_BOARD_VIEW (view));
+    int x, y;
 
-    gtk_widget_queue_draw (GTK_WIDGET (view));
+    grr_board_view_transform_cell_to_pixel (view, i, j, &x, &y);
+    gtk_widget_queue_draw_area (GTK_WIDGET (view), x, y, view->cell_width, view->cell_height);
 }
-

Index: grr_board_view.h
===================================================================
RCS file: /local/src/CVS/grrobot/src/grr_board_view.h,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- grr_board_view.h	11 Jul 2003 01:11:41 -0000	1.8
+++ grr_board_view.h	23 Jul 2003 21:34:34 -0000	1.9
@@ -56,6 +56,8 @@
     int board_width, board_height;
     int board_pad_x, board_pad_y;
 
+    int **damaged;
+
     int cell_width, cell_height;
 
     rr_robot_t active_robot;
@@ -97,6 +99,14 @@
 
 void
 grr_board_view_undo (grr_board_view_t *view);
+
+void
+grr_board_view_mark_damage (grr_board_view_t *view, int i, int j);
+
+void
+grr_board_view_transform_pixel_to_cell (grr_board_view_t *view,
+					int pixel_x, int pixel_y,
+					int *cell_x, int *cell_y);
 
 #ifdef __cplusplus
 }

Index: grr_icon.c
===================================================================
RCS file: /local/src/CVS/grrobot/src/grr_icon.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- grr_icon.c	25 Jun 2003 10:52:59 -0000	1.2
+++ grr_icon.c	23 Jul 2003 21:34:34 -0000	1.3
@@ -129,6 +129,10 @@
     char *file, *buf;
     int buflen;
 
+    icon->surface = NULL;
+    icon->surface_width = 0;
+    icon->surface_height = 0;
+
     status = xsvg_create (&icon->xsvg);
     if (status)
 	return RR_STATUS_NO_MEMORY;
@@ -151,9 +155,6 @@
 	fprintf (stderr, "Error parsing built-in SVG icon for: %s\n", name);
 	return RR_STATUS_PARSE_ERROR;
     }
-
-    icon->surface = NULL;
-
     return RR_STATUS_SUCCESS;
 }
 
@@ -220,7 +221,7 @@
 }
 
 void
-grr_icon_draw (grr_icon_t *icon, XrState *xrs)
+grr_icon_draw (grr_icon_t *icon, cairo_t *xrs)
 {
     xsvg_status_t status;
 
@@ -232,27 +233,27 @@
 }
 
 void
-grr_icon_predraw (grr_icon_t *icon, XrState *xrs, int width, int height)
+grr_icon_predraw (grr_icon_t *icon, cairo_t *xrs, int width, int height)
 {
     if (icon->surface_width != width || icon->surface_height != height) {
 	if (icon->surface)
-	    XrSurfaceDestroy (icon->surface);
+	    cairo_surface_destroy (icon->surface);
 	icon->surface_width = width;
 	icon->surface_height = height;
-	icon->surface = XrSurfaceCreateNextTo (XrGetTargetSurface (xrs),
-					       XrFormatARGB32,
-					       width, height);
+	icon->surface = cairo_surface_create_similar (cairo_get_target_surface (xrs),
+						      CAIRO_FORMAT_ARGB32,
+						      width, height);
     }
 
-    XrSave (xrs);
-    XrSetTargetSurface (xrs, icon->surface);
+    cairo_save (xrs);
+    cairo_set_target_surface (xrs, icon->surface);
     grr_icon_draw (icon, xrs);
-    XrRestore (xrs);
+    cairo_restore (xrs);
 }
 
 void
-grr_icon_draw_predrawn (grr_icon_t *icon, XrState *xrs)
+grr_icon_draw_predrawn (grr_icon_t *icon, cairo_t *xrs)
 {
     if (icon->surface)
-	XrShowSurface (xrs, icon->surface, icon->surface_width, icon->surface_height);
+	cairo_show_surface (xrs, icon->surface, icon->surface_width, icon->surface_height);
 }

Index: grr_icon.h
===================================================================
RCS file: /local/src/CVS/grrobot/src/grr_icon.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- grr_icon.h	25 Jun 2003 10:52:59 -0000	1.2
+++ grr_icon.h	23 Jul 2003 21:34:34 -0000	1.3
@@ -33,7 +33,7 @@
 typedef struct grr_icon {
     xsvg_t *xsvg;
 
-    XrSurface *surface;
+    cairo_surface_t *surface;
     int surface_width;
     int surface_height;
 } grr_icon_t;
@@ -45,13 +45,13 @@
 grr_icon_destroy (grr_icon_t *icon);
 
 void
-grr_icon_draw (grr_icon_t *icon, XrState *xrs);
+grr_icon_draw (grr_icon_t *icon, cairo_t *xrs);
 
 void
-grr_icon_predraw (grr_icon_t *icon, XrState *xrs, int width, int height);
+grr_icon_predraw (grr_icon_t *icon, cairo_t *xrs, int width, int height);
 
 void
-grr_icon_draw_predrawn (grr_icon_t *icon, XrState *xrs);
+grr_icon_draw_predrawn (grr_icon_t *icon, cairo_t *xrs);
 
 char *
 grr_icon_robot_name (rr_robot_t robot);

Index: grrobot.c
===================================================================
RCS file: /local/src/CVS/grrobot/src/grrobot.c,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -d -r1.14 -r1.15
--- grrobot.c	12 Jul 2003 01:53:37 -0000	1.14
+++ grrobot.c	23 Jul 2003 21:34:34 -0000	1.15
@@ -1,4 +1,4 @@
-/* grrobot - Ricochet Robot using GTK+ and Xr
+/* grrobot - Ricochet Robot using GTK+, libxsvg, and Cairo
  *
  * Copyright © 2003 Carl Worth
  *
@@ -106,6 +106,8 @@
 	}
 	game.client = NULL;
     } else {
+	GPollFD *poll_fd;
+
 	game.client = rr_client_create (args.host, args.port, args.user);
 	if (game.client == NULL) {
 	    fprintf (stderr, "Failed connecting to %s:%s as %s\n",
@@ -131,6 +133,11 @@
 	game.last_move_robot = RR_ROBOT_NONE;
 
 	source = grr_game_notices_source_new (&game);
+	poll_fd = g_new (GPollFD, 1);
+	poll_fd->fd = rr_client_fd (game.client);
+	poll_fd->events = G_IO_IN;
+	g_source_add_poll (source, poll_fd);
+
 	g_source_set_priority (source, GDK_PRIORITY_EVENTS);
 	g_source_attach (source, NULL);
 	g_source_unref (source);
@@ -332,7 +339,7 @@
 	    break;
 	case RR_NOTICE_ACTIVE:
 	    grr_game_printf (game, "\nUser %s now active to demonstrate solution in %d moves.",
-			       notice->u.bid.username,
+			     notice->u.bid.username,
 			     notice->u.bid.number);
 	    break;
 	case RR_NOTICE_TIMER:
@@ -340,10 +347,16 @@
 			     notice->u.number);
 	    break;
 	case RR_NOTICE_POSITION:
+	{
+	    int x, y;
+	    rr_board_find_robot (board, notice->u.position.robot, &x, &y);
 	    rr_board_add_robot (board, notice->u.position.robot,
 				notice->u.position.x, notice->u.position.y);
-	    gtk_widget_queue_draw (GTK_WIDGET (game->window));
-	    break;
+	    grr_board_view_mark_damage (game->board_view, x, y);
+	    grr_board_view_mark_damage (game->board_view,
+					notice->u.position.x, notice->u.position.y);
+	}
+	break;
 	default:
 	    fprintf (stderr, "Unknown notice: %d\n", notice->type);
 	    break;




More information about the Commit mailing list