2 * Copyright (C) 2008 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>
23 #include "loa-board.h"
41 on_delete_event_quit (GtkWidget *widget,
47 /* Returning FALSE allows the default handler for delete-event
48 * to proceed to cleanup the widget. */
52 /* Something like buff */
53 #define LIGHT_SQUARE_COLOR 0.89, 0.70, 0.40
54 /* Something like mahogany */
55 #define DARK_SQUARE_COLOR 0.26, 0.02, 0.01
58 on_expose_event_draw (GtkWidget *widget,
59 GdkEventExpose *event,
62 loa_game_t *game = user_data;
63 layout_t *layout = &game->layout;
67 if (layout->width != widget->allocation.width ||
68 layout->height != widget->allocation.height)
72 layout->width = widget->allocation.width;
73 layout->height = widget->allocation.height;
75 size = MIN (layout->width, layout->height);
76 /* Size must be a multiple of the integer cell_size */
77 layout->cell_size = size / BOARD_SIZE;
78 size = layout->cell_size * BOARD_SIZE;
80 layout->x_offset = (layout->width - size) / 2;
81 layout->y_offset = (layout->height - size) / 2;
84 cr = gdk_cairo_create (widget->window);
86 cairo_translate (cr, layout->x_offset, layout->y_offset);
88 for (y = 0; y < BOARD_SIZE; y++) {
89 for (x = 0; x < BOARD_SIZE; x++) {
92 cairo_translate(cr, x * layout->cell_size, y * layout->cell_size);
94 cairo_set_source_rgb (cr, LIGHT_SQUARE_COLOR);
96 cairo_set_source_rgb (cr, DARK_SQUARE_COLOR);
97 cairo_rectangle (cr, 0, 0, layout->cell_size, layout->cell_size);
99 cell = game->board.cells[x][y];
100 if (cell != LOA_CELL_EMPTY) {
102 layout->cell_size / 2.0, layout->cell_size / 2.0,
103 layout->cell_size / 2.5,
105 if (cell == LOA_CELL_BLACK)
106 cairo_set_source_rgb (cr, 0, 0, 0); /* black */
108 cairo_set_source_rgb (cr, 1, 1, 1); /* white */
109 cairo_fill_preserve (cr);
110 cairo_set_line_width (cr, 2.0);
111 if (cell == LOA_CELL_BLACK)
112 cairo_set_source_rgb (cr, LIGHT_SQUARE_COLOR);
114 cairo_set_source_rgb (cr, DARK_SQUARE_COLOR);
127 loa_game_init (loa_game_t *game)
129 game->layout.width = 0;
130 game->layout.height = 0;
132 loa_board_init (&game->board);
136 main (int argc, char *argv[])
139 GtkWidget *drawing_area;
142 loa_game_init (&game);
144 gtk_init (&argc, &argv);
146 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
148 gtk_window_set_default_size (GTK_WINDOW (window), 256, 256);
150 g_signal_connect (window, "delete-event",
151 G_CALLBACK (on_delete_event_quit), NULL);
153 drawing_area = gtk_drawing_area_new ();
155 gtk_container_add (GTK_CONTAINER (window), drawing_area);
157 g_signal_connect (drawing_area, "expose-event",
158 G_CALLBACK (on_expose_event_draw), &game);
160 gtk_widget_show_all (window);