Import upstream Source
[gamazons.git] / src / main-boarddrawn.c
blob3ab2c594c4e607a431b81d5a36c416f86d65e41c
1 /*
2 * Initial main.c file generated by Glade. Edit as required.
3 * Glade will not overwrite this file.
4 */
6 #ifdef HAVE_CONFIG_H
7 # include <config.h>
8 #endif
10 #include <gnome.h>
12 #include "interface.h"
13 #include "support.h"
15 /* Widget Names */
16 #define BOARD_NAME "CNVS_GAMEBOARD"
19 /* the board size */
20 #define BOARD_SIZE 10
21 /* border around the board itself */
22 #define BOARD_BORDER 15
23 /* cell size on the board */
24 #define CELL_SIZE 40
25 /* cell padding */
26 #define CELL_PAD 6
27 /* square colors */
28 #define SQUARE_COLOR_1 "white"
29 #define SQUARE_COLOR_2 "grey"
31 #define THICKNESS 3
34 /* Prototypes */
35 static void init_game_board(GtkWidget *GamazonsMain);
36 static void draw_a_line(GnomeCanvasGroup *group,
37 int x1, int y1, int x2, int y2, char *color);
38 void static fill_a_square(GnomeCanvasGroup *group,
39 double x1, double y1, double x2, double y2, char *color);
40 static void get_square_color(int square, char *color);
41 void draw_board();
44 /* Globals */
45 GnomeCanvas *canvas;
46 int GameBoard[BOARD_SIZE][BOARD_SIZE];
48 /* Possible square occupants */
49 enum {
50 NOTHING,
51 BLACK,
52 WHITE,
53 ARROW
58 int
59 main (int argc, char *argv[])
61 GtkWidget *GamazonsMain;
63 #ifdef ENABLE_NLS
64 bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
65 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
66 textdomain (GETTEXT_PACKAGE);
67 #endif
69 gnome_program_init (PACKAGE, VERSION, LIBGNOMEUI_MODULE,
70 argc, argv,
71 GNOME_PARAM_APP_DATADIR, PACKAGE_DATA_DIR,
72 NULL);
75 * The following code was added by Glade to create one of each component
76 * (except popup menus), just so that you see something after building
77 * the project. Delete any components that you don't want shown initially.
79 GamazonsMain = create_GamazonsMain ();
80 init_game_board(GamazonsMain);
81 gtk_widget_show (GamazonsMain);
83 gtk_main ();
84 return 0;
89 static void init_game_board(GtkWidget *GamazonsMain)
91 int i,j;
92 char color[256];
93 GtkWidget *w = lookup_widget(GamazonsMain, BOARD_NAME);
95 canvas = GNOME_CANVAS(w);
97 /* set where can the canvas scroll (our usable area) */
98 gnome_canvas_set_scroll_region(canvas, 0.0, 0.0,
99 BOARD_SIZE*CELL_SIZE + 2*BOARD_BORDER,
100 BOARD_SIZE*CELL_SIZE + BOARD_BORDER);
102 /* set the size of the widget */
103 gtk_widget_set_usize(w,
104 BOARD_SIZE*CELL_SIZE + 2*BOARD_BORDER,
105 BOARD_SIZE*CELL_SIZE + BOARD_BORDER);
108 /* initialize pieces */
109 for (i=0; i<BOARD_SIZE; i++)
111 for (j=0; j<BOARD_SIZE; j++)
113 GameBoard[i][j] = NOTHING;
117 GameBoard[3][9] = WHITE;
118 GameBoard[6][9] = WHITE;
119 GameBoard[0][6] = WHITE;
120 GameBoard[9][6] = WHITE;
122 GameBoard[3][0] = BLACK;
123 GameBoard[6][0] = BLACK;
124 GameBoard[0][3] = BLACK;
125 GameBoard[9][3] = BLACK;
127 draw_board();
132 static void
133 draw_a_line(GnomeCanvasGroup *group,
134 int x1, int y1, int x2, int y2, char *color)
136 GnomeCanvasPoints *points;
138 /* allocate a new points array */
139 points = gnome_canvas_points_new (2);
141 /* fill out the points */
142 points->coords[0] = x1;
143 points->coords[1] = y1;
144 points->coords[2] = x2;
145 points->coords[3] = y2;
146 /* draw the line */
147 gnome_canvas_item_new(group,
148 gnome_canvas_line_get_type(),
149 "points", points,
150 "fill_color", color,
151 "width_units", (double)THICKNESS,
152 NULL);
154 /* free the points array */
155 gnome_canvas_points_free(points);
158 void fill_a_square(GnomeCanvasGroup *group,
159 double x1, double y1, double x2, double y2, char *color)
161 /* draw the line */
162 gnome_canvas_item_new(group,
163 GNOME_TYPE_CANVAS_RECT,
164 "x1", x1,
165 "y1", y1,
166 "x2", x2,
167 "y2", y2,
168 "outline_color", "black",
169 "fill_color", color,
170 "width_pixels", (double)THICKNESS,
171 NULL);
175 static void get_square_color(int square, char *color)
177 if ((square % 2) == 0)
178 strcpy(color, SQUARE_COLOR_1);
179 else
180 strcpy(color, SQUARE_COLOR_2);
184 void draw_board()
186 int i,j;
187 GdkPixbuf *white_pb, *black_pb;
188 char color[256];
189 GnomeCanvasItem *image;
190 GnomeCanvasGroup *root = GNOME_CANVAS_GROUP(gnome_canvas_root (GNOME_CANVAS (canvas)));
193 white_pb = gdk_pixbuf_new_from_file("/home/yorgasor/Projects/Gamazons/src/white.png", NULL);
194 if (white_pb == NULL)
195 printf("error loading white.png\n");
196 else
197 printf("loaded white.png just fine\n");
198 black_pb = gdk_pixbuf_new_from_file("/home/yorgasor/Projects/Gamazons/src/black.png", NULL);
199 if (black_pb == NULL)
200 printf("error loading black.png\n");
202 /* fill alternate squares */
203 for(i=0;i<BOARD_SIZE;i++)
205 for(j=0;j<BOARD_SIZE;j++)
207 get_square_color(i+j, color);
208 fill_a_square(gnome_canvas_root(canvas),
209 BOARD_BORDER + i*CELL_SIZE,
210 BOARD_BORDER + j*CELL_SIZE,
211 BOARD_BORDER + i*(CELL_SIZE) + CELL_SIZE,
212 BOARD_BORDER + j*(CELL_SIZE) + CELL_SIZE,
213 color);
214 if (GameBoard[i][j] == WHITE)
216 printf("Square %dx%d contains a white queen\n",i,j);
217 image = gnome_canvas_item_new (root,
218 gnome_canvas_pixbuf_get_type (),
219 "x", i*40.0+15.0, "y", j*40.0+15.0,
220 "width", 40.0, "height", 40.0,
221 "width_set", TRUE, "height_set", TRUE,
222 "pixbuf", white_pb,
223 NULL);
225 else if (GameBoard[i][j] == BLACK)
227 printf("Square %dx%d contains a black queen\n",i,j);
228 image = gnome_canvas_item_new (root,
229 gnome_canvas_pixbuf_get_type (),
230 "x", i*40+15.0, "y", j*40+15.0,
231 "width", 40.0, "height", 40.0,
232 "width_set", TRUE, "height_set", TRUE,
233 "pixbuf", black_pb,
234 NULL);
240 image = gnome_canvas_item_new (root,
241 gnome_canvas_pixbuf_get_type (),
242 "x", 10.0, "y", 10.0,
243 "width", 40.0, "height", 40.0,
244 "width_set", TRUE, "height_set", TRUE,
245 "pixbuf", white_pb,
246 NULL);