[cairo-commit] cairo-demo/tsetse ChangeLog, 1.2, 1.3 Makefile, 1.1, 1.2 tsetse.c, 1.3, 1.4

Carl Worth commit at pdx.freedesktop.org
Sun Mar 20 18:28:46 PST 2005


Committed by: cworth

Update of /cvs/cairo/cairo-demo/tsetse
In directory gabe:/tmp/cvs-serv17524

Modified Files:
	ChangeLog Makefile tsetse.c 
Log Message:

        * Makefile: Fix to not overwrite user's CFLAGS

        * tsetse.c: (check_selected_for_set), (draw_board), (win_refresh),
        (board_count_sets_possible), (board_init), (deal), (reshuffle),
        (win_init), (reshuffle_cb), (toggle_display_sets_possible_cb):
        Add support for displaying possible sets in current cards,
        (toggled with space bar). Also refuse to reshuffle, (and set
        display of possible sets on) if there are still possible sets.


Index: ChangeLog
===================================================================
RCS file: /cvs/cairo/cairo-demo/tsetse/ChangeLog,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- ChangeLog	19 Feb 2005 21:06:21 -0000	1.2
+++ ChangeLog	21 Mar 2005 02:28:44 -0000	1.3
@@ -1,3 +1,14 @@
+2005-03-20  Carl Worth  <cworth at cworth.org>
+
+	* Makefile: Fix to not overwrite user's CFLAGS
+	
+	* tsetse.c: (check_selected_for_set), (draw_board), (win_refresh),
+	(board_count_sets_possible), (board_init), (deal), (reshuffle),
+	(win_init), (reshuffle_cb), (toggle_display_sets_possible_cb):
+	Add support for displaying possible sets in current cards,
+	(toggled with space bar). Also refuse to reshuffle, (and set
+	display of possible sets on) if there are still possible sets.
+
 2005-02-19  Carl Worth  <cworth at cworth.org>
 
 	* tsetse.c (deal): Don't deal from a deck with no cards.

Index: Makefile
===================================================================
RCS file: /cvs/cairo/cairo-demo/tsetse/Makefile,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- Makefile	18 Feb 2005 16:54:59 -0000	1.1
+++ Makefile	21 Mar 2005 02:28:44 -0000	1.2
@@ -1,9 +1,10 @@
-CFLAGS+=-g -Wall -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls `pkg-config --cflags cairo`
-LDFLAGS+=`pkg-config --libs cairo`
+MYCFLAGS=-g -Wall -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls `pkg-config --cflags cairo`
+MYLDFLAGS=`pkg-config --libs cairo`
 
-all: tsetse
+tsetse: tsetse.c
+	$(CC) $(CFLAGS) $(CPPFLAGS) ${MYCFLAGS} ${MYLDFLAGS} $< -o $@
 
 clean:
-	rm -f $(PROGS) *.o
+	rm -f tsetse
 
 

Index: tsetse.c
===================================================================
RCS file: /cvs/cairo/cairo-demo/tsetse/tsetse.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- tsetse.c	19 Feb 2005 21:06:21 -0000	1.3
+++ tsetse.c	21 Mar 2005 02:28:44 -0000	1.4
@@ -66,6 +66,8 @@
     int num_slots;
     slot_t slots[BOARD_MAX_SLOTS];
     int needs_deal;
+    int sets_possible;
+    int display_sets_possible;
 } board_t;
 
 typedef struct win {
@@ -81,6 +83,7 @@
 
     int click;
     int active;
+    int display_sets_possible;
 
     deck_t deck;
     board_t board;
@@ -110,11 +113,16 @@
 
 static int new_game_cb(win_t *win);
 static int reshuffle_cb(win_t *win);
+static int toggle_display_sets_possible_cb(win_t *win);
 static int quit_cb(win_t *win);
 
+static void
+board_count_sets_possible (board_t *board);
+
 static const callback_doc_t callback_doc[] = {
     { new_game_cb,      "New game" },
     { reshuffle_cb,	"Return dealt cards to deck and reshuffle" },
+    { toggle_display_sets_possible_cb, "Toggle display of possible sets" },
     { quit_cb,		"Exit the program" }
 };
 
@@ -122,6 +130,7 @@
     /* Keysym, Alias, Keycode, callback */
     { "N",      0, 0, new_game_cb },
     { "R",      0, 0, reshuffle_cb },
+    { "space",  0, 0, toggle_display_sets_possible_cb },
     { "Q",	0, 0, quit_cb }
 };
 
