2 * Copyright (C) 2009 Carl Worth
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see http://www.gnu.org/licenses/ .
17 * Author: Carl Worth <cworth@cworth.org>
20 #define _GNU_SOURCE /* for vasprintf */
27 #include "dvonn-board.h"
29 #define BOARD_X_SIZE 11
30 #define BOARD_Y_SIZE 5
40 typedef struct _dvonn_game dvonn_game_t;
53 dvonn_bool_t has_selected;
57 PangoFontDescription *font;
61 on_delete_event_quit (GtkWidget *widget,
67 /* Returning FALSE allows the default handler for delete-event
68 * to proceed to cleanup the widget. */
72 /* Something like buff */
73 #define BACKGROUND_COLOR 0.89, 0.70, 0.40
75 #define RED_RING_COLOR 0.8, 0.2, 0.2
77 /* Relative to a unit square. */
78 #define RING_OUTER_RADIUS 0.4
79 #define RING_INNER_RADIUS 0.2
80 #define FONT_SIZE (RING_INNER_RADIUS * 1.5)
82 /* XXX: This really should have an interest rectangle. */
84 dvonn_game_update_windows (dvonn_game_t *game)
88 for (i = 0; i < game->num_views; i++)
89 gtk_widget_queue_draw (game->views[i]->window);
92 /* Convert from a board index to a device-pixel coordinate pair, (at
93 * the center of the ring). */
95 layout_board_to_device (layout_t *layout, double *x_ret, double *y_ret)
97 double x = *x_ret, y = *y_ret;
99 *x_ret = layout->x_offset + layout->cell_size *
100 (x + (y - DVONN_BOARD_Y_SIZE/2)/2.0 + 0.5);
101 *y_ret = layout->y_offset + layout->cell_size * (M_SQRT1_2 * y + 0.5);
104 /* Convert from a device-pixel coordinate pair to a board index. */
106 layout_device_to_board (layout_t *layout, int *x_ret, int *y_ret)
108 int x = *x_ret, y = *y_ret;
110 double x1_dev, y1_dev, x2_dev, y2_dev;
111 double dx, dy, d1, d2;
113 /* Because the vertical spacing between adjacent rows is less than
114 * layout->cell_size, the simple calculations here give us two
115 * candidate (x,y) pairs. We then choose the correct one based on
116 * a distance calculation.
118 y1 = (y - layout->y_offset) / (layout->cell_size * M_SQRT1_2);
119 x1 = (double) (x - layout->x_offset) / layout->cell_size - (y1 - DVONN_BOARD_Y_SIZE/2)/2.0;
122 x2 = (double) (x - layout->x_offset) / layout->cell_size - (y2 - DVONN_BOARD_Y_SIZE/2)/2.0;
126 layout_board_to_device (layout, &x1_dev, &y1_dev);
130 layout_board_to_device (layout, &x2_dev, &y2_dev);
134 d1 = sqrt (dx*dx + dy*dy);
138 d2 = sqrt (dx*dx + dy*dy);
150 on_button_press_event (GtkWidget *widget,
151 GdkEventButton *event,
154 view_t *view = user_data;
155 layout_t *layout = &view->layout;
156 dvonn_game_t *game = view->game;
160 /* Ignore double and triple clicks. */
161 if (event->type >= GDK_2BUTTON_PRESS)
164 /* Right-click means pass, (yes, it would be better to add some
165 * actual UI elements... */
166 if (event->button == 3) {
167 dvonn_board_pass (&game->board);
173 layout_device_to_board (layout, &x, &y);
175 /* Do nothing for out-of-bounds clicks */
176 if (x < 0 || x >= DVONN_BOARD_X_SIZE ||
177 y < 0 || y >= DVONN_BOARD_Y_SIZE)
182 /* Nor for cells which have array entries that are invalid. */
183 if (game->board.cells[x][y].type == DVONN_CELL_INVALID)
186 if (game->board.phase == DVONN_PHASE_PLACEMENT) {
187 if (dvonn_board_place (&game->board, x, y, &error))
188 dvonn_game_update_windows (game);
190 printf ("Illegal placement %c%d: %s\n",
198 if (! game->has_selected) {
199 if (game->board.cells[x][y].type == game->board.player &&
200 ! dvonn_board_cell_surrounded (&game->board, x, y))
202 game->has_selected = TRUE;
203 game->selected_x = x;
204 game->selected_y = y;
205 dvonn_game_update_windows (game);
210 if (x == game->selected_x && y == game->selected_y)
212 game->has_selected = FALSE;
213 dvonn_game_update_windows (game);
217 if (dvonn_board_move (&game->board,
218 game->selected_x, game->selected_y,
221 game->has_selected = FALSE;
222 dvonn_game_update_windows (game);
225 printf ("Illegal move %c%d%c%d: %s\n",
226 'a' + game->selected_x,
227 game->selected_y + 1,
236 /* Add a unit-sized DVONN-ring path to cr, from (0,0) to (1,1). */
238 ring_path (cairo_t *cr)
240 cairo_new_sub_path (cr);
241 cairo_arc (cr, 0.5, 0.5, RING_OUTER_RADIUS, 0, 2 * M_PI);
242 cairo_arc_negative (cr, 0.5, 0.5, RING_INNER_RADIUS, 2 * M_PI, 0);
245 /* Some helper functions for using pango. */
247 _create_layout (cairo_t *cr, PangoFontDescription *font, const char *text)
251 layout = pango_cairo_create_layout (cr);
252 pango_layout_set_font_description (layout, font);
253 pango_layout_set_text (layout, text, -1);
254 pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
259 #define PRINTF_FORMAT(fmt_index, va_index) __attribute__ ((__format__(__printf__, fmt_index, va_index)))
262 _create_layout_vprintf (cairo_t *cr, PangoFontDescription *font, const char *fmt, va_list ap)
267 vasprintf (&text, fmt, ap);
269 layout = _create_layout (cr, font, text);
277 _create_layout_printf (cairo_t *cr, PangoFontDescription *font, const char *fmt, ...)
278 PRINTF_FORMAT (3, 4);
281 _create_layout_printf (cairo_t *cr, PangoFontDescription *font, const char *fmt, ...)
288 layout = _create_layout_vprintf (cr, font, fmt, ap);
296 _destroy_layout (PangoLayout *layout)
298 g_object_unref (layout);
302 _show_layout (cairo_t *cr, PangoLayout *layout)
304 pango_cairo_show_layout (cr, layout);
306 _destroy_layout (layout);
310 on_expose_event_draw (GtkWidget *widget,
311 GdkEventExpose *event,
314 view_t *view = user_data;
315 layout_t *layout = &view->layout;
316 dvonn_game_t *game = view->game;
320 if (layout->width != widget->allocation.width ||
321 layout->height != widget->allocation.height)
325 layout->width = widget->allocation.width;
326 layout->height = widget->allocation.height;
328 x_size = layout->width;
329 if (x_size > layout->height * BOARD_X_SIZE / (1 + M_SQRT1_2 * (BOARD_Y_SIZE-1)))
330 x_size = layout->height * BOARD_X_SIZE / (1 + M_SQRT1_2 * (BOARD_Y_SIZE-1));
332 /* Size must be a multiple of the integer cell_size */
333 layout->cell_size = x_size / BOARD_X_SIZE;
334 x_size = layout->cell_size * BOARD_X_SIZE;
335 y_size = layout->cell_size * (1 + M_SQRT1_2 * (BOARD_Y_SIZE-1));
337 layout->x_offset = (layout->width - x_size) / 2;
338 layout->y_offset = (layout->height - y_size) / 2;
340 if (game->font == NULL) {
341 game->font = pango_font_description_new ();
342 pango_font_description_set_family (game->font, "sans");
344 pango_font_description_set_absolute_size (game->font,
345 FONT_SIZE * PANGO_SCALE);
348 cr = gdk_cairo_create (widget->window);
350 cairo_set_source_rgb (cr, BACKGROUND_COLOR);
353 cairo_translate (cr, layout->x_offset, layout->y_offset);
354 cairo_scale (cr, layout->cell_size, layout->cell_size);
356 for (y = 0; y < BOARD_Y_SIZE; y++) {
357 for (x = 0; x < BOARD_X_SIZE; x++) {
360 cell = game->board.cells[x][y];
361 if (cell.type == DVONN_CELL_INVALID)
366 x + (y - DVONN_BOARD_Y_SIZE/2) / 2.0,
368 if (cell.contains_red && cell.type != DVONN_CELL_RED) {
369 cairo_new_sub_path (cr);
370 cairo_arc (cr, 0.5, 0.5,
371 (RING_INNER_RADIUS + RING_OUTER_RADIUS)/2.0,
373 cairo_set_source_rgb (cr, RED_RING_COLOR);
378 case DVONN_CELL_WHITE:
379 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* white */
381 case DVONN_CELL_BLACK:
382 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
385 cairo_set_source_rgb (cr, RED_RING_COLOR);
387 case DVONN_CELL_EMPTY:
389 cairo_set_source_rgba (cr, 0.0, 0.0, 0.2, 0.1);
392 if (game->has_selected &&
393 x == game->selected_x &&
394 y == game->selected_y)
396 cairo_fill_preserve (cr);
397 cairo_set_source_rgba (cr, 0.2, 0.2, 1.0, 0.4);
401 if (game->board.cells[x][y].height > 1) {
404 0.5 - 0.7 * RING_INNER_RADIUS * cos (M_PI_4),
405 0.5 - 1.2 * RING_INNER_RADIUS * sin (M_PI_4));
406 height = _create_layout_printf (cr, game->font, "%d",
407 game->board.cells[x][y].height);
408 _show_layout (cr, height);
421 dvonn_game_init (dvonn_game_t *game)
426 game->has_selected = FALSE;
428 dvonn_board_init (&game->board);
434 view_init (view_t *view, dvonn_game_t *game, GtkWidget *window)
437 view->window = window;
439 view->layout.width = 0;
440 view->layout.height = 0;
444 dvonn_game_create_view (dvonn_game_t *game)
448 GtkWidget *drawing_area;
450 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
452 view = malloc (sizeof (view_t));
454 fprintf (stderr, "Out of memory.\n");
459 game->views = realloc (game->views,
460 game->num_views * sizeof (view_t *));
461 if (game->views == NULL) {
462 fprintf (stderr, "Out of memory.\n");
465 game->views[game->num_views - 1] = view;
467 view_init (view, game, window);
469 gtk_window_set_default_size (GTK_WINDOW (window), 780, 251);
471 g_signal_connect (window, "delete-event",
472 G_CALLBACK (on_delete_event_quit), NULL);
474 drawing_area = gtk_drawing_area_new ();
476 gtk_container_add (GTK_CONTAINER (window), drawing_area);
478 g_signal_connect (drawing_area, "expose-event",
479 G_CALLBACK (on_expose_event_draw), view);
481 gtk_widget_add_events (drawing_area, GDK_BUTTON_PRESS_MASK);
482 g_signal_connect (drawing_area, "button-press-event",
483 G_CALLBACK (on_button_press_event), view);
485 gtk_widget_show_all (window);
491 main (int argc, char *argv[])
498 dvonn_game_init (&game);
500 gtk_init (&argc, &argv);
502 /* Create a view for player 1. */
503 dvonn_game_create_view (&game);
505 /* If Keith has connected to my machine, create a view for him as
507 setenv ("XAUTHORITY", "/home/keithp/.Xauthority", 1);
508 display = gdk_display_open ("localhost:10.0");
510 screen = gdk_display_get_default_screen (display);
511 window = dvonn_game_create_view (&game);
512 gtk_window_set_screen (GTK_WINDOW (window), screen);