From 2c8c385d00f60fad7a4b74b0b66516fbce62c50f Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Thu, 5 Mar 2009 01:37:25 -0800 Subject: [PATCH] Add display of stack height and implement movement There's still no checking for legal moves on the movement, but at least the stack height increases appropriately. --- dvonn-board.c | 8 +++- dvonn.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 104 insertions(+), 7 deletions(-) diff --git a/dvonn-board.c b/dvonn-board.c index 795ff55..f802775 100644 --- a/dvonn-board.c +++ b/dvonn-board.c @@ -147,6 +147,8 @@ dvonn_board_place (dvonn_board_t *board, else board->cells[x][y].type = DVONN_CELL_WHITE; + board->cells[x][y].height = 1; + board->moves++; if (board->moves == 49) { @@ -171,7 +173,11 @@ dvonn_board_move (dvonn_board_t *board, if (! dvonn_board_move_legal (board, x1, y1, x2, y2, error)) return FALSE; - /* XXX: Need to execute the move here. */ + board->cells[x2][y2].height += board->cells[x1][y1].height; + board->cells[x2][y2].type = board->cells[x1][y1].type; + + board->cells[x1][y1].type = DVONN_CELL_EMPTY; + board->cells[x1][y1].height = 0; dvonn_board_next_player (board); diff --git a/dvonn.c b/dvonn.c index ebea5e6..1442923 100644 --- a/dvonn.c +++ b/dvonn.c @@ -17,7 +17,10 @@ * Author: Carl Worth */ +#define _GNU_SOURCE /* for vasprintf */ +#include #include +#include #include #include @@ -50,6 +53,8 @@ struct _dvonn_game { dvonn_bool_t has_selected; int selected_x; int selected_y; + + PangoFontDescription *font; }; static gboolean @@ -66,9 +71,11 @@ on_delete_event_quit (GtkWidget *widget, /* Something like buff */ #define BACKGROUND_COLOR 0.89, 0.70, 0.40 -#define LIGHT_SQUARE_COLOR 0.89, 0.70, 0.40 -/* Something like mahogany */ -#define DARK_SQUARE_COLOR 0.26, 0.02, 0.01 + +/* Relative to a unit square. */ +#define RING_OUTER_RADIUS 0.4 +#define RING_INNER_RADIUS 0.2 +#define FONT_SIZE (RING_INNER_RADIUS * 1.5) /* XXX: This really should have an interest rectangle. */ static void @@ -215,8 +222,73 @@ on_button_press_event (GtkWidget *widget, static void ring_path (cairo_t *cr) { - cairo_arc (cr, 0.5, 0.5, 0.4, 0, 2 * M_PI); - cairo_arc_negative (cr, 0.5, 0.5, 0.2, 2 * M_PI, 0); + cairo_new_sub_path (cr); + cairo_arc (cr, 0.5, 0.5, RING_OUTER_RADIUS, 0, 2 * M_PI); + cairo_arc_negative (cr, 0.5, 0.5, RING_INNER_RADIUS, 2 * M_PI, 0); +} + +/* Some helper functions for using pango. */ +static PangoLayout * +_create_layout (cairo_t *cr, PangoFontDescription *font, const char *text) +{ + PangoLayout *layout; + + layout = pango_cairo_create_layout (cr); + pango_layout_set_font_description (layout, font); + pango_layout_set_text (layout, text, -1); + pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER); + + return layout; +} + +#define PRINTF_FORMAT(fmt_index, va_index) __attribute__ ((__format__(__printf__, fmt_index, va_index))) + +static PangoLayout * +_create_layout_vprintf (cairo_t *cr, PangoFontDescription *font, const char *fmt, va_list ap) +{ + PangoLayout *layout; + char *text; + + vasprintf (&text, fmt, ap); + + layout = _create_layout (cr, font, text); + + free (text); + + return layout; +} + +static PangoLayout * +_create_layout_printf (cairo_t *cr, PangoFontDescription *font, const char *fmt, ...) + PRINTF_FORMAT (3, 4); + +static PangoLayout * +_create_layout_printf (cairo_t *cr, PangoFontDescription *font, const char *fmt, ...) +{ + va_list ap; + PangoLayout *layout; + + va_start (ap, fmt); + + layout = _create_layout_vprintf (cr, font, fmt, ap); + + va_end (ap); + + return layout; +} + +static void +_destroy_layout (PangoLayout *layout) +{ + g_object_unref (layout); +} + +static void +_show_layout (cairo_t *cr, PangoLayout *layout) +{ + pango_cairo_show_layout (cr, layout); + + _destroy_layout (layout); } static gboolean @@ -249,10 +321,17 @@ on_expose_event_draw (GtkWidget *widget, layout->x_offset = (layout->width - x_size) / 2; layout->y_offset = (layout->height - y_size) / 2; + + if (game->font == NULL) { + game->font = pango_font_description_new (); + pango_font_description_set_family (game->font, "sans"); + } + pango_font_description_set_absolute_size (game->font, + FONT_SIZE * PANGO_SCALE); } cr = gdk_cairo_create (widget->window); - + cairo_set_source_rgb (cr, BACKGROUND_COLOR); cairo_paint (cr); @@ -296,6 +375,16 @@ on_expose_event_draw (GtkWidget *widget, } cairo_fill (cr); + if (game->board.cells[x][y].height) { + PangoLayout *height; + cairo_move_to (cr, + 0.5 - 0.8 * RING_INNER_RADIUS * cos (M_PI_4), + 0.5 - 1.2 * RING_INNER_RADIUS * sin (M_PI_4)); + height = _create_layout_printf (cr, game->font, "%d", + game->board.cells[x][y].height); + _show_layout (cr, height); + } + cairo_restore (cr); } } @@ -314,6 +403,8 @@ dvonn_game_init (dvonn_game_t *game) game->has_selected = FALSE; dvonn_board_init (&game->board); + + game->font = NULL; } static void -- 2.43.0