@@ -352,6 +361,8 @@
 	slots[i]->has_card = 0;
     }
 
+    board_count_sets_possible (board);
+
     board->needs_deal = 1;
 
     return 1;
@@ -374,6 +385,8 @@
 {
     int i;
 
+    cairo_save (cr);
+
     cairo_scale (cr, width, height);
 
     for (i=0; i < board->num_slots; i++)
@@ -386,6 +399,8 @@
 	    draw_card (cr, &board->slots[i].card, board->slots[i].selected);
 	    cairo_restore (cr);
 	}
+
+    cairo_restore (cr);
 }
 
 static void
@@ -406,6 +421,29 @@
 
     draw_board (cr, &win->board, win->width, win->height);
 
+    if (win->display_sets_possible) {
+	char sets_possible[3];
+	cairo_text_extents_t extents;
+
+	snprintf (sets_possible, 3, "%d", win->board.sets_possible);
+	sets_possible[2] = '\0';
+	
+	cairo_save (cr);
+	{
+	    cairo_select_font (cr, "sans", 0, 0);
+	    cairo_scale_font (cr, win->height / 1.2);
+	    cairo_move_to (cr, 0, 0);
+	    cairo_text_extents (cr, sets_possible, &extents);
+	    cairo_move_to (cr,
+			   win->width/2 - (extents.x_bearing + extents.width/2),
+			   win->height/2 - (extents.y_bearing + extents.height/2));
+	    cairo_set_rgb_color (cr, 0, 0, .5);
+	    cairo_set_alpha (cr, .75);
+	    cairo_show_text (cr, sets_possible);
+	}
+	cairo_restore (cr);
+    }
+
     status = cairo_status(cr);
     if (status) {
 	fprintf(stderr, "Cairo is unhappy: %s\n", cairo_status_string(cr));
@@ -462,6 +500,32 @@
 }
 
 static void
+board_count_sets_possible (board_t *board)
+{
+    int i, j, k;
+    int sets_possible = 0;
+
+    for (i = 0; i < board->num_slots; i++) {
+	if (! board->slots[i].has_card)
+	    continue;
+	for (j = i+1; j < board->num_slots; j++) {
+	    if (! board->slots[j].has_card)
+		continue;
+	    for (k = j+1; k < board->num_slots; k++) {
+		if (! board->slots[k].has_card)
+		    continue;
+		if (is_set (&board->slots[i].card,
+			    &board->slots[j].card,
+			    &board->slots[k].card))
+		    sets_possible++;
+	    }
+	}
+    }
+
+    board->sets_possible = sets_possible;
+}
+
+static void
 board_init (board_t *board)
 {
     int i;
@@ -471,6 +535,8 @@
 	board->slots[i].selected = 0;
     }
     board->needs_deal = 0;
+
+    board_count_sets_possible (board);
 }
 
 static void
@@ -486,6 +552,8 @@
 	    }
 	}
 
+    board_count_sets_possible (board);
+
     board->needs_deal = 0;
 }
 
@@ -499,11 +567,15 @@
 }
 
 /* Return the dealt cards to the deck, reshuffle, and deal again. */
-static void
+static int
 reshuffle (deck_t *deck, board_t *board)
 {
     int i;
 
+    if (board->sets_possible) {
+	return 0;
+    }
+
     for (i=0; i < board->num_slots; i++) {
 	if (board->slots[i].has_card) {
 	    deck->cards[++deck->num_cards - 1] = board->slots[i].card;
@@ -514,6 +586,8 @@
 
     deck_shuffle (deck);
     deal (deck, board);
+
+    return 1;
 }
 
 static void
@@ -551,8 +625,8 @@
     }
 
     win->active = 0;
-
     win->click = 0;
+    win->display_sets_possible = 0;
 
     win_refresh(win);
     win->needs_refresh = 0;
@@ -734,7 +808,21 @@
 static int
 reshuffle_cb (win_t *win)
 {
-    reshuffle (&win->deck, &win->board);
+    if (reshuffle (&win->deck, &win->board))
+	win->display_sets_possible = 0;
+    else
+	win->display_sets_possible = 1;
+
+    win->needs_refresh = 1;
+
+    return 0;
+}
+
+static int
+toggle_display_sets_possible_cb (win_t *win)
+{
+    win->display_sets_possible = !win->display_sets_possible;
+
     win->needs_refresh = 1;
 
     return 0;




More information about the cairo-commit mailing list