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 /* Relative to a unit square. */
76 #define RING_OUTER_RADIUS 0.4
77 #define RING_INNER_RADIUS 0.2
78 #define FONT_SIZE (RING_INNER_RADIUS * 1.5)
80 /* XXX: This really should have an interest rectangle. */
82 dvonn_game_update_windows (dvonn_game_t *game)
86 for (i = 0; i < game->num_views; i++)
87 gtk_widget_queue_draw (game->views[i]->window);
90 /* Convert from a board index to a device-pixel coordinate pair, (at
91 * the center of the ring). */
93 layout_board_to_device (layout_t *layout, double *x_ret, double *y_ret)
95 double x = *x_ret, y = *y_ret;
97 *x_ret = layout->x_offset + layout->cell_size *
98 (x + (y - DVONN_BOARD_Y_SIZE/2)/2.0 + 0.5);
99 *y_ret = layout->y_offset + layout->cell_size * (M_SQRT1_2 * y + 0.5);
102 /* Convert from a device-pixel coordinate pair to a board index. */
104 layout_device_to_board (layout_t *layout, int *x_ret, int *y_ret)
106 int x = *x_ret, y = *y_ret;
108 double x1_dev, y1_dev, x2_dev, y2_dev;
109 double dx, dy, d1, d2;
111 /* Because the vertical spacing between adjacent rows is less than
112 * layout->cell_size, the simple calculations here give us two
113 * candidate (x,y) pairs. We then choose the correct one based on
114 * a distance calculation.
116 y1 = (y - layout->y_offset) / (layout->cell_size * M_SQRT1_2);
117 x1 = (double) (x - layout->x_offset) / layout->cell_size - (y1 - DVONN_BOARD_Y_SIZE/2)/2.0;
120 x2 = (double) (x - layout->x_offset) / layout->cell_size - (y2 - DVONN_BOARD_Y_SIZE/2)/2.0;
124 layout_board_to_device (layout, &x1_dev, &y1_dev);
128 layout_board_to_device (layout, &x2_dev, &y2_dev);
132 d1 = sqrt (dx*dx + dy*dy);
136 d2 = sqrt (dx*dx + dy*dy);
148 on_button_press_event (GtkWidget *widget,
149 GdkEventButton *event,
152 view_t *view = user_data;
153 layout_t *layout = &view->layout;
154 dvonn_game_t *game = view->game;
160 layout_device_to_board (layout, &x, &y);
162 /* Do nothing for out-of-bounds clicks */
163 if (x < 0 || x >= DVONN_BOARD_X_SIZE ||
164 y < 0 || y >= DVONN_BOARD_Y_SIZE)
169 /* Nor for cells which have array entries that are invalid. */
170 if (game->board.cells[x][y].type == DVONN_CELL_INVALID)
173 if (game->board.phase == DVONN_PHASE_PLACEMENT) {
174 if (dvonn_board_place (&game->board, x, y, &error))
175 dvonn_game_update_windows (game);
177 printf ("Illegal placement %c%d: %s\n",
185 if (! game->has_selected) {
186 if (game->board.cells[x][y].type == game->board.player) {
187 game->has_selected = TRUE;
188 game->selected_x = x;
189 game->selected_y = y;
190 dvonn_game_update_windows (game);
195 if (x == game->selected_x && y == game->selected_y)
197 game->has_selected = FALSE;
198 dvonn_game_update_windows (game);
202 if (dvonn_board_move (&game->board,
203 game->selected_x, game->selected_y,
206 game->has_selected = FALSE;
207 dvonn_game_update_windows (game);
210 printf ("Illegal move %c%d%c%d: %s\n",
211 'a' + game->selected_x,
212 game->selected_y + 1,
221 /* Add a unit-sized DVONN-ring path to cr, from (0,0) to (1,1). */
223 ring_path (cairo_t *cr)
225 cairo_new_sub_path (cr);
226 cairo_arc (cr, 0.5, 0.5, RING_OUTER_RADIUS, 0, 2 * M_PI);
227 cairo_arc_negative (cr, 0.5, 0.5, RING_INNER_RADIUS, 2 * M_PI, 0);
230 /* Some helper functions for using pango. */
232 _create_layout (cairo_t *cr, PangoFontDescription *font, const char *text)
236 layout = pango_cairo_create_layout (cr);
237 pango_layout_set_font_description (layout, font);
238 pango_layout_set_text (layout, text, -1);
239 pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
244 #define PRINTF_FORMAT(fmt_index, va_index) __attribute__ ((__format__(__printf__, fmt_index, va_index)))
247 _create_layout_vprintf (cairo_t *cr, PangoFontDescription *font, const char *fmt, va_list ap)
252 vasprintf (&text, fmt, ap);
254 layout = _create_layout (cr, font, text);
262 _create_layout_printf (cairo_t *cr, PangoFontDescription *font, const char *fmt, ...)
263 PRINTF_FORMAT (3, 4);
266 _create_layout_printf (cairo_t *cr, PangoFontDescription *font, const char *fmt, ...)
273 layout = _create_layout_vprintf (cr, font, fmt, ap);
281 _destroy_layout (PangoLayout *layout)
283 g_object_unref (layout);
287 _show_layout (cairo_t *cr, PangoLayout *layout)
289 pango_cairo_show_layout (cr, layout);
291 _destroy_layout (layout);
295 on_expose_event_draw (GtkWidget *widget,
296 GdkEventExpose *event,
299 view_t *view = user_data;
300 layout_t *layout = &view->layout;
301 dvonn_game_t *game = view->game;
305 if (layout->width != widget->allocation.width ||
306 layout->height != widget->allocation.height)
310 layout->width = widget->allocation.width;
311 layout->height = widget->allocation.height;
313 x_size = layout->width;
314 if (x_size > layout->height * BOARD_X_SIZE / (1 + M_SQRT1_2 * (BOARD_Y_SIZE-1)))
315 x_size = layout->height * BOARD_X_SIZE / (1 + M_SQRT1_2 * (BOARD_Y_SIZE-1));
317 /* Size must be a multiple of the integer cell_size */
318 layout->cell_size = x_size / BOARD_X_SIZE;
319 x_size = layout->cell_size * BOARD_X_SIZE;
320 y_size = layout->cell_size * (1 + M_SQRT1_2 * (BOARD_Y_SIZE-1));
322 layout->x_offset = (layout->width - x_size) / 2;
323 layout->y_offset = (layout->height - y_size) / 2;
325 if (game->font == NULL) {
326 game->font = pango_font_description_new ();
327 pango_font_description_set_family (game->font, "sans");
329 pango_font_description_set_absolute_size (game->font,
330 FONT_SIZE * PANGO_SCALE);
333 cr = gdk_cairo_create (widget->window);
335 cairo_set_source_rgb (cr, BACKGROUND_COLOR);
338 cairo_translate (cr, layout->x_offset, layout->y_offset);
339 cairo_scale (cr, layout->cell_size, layout->cell_size);
341 for (y = 0; y < BOARD_Y_SIZE; y++) {
342 for (x = 0; x < BOARD_X_SIZE; x++) {
345 cell = game->board.cells[x][y];
346 if (cell.type == DVONN_CELL_INVALID)
351 x + (y - DVONN_BOARD_Y_SIZE/2) / 2.0,
355 case DVONN_CELL_WHITE:
356 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* white */
358 case DVONN_CELL_BLACK:
359 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
362 cairo_set_source_rgb (cr, 0.8, 0.2, 0.2); /* red */
364 case DVONN_CELL_EMPTY:
366 cairo_set_source_rgba (cr, 0.0, 0.0, 0.2, 0.1);
369 if (game->has_selected &&
370 x == game->selected_x &&
371 y == game->selected_y)
373 cairo_fill_preserve (cr);
374 cairo_set_source_rgba (cr, 0.2, 0.2, 1.0, 0.4);
378 if (game->board.cells[x][y].height) {
381 0.5 - 0.8 * RING_INNER_RADIUS * cos (M_PI_4),
382 0.5 - 1.2 * RING_INNER_RADIUS * sin (M_PI_4));
383 height = _create_layout_printf (cr, game->font, "%d",
384 game->board.cells[x][y].height);
385 _show_layout (cr, height);
398 dvonn_game_init (dvonn_game_t *game)
403 game->has_selected = FALSE;
405 dvonn_board_init (&game->board);
411 view_init (view_t *view, dvonn_game_t *game, GtkWidget *window)
414 view->window = window;
416 view->layout.width = 0;
417 view->layout.height = 0;
421 dvonn_game_create_view (dvonn_game_t *game)
425 GtkWidget *drawing_area;
427 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
429 view = malloc (sizeof (view_t));
431 fprintf (stderr, "Out of memory.\n");
436 game->views = realloc (game->views,
437 game->num_views * sizeof (view_t *));
438 if (game->views == NULL) {
439 fprintf (stderr, "Out of memory.\n");
442 game->views[game->num_views - 1] = view;
444 view_init (view, game, window);
446 gtk_window_set_default_size (GTK_WINDOW (window), 780, 251);
448 g_signal_connect (window, "delete-event",
449 G_CALLBACK (on_delete_event_quit), NULL);
451 drawing_area = gtk_drawing_area_new ();
453 gtk_container_add (GTK_CONTAINER (window), drawing_area);
455 g_signal_connect (drawing_area, "expose-event",
456 G_CALLBACK (on_expose_event_draw), view);
458 gtk_widget_add_events (drawing_area, GDK_BUTTON_PRESS_MASK);
459 g_signal_connect (drawing_area, "button-press-event",
460 G_CALLBACK (on_button_press_event), view);
462 gtk_widget_show_all (window);
466 main (int argc, char *argv[])
470 dvonn_game_init (&game);
472 gtk_init (&argc, &argv);
474 /* Create two views of the game (one for each player) */
475 dvonn_game_create_view (&game);
476 dvonn_game_create_view (&game);