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;
58 PangoFontDescription *ring_font;
60 dvonn_bool_t dual_window_mode;
61 GtkWidget *windows[2];
65 on_delete_event_quit (GtkWidget *widget,
71 /* Returning FALSE allows the default handler for delete-event
72 * to proceed to cleanup the widget. */
76 /* Something like buff */
77 #define BACKGROUND_COLOR 0.89, 0.70, 0.40
79 #define RED_RING_COLOR 0.8, 0.2, 0.2
81 #define DVONN_FONT "sans"
82 #define DVONN_FONT_SIZE 12
84 /* Relative to a unit square. */
85 #define RING_OUTER_RADIUS 0.4
86 #define RING_INNER_RADIUS 0.2
87 #define RING_FONT_SIZE (RING_INNER_RADIUS * 1.5)
89 /* XXX: This really should have an interest rectangle. */
91 dvonn_game_update_windows (dvonn_game_t *game)
95 for (i = 0; i < game->num_views; i++)
96 gtk_widget_queue_draw (game->views[i]->window);
99 /* Convert from a board index to a device-pixel coordinate pair, (at
100 * the center of the ring). */
102 layout_board_to_device (layout_t *layout, double *x_ret, double *y_ret)
104 double x = *x_ret, y = *y_ret;
106 *x_ret = layout->x_offset + layout->cell_size *
107 (x + (y - DVONN_BOARD_Y_SIZE/2)/2.0 + 0.5);
108 *y_ret = layout->y_offset + layout->cell_size * (M_SQRT1_2 * y + 0.5);
111 /* Convert from a device-pixel coordinate pair to a board index. */
113 layout_device_to_board (layout_t *layout, int *x_ret, int *y_ret)
115 int x = *x_ret, y = *y_ret;
117 double x1_dev, y1_dev, x2_dev, y2_dev;
118 double dx, dy, d1, d2;
120 /* Because the vertical spacing between adjacent rows is less than
121 * layout->cell_size, the simple calculations here give us two
122 * candidate (x,y) pairs. We then choose the correct one based on
123 * a distance calculation.
125 y1 = (y - layout->y_offset) / (layout->cell_size * M_SQRT1_2);
126 x1 = (double) (x - layout->x_offset) / layout->cell_size - (y1 - DVONN_BOARD_Y_SIZE/2)/2.0;
129 x2 = (double) (x - layout->x_offset) / layout->cell_size - (y2 - DVONN_BOARD_Y_SIZE/2)/2.0;
133 layout_board_to_device (layout, &x1_dev, &y1_dev);
137 layout_board_to_device (layout, &x2_dev, &y2_dev);
141 d1 = sqrt (dx*dx + dy*dy);
145 d2 = sqrt (dx*dx + dy*dy);
157 on_button_press_event (GtkWidget *widget,
158 GdkEventButton *event,
161 view_t *view = user_data;
162 layout_t *layout = &view->layout;
163 dvonn_game_t *game = view->game;
167 /* Ignore events from the non-player. (Obviously when we add more
168 * interaction abilities, we will want to allow those even for the
169 * non-player---things like quit, etc.). */
170 if (game->dual_window_mode &&
171 widget->parent != game->windows[game->board.player])
176 /* Ignore double and triple clicks. */
177 if (event->type >= GDK_2BUTTON_PRESS)
180 /* Right-click means pass, (yes, it would be better to add some
181 * actual UI elements... */
182 if (event->button == 3) {
183 dvonn_board_pass (&game->board);
189 layout_device_to_board (layout, &x, &y);
191 /* Do nothing for out-of-bounds clicks */
192 if (x < 0 || x >= DVONN_BOARD_X_SIZE ||
193 y < 0 || y >= DVONN_BOARD_Y_SIZE)
198 /* Nor for cells which have array entries that are invalid. */
199 if (game->board.cells[x][y].type == DVONN_CELL_INVALID)
202 if (game->board.phase == DVONN_PHASE_PLACEMENT) {
203 if (dvonn_board_place (&game->board, x, y, &error))
204 dvonn_game_update_windows (game);
206 printf ("Illegal placement %c%d: %s\n",
214 if (! game->has_selected) {
215 if (dvonn_board_cell_owned_by (&game->board, x, y, game->board.player) &&
216 ! dvonn_board_cell_surrounded (&game->board, x, y))
218 game->has_selected = TRUE;
219 game->selected_x = x;
220 game->selected_y = y;
221 dvonn_game_update_windows (game);
226 if (x == game->selected_x && y == game->selected_y)
228 game->has_selected = FALSE;
229 dvonn_game_update_windows (game);
233 if (dvonn_board_move (&game->board,
234 game->selected_x, game->selected_y,
237 game->has_selected = FALSE;
238 dvonn_game_update_windows (game);
241 printf ("Illegal move %c%d%c%d: %s\n",
242 'a' + game->selected_x,
243 game->selected_y + 1,
252 /* Add a unit-sized DVONN-ring path to cr, from (0,0) to (1,1). */
254 ring_path (cairo_t *cr)
256 cairo_new_sub_path (cr);
257 cairo_arc (cr, 0.5, 0.5, RING_OUTER_RADIUS, 0, 2 * M_PI);
258 cairo_arc_negative (cr, 0.5, 0.5, RING_INNER_RADIUS, 2 * M_PI, 0);
261 /* Some helper functions for using pango. */
263 _create_layout (cairo_t *cr, PangoFontDescription *font, const char *text)
267 layout = pango_cairo_create_layout (cr);
268 pango_layout_set_font_description (layout, font);
269 pango_layout_set_text (layout, text, -1);
270 pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
275 #define PRINTF_FORMAT(fmt_index, va_index) __attribute__ ((__format__(__printf__, fmt_index, va_index)))
278 _create_layout_vprintf (cairo_t *cr, PangoFontDescription *font, const char *fmt, va_list ap)
283 vasprintf (&text, fmt, ap);
285 layout = _create_layout (cr, font, text);
293 _create_layout_printf (cairo_t *cr, PangoFontDescription *font, const char *fmt, ...)
294 PRINTF_FORMAT (3, 4);
297 _create_layout_printf (cairo_t *cr, PangoFontDescription *font, const char *fmt, ...)
304 layout = _create_layout_vprintf (cr, font, fmt, ap);
312 _destroy_layout (PangoLayout *layout)
314 g_object_unref (layout);
318 _show_layout (cairo_t *cr, PangoLayout *layout)
320 pango_cairo_show_layout (cr, layout);
322 _destroy_layout (layout);
326 on_expose_event_draw (GtkWidget *widget,
327 GdkEventExpose *event,
330 view_t *view = user_data;
331 layout_t *layout = &view->layout;
332 dvonn_game_t *game = view->game;
335 PangoLayout *to_move;
337 if (layout->width != widget->allocation.width ||
338 layout->height != widget->allocation.height)
342 layout->width = widget->allocation.width;
343 layout->height = widget->allocation.height;
345 x_size = layout->width;
346 if (x_size > layout->height * BOARD_X_SIZE / (1 + M_SQRT1_2 * (BOARD_Y_SIZE-1)))
347 x_size = layout->height * BOARD_X_SIZE / (1 + M_SQRT1_2 * (BOARD_Y_SIZE-1));
349 /* Size must be a multiple of the integer cell_size */
350 layout->cell_size = x_size / BOARD_X_SIZE;
351 x_size = layout->cell_size * BOARD_X_SIZE;
352 y_size = layout->cell_size * (1 + M_SQRT1_2 * (BOARD_Y_SIZE-1));
354 layout->x_offset = (layout->width - x_size) / 2;
355 layout->y_offset = (layout->height - y_size) / 2;
357 if (game->ring_font == NULL) {
358 game->ring_font = pango_font_description_new ();
359 pango_font_description_set_family (game->ring_font,
362 pango_font_description_set_absolute_size (game->ring_font,
363 RING_FONT_SIZE * PANGO_SCALE);
366 cr = gdk_cairo_create (widget->window);
368 cairo_set_source_rgb (cr, BACKGROUND_COLOR);
371 if (game->font == NULL) {
372 game->font = pango_font_description_new ();
373 pango_font_description_set_family (game->font, DVONN_FONT);
374 pango_font_description_set_absolute_size (game->font, DVONN_FONT_SIZE * PANGO_SCALE);
376 to_move = _create_layout_printf (cr, game->font,
378 game->board.player == DVONN_PLAYER_WHITE ?
380 game->board.phase == DVONN_PHASE_PLACEMENT ?
382 cairo_move_to (cr, 2, 2);
383 if (game->board.player == DVONN_PLAYER_WHITE)
384 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
386 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
387 _show_layout (cr, to_move);
389 cairo_translate (cr, layout->x_offset, layout->y_offset);
390 cairo_scale (cr, layout->cell_size, layout->cell_size);
392 for (y = 0; y < BOARD_Y_SIZE; y++) {
393 for (x = 0; x < BOARD_X_SIZE; x++) {
396 cell = game->board.cells[x][y];
397 if (cell.type == DVONN_CELL_INVALID)
402 x + (y - DVONN_BOARD_Y_SIZE/2) / 2.0,
404 if (cell.contains_red && cell.type != DVONN_CELL_RED) {
405 cairo_new_sub_path (cr);
406 cairo_arc (cr, 0.5, 0.5,
407 (RING_INNER_RADIUS + RING_OUTER_RADIUS)/2.0,
409 cairo_set_source_rgb (cr, RED_RING_COLOR);
414 case DVONN_CELL_WHITE:
415 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* white */
417 case DVONN_CELL_BLACK:
418 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
421 cairo_set_source_rgb (cr, RED_RING_COLOR);
423 case DVONN_CELL_EMPTY:
425 cairo_set_source_rgba (cr, 0.0, 0.0, 0.2, 0.1);
428 if (game->has_selected &&
429 x == game->selected_x &&
430 y == game->selected_y)
432 cairo_fill_preserve (cr);
433 cairo_set_source_rgba (cr, 0.2, 0.2, 1.0, 0.4);
437 if (game->board.cells[x][y].height > 1) {
440 0.5 - 0.7 * RING_INNER_RADIUS * cos (M_PI_4),
441 0.5 - 1.2 * RING_INNER_RADIUS * sin (M_PI_4));
442 height = _create_layout_printf (cr, game->ring_font, "%d",
443 game->board.cells[x][y].height);
444 _show_layout (cr, height);
457 dvonn_game_init (dvonn_game_t *game)
462 game->has_selected = FALSE;
464 dvonn_board_init (&game->board);
467 game->ring_font = NULL;
469 game->dual_window_mode = 0;
470 game->windows[0] = NULL;
471 game->windows[1] = NULL;
475 view_init (view_t *view, dvonn_game_t *game, GtkWidget *window)
478 view->window = window;
480 view->layout.width = 0;
481 view->layout.height = 0;
485 dvonn_game_create_view (dvonn_game_t *game)
489 GtkWidget *drawing_area;
491 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
493 view = malloc (sizeof (view_t));
495 fprintf (stderr, "Out of memory.\n");
500 game->views = realloc (game->views,
501 game->num_views * sizeof (view_t *));
502 if (game->views == NULL) {
503 fprintf (stderr, "Out of memory.\n");
506 game->views[game->num_views - 1] = view;
508 view_init (view, game, window);
510 gtk_window_set_default_size (GTK_WINDOW (window), 780, 251);
512 g_signal_connect (window, "delete-event",
513 G_CALLBACK (on_delete_event_quit), NULL);
515 drawing_area = gtk_drawing_area_new ();
517 gtk_container_add (GTK_CONTAINER (window), drawing_area);
519 g_signal_connect (drawing_area, "expose-event",
520 G_CALLBACK (on_expose_event_draw), view);
522 gtk_widget_add_events (drawing_area, GDK_BUTTON_PRESS_MASK);
523 g_signal_connect (drawing_area, "button-press-event",
524 G_CALLBACK (on_button_press_event), view);
526 gtk_widget_show_all (window);
532 main (int argc, char *argv[])
534 GtkWidget *window0, *window1;
539 dvonn_game_init (&game);
541 gtk_init (&argc, &argv);
543 /* Create a view for player 1. */
544 window0 = dvonn_game_create_view (&game);
546 /* Ugly little hack to get Xauthority data from keithp. Obviously
547 * won't work for any other user.
549 setenv ("XAUTHORITY", "/home/keithp/.Xauthority", 1);
552 /* Also ugly that localhost:10.0 is hard-coded, but this will work
553 * for many situations--both for when keithp attaches to my
554 * machine, and for when I connect to anyone's machine and then
555 * connect back, (assuming I haven't made other X forwardings
558 * Clearly we'll want some actual UI to select the right thing
561 display = gdk_display_open ("localhost:10.0");
563 screen = gdk_display_get_default_screen (display);
564 window1 = dvonn_game_create_view (&game);
565 gtk_window_set_screen (GTK_WINDOW (window1), screen);
567 game.dual_window_mode = 1;
568 game.windows[0] = window0;
569 game.windows[1] = window1;