Debian Codegraph @

src/gfx.c

raw symbols SCIP
1 /* gfx.c - (almost) all stuff related to drawing
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
7 */
8 #include <gtk/gtk.h>
9 #include <sys/types.h>
10 #include <sys/time.h>
11
12 #include "gtkballs.h"
13 #include "scoreboard.h" /* read_score */
14 #include "prefs.h" /* pref_get_*() */
15 #include "path.h"
16 #include "theme.h"
17 #include "gfx.h"
18 #include "mainmenu.h" /* menu_set_sensitive_undo */
19 #include "mainwin.h"
20 #include "game.h"
21
22 /* x and y of cell "under pointer" */
23 gint _pointer_x = 0, _pointer_y = 0;
24
25 /* Balls that shows what colors will appear on next turn */
26 GtkWidget **_small_balls = NULL;
27
28 /* Number of node and color of the jumping ball */
29 gint _jumping_ball = 0;
30
31 gint _animation_in_progress = FALSE;
32
33 /* jumping animation phase */
34 volatile gint _phase = 0;
35
36 /* jumping animation timer tag */
37 guint _timer_tag = 0;
38
39
40 gboolean have_path(gint source_ball, gint target_ball);
41 void move_ball(gint source_ball, gint target_ball);
42
43 gint xy_to_cell_number(gint x, gint y) {
44 return find_node_of_x_y(x, y, rules_get_width());
45 }
46
47
48 gint get_jump_phase(gint x, gint y) {
49 if (!_animation_in_progress || xy_to_cell_number(x, y) != _jumping_ball) {
50 return 0;
51 }
52 return _phase;
53 }
54
55 void set_jump_phase(gint p) {
56 if (!_animation_in_progress) {
57 return;
58 }
59 _phase = p;
60 }
61
62 gint get_destroy_phase(gint x, gint y) {
63 return board_get_destroy_at_xy(x, y);
64 }
65
66 static void update_rectangle (gint x, gint y, gint w, gint h)
67 {
68 GtkWidget * board_w = mw_get_da();
69 gtk_widget_queue_draw_area (board_w, x, y, w, h);
70 }
71
72 void draw_ball_no_update(gint ballcolor, gint x, gint y, gint jumpnum, gint destroynum)
73 {
74 gint cxs = gtkbTheme->emptycell.xsize;
75 gint cys = gtkbTheme->emptycell.ysize;
76 GtkbPixmap *obj;
77 gint xr, yr;
78
79 if (!pixsurf) {
80 return;
81 }
82
83 if (pref_get_show_highlight() && x == _pointer_x && y == _pointer_y) {
84 obj = &gtkbTheme->hemptycell;
85 } else {
86 obj = &gtkbTheme->emptycell;
87 }
88
89 cairo_t * cr = cairo_create (pixsurf);
90 gdk_cairo_set_source_pixbuf (cr, obj->pixbuf, x * cxs, y * cys);
91 cairo_rectangle (cr, x * cxs, y * cys, cxs, cys);
92 cairo_fill (cr);
93
94
95 if (ballcolor > 0) { /* ball */
96 if (!jumpnum && !destroynum) { /* still ball */
97 obj = &gtkbTheme->balls[ballcolor - 1].ball;
98 } else if (jumpnum) { /* jumping ball */
99 obj = &gtkbTheme->balls[ballcolor - 1].jump[jumpnum - 1];
100 } else { /* disappearing ball */
101 obj = &gtkbTheme->balls[ballcolor - 1].destroy[destroynum - 1];
102 }
103 } else if (ballcolor < 0) { /* paw */
104 obj = &gtkbTheme->paws[-1 - ballcolor];
105 } else { /* empty cell */
106 return;
107 }
108
109 xr = x * cxs + (cxs - obj->xsize) / 2;
110 yr = y * cys + (cys - obj->ysize) / 2;
111
112 gdk_cairo_set_source_pixbuf (cr, obj->pixbuf, xr, yr);
113 cairo_paint (cr);
114 cairo_destroy (cr);
115 }
116
117 void draw_ball(gint ballcolor, gint x, gint y, gint jumpnum, gint destroynum) {
118 gint cxs = gtkbTheme->emptycell.xsize;
119 gint cys = gtkbTheme->emptycell.ysize;
120
121 draw_ball_no_update(ballcolor, x, y, jumpnum, destroynum);
122 update_rectangle(x * cxs, y * cys, cxs, cys);
123 }
124
125 void redraw_ball(gint x, gint y) {
126 draw_ball(board_get_at_xy(x, y), x, y, get_jump_phase(x, y), get_destroy_phase(x, y));
127 }
128
129 void redraw_pointer(void) {
130 draw_ball(board_get_at_xy(_pointer_x, _pointer_y),
131 _pointer_x, _pointer_y,
132 get_jump_phase(_pointer_x, _pointer_y),
133 get_destroy_phase(_pointer_x, _pointer_y));
134 }
135
136 void draw_board(void) {
137 gint i, j;
138 GtkWidget * board_w = mw_get_da();
139
140 if (!board_w) {
141 return;
142 }
143 for (j = 0; j < rules_get_height(); j++) {
144 for (i = 0; i < rules_get_width(); i++) {
145 if (!_animation_in_progress || ((xy_to_cell_number(i, j)) != _jumping_ball)) {
146 draw_ball_no_update(board_get_at_xy(i, j), i, j, 0, 0);
147 }
148 }
149 }
150
151 gtk_widget_queue_draw (board_w);
152 }
153
154 gint inc_with_limit(gint val, gint lim) {
155 if (val < lim) {
156 return val + 1;
157 }
158 return lim;
159 }
160
161 gint dec_with_limit(gint val, gint lim) {
162 if (val > lim) {
163 return val - 1;
164 }
165 return lim;
166 }
167
168 gboolean animate_ball(gpointer data) {
169 GtkWidget *widget = GTK_WIDGET(data);
170 int x, y, bc = board_get_at_node(_jumping_ball);
171
172 find_x_y_of_the_node(&x, &y, _jumping_ball, rules_get_width(), rules_get_height());
173 if (widget != NULL) {
174 draw_ball(bc, x, y, _phase + 1, 0);
175 }
176
177 ++_phase;
178 if (_phase >= gtkbTheme->balls[bc - 1].jumpphases) {
179 _phase = 0;
180 }
181 _timer_tag = g_timeout_add (gtkbTheme->balls[bc - 1].jumpdelays[_phase], animate_ball, widget);
182
183 return FALSE;
184 }
185
186 void stop_jumping_animation(void) {
187 if (_animation_in_progress) {
188 g_source_remove (_timer_tag);
189 _animation_in_progress = FALSE;
190 }
191 }
192
193 void move_pointer_to(gint x, gint y)
194 {
195 gint i, jp, dp, xc = _pointer_x, yc = _pointer_y;
196
197 if (x >= rules_get_width() || y >= rules_get_height() || x < 0 || y < 0) {
198 /* "boundary check" */
199 return;
200 }
201 if (_pointer_x == x && _pointer_y == y) {
202 return;
203 }
204 _pointer_x = x;
205 _pointer_y = y;
206 for (i = 0; i < 2; i++) {
207 if (i) {
208 xc = _pointer_x;
209 yc = _pointer_y;
210 }
211 jp = get_jump_phase(xc, yc);
212 dp = get_destroy_phase(xc, yc);
213 draw_ball(board_get_at_xy(xc, yc), xc, yc, jp, dp);
214 }
215 }
216
217 void move_pointer(Direction dir) {
218 gint xn = _pointer_x, yn = _pointer_y;
219
220 if (dir == DIR_LEFT || dir == DIR_UP_LEFT || dir == DIR_DOWN_LEFT) {
221 xn = dec_with_limit(_pointer_x, 0);
222 }
223 if (dir == DIR_RIGHT || dir == DIR_UP_RIGHT || dir == DIR_DOWN_RIGHT) {
224 xn = inc_with_limit(_pointer_x, rules_get_width() - 1);
225 }
226 if (dir == DIR_UP || dir == DIR_UP_LEFT || dir == DIR_UP_RIGHT) {
227 yn = dec_with_limit(_pointer_y, 0);
228 }
229 if (dir == DIR_DOWN || dir == DIR_DOWN_LEFT || dir == DIR_DOWN_RIGHT) {
230 yn = inc_with_limit(_pointer_y, rules_get_height() - 1);
231 }
232 if (xn == _pointer_x && yn == _pointer_y) {
233 return;
234 }
235
236 move_pointer_to(xn, yn);
237 }
238
239 void pointer_pressed(gint x, gint y) {
240 gint xn, yn, node, bc;
241
242 if (x == -1 || y == -1) {
243 x = _pointer_x;
244 y = _pointer_y;
245 }
246 node = xy_to_cell_number(x, y);
247
248 if (_animation_in_progress && node == _jumping_ball) {
249 /* already selected ball clicked */
250 return;
251 }
252
253 if (board_get_at_node(node) > 0) {
254 /* cell with ball pressed */
255 if (_animation_in_progress) {
256 /* another ball already jumping, stop him */
257 stop_jumping_animation();
258 find_x_y_of_the_node(&xn, &yn, _jumping_ball, rules_get_width(), rules_get_height());
259 draw_ball(board_get_at_node(_jumping_ball), xn, yn, 0, 0);
260 }
261 /* select ball at x,y and start it jumping */
262 _jumping_ball = node;
263 bc = board_get_at_node(node);
264 _animation_in_progress = TRUE;
265 _phase = 0;
266 _timer_tag = g_timeout_add(gtkbTheme->balls[bc - 1].jumpdelays[_phase], animate_ball, mw_get_da());
267 } else if (_animation_in_progress && have_path(_jumping_ball, node)) {
268 /* cell without ball pressed while ball selected */
269 game_save_state_for_undo();
270 menu_set_sensitive_undo(TRUE);
271 stop_jumping_animation();
272 move_ball(_jumping_ball, node);
273 if (!destroy_lines(TRUE)) {
274 new_turn(rules_get_next(), FALSE);
275 }
276 timer_start();
277 }
278 }
279
280 void reinit_board(gint *newboard, gint *newnext, gint score, gint oldnext) {
281 struct score_board sb[10];
282 gint cells = rules_get_width() * rules_get_height() - 1;
283
284 stop_jumping_animation();
285 if (_pointer_x >= rules_get_width()) {
286 _pointer_x = rules_get_width() - 1;
287 }
288 if (_pointer_y >= rules_get_height()) {
289 _pointer_y = rules_get_height() - 1;
290 }
291 if (rules_get_destroy() > cells) {
292 rules_set_destroy(cells);
293 }
294 if (rules_get_next() > cells) {
295 rules_set_next(cells);
296 }
297 game_init_game(newboard, newnext);
298 read_score(sb, NULL, NULL);
299 mw_set_hi_score(score > sb[0].score ? score : sb[0].score);
300 mw_set_user_score(score);
301 remake_board(oldnext, newnext ? 1 : 0);
302 if (!newboard) {
303 new_game();
304 } else {
305 menu_set_sensitive_undo(FALSE);
306 draw_next_balls();
307 }
308 }
309
310 void remake_board(gint numoldchilds, gboolean isnextvalid) {
311 gint cxs, cys, i;
312
313 if (numoldchilds && numoldchilds != rules_get_next())
314 {
315 if (_small_balls) {
316 for (i = 0; i < numoldchilds; i++) {
317 gtk_widget_destroy(_small_balls[i]);
318 }
319 }
320 _small_balls = g_realloc(_small_balls, rules_get_next() * sizeof(GtkWidget *));
321 for (i = 0; i < rules_get_next(); i++) {
322 if (isnextvalid) {
323 _small_balls[i] = gtk_image_new_from_pixbuf(gtkbTheme->balls[next_get(i) - 1].small.pixbuf);
324 } else {
325 _small_balls[i] = gtk_image_new_from_pixbuf(gtkbTheme->balls[0].small.pixbuf);
326 }
327 mw_small_balls_add(_small_balls[i]);
328 }
329 mw_show_hide_next_balls(pref_get_show_next_colors());
330 }
331
332 cxs = gtkbTheme->emptycell.xsize;
333 cys = gtkbTheme->emptycell.ysize;
334 gtk_widget_set_size_request(mw_get_da(), rules_get_width() * cxs, rules_get_height() * cys);
335
336 if (pixsurf) {
337 cairo_surface_destroy (pixsurf);
338 }
339 pixsurf = gdk_window_create_similar_surface (gtk_widget_get_window (mw_get_da()),
340 CAIRO_CONTENT_COLOR_ALPHA,
341 rules_get_width() * cxs, rules_get_height() * cys);
342
343 draw_board();
344 }
345
346 void draw_next_balls(void) {
347 gint i;
348
349 for (i = 0; i < rules_get_next(); i++) {
350 gtk_image_set_from_pixbuf(GTK_IMAGE(_small_balls[i]), gtkbTheme->balls[next_get(i) - 1].small.pixbuf);
351 }
352 }
353
354 /* Refill the screen from the backing pixmap */
355 gboolean boardw_draw_event (GtkWidget *widget, gpointer compat, gpointer data)
356 {
357 #if GTK_MAJOR_VERSION >= 3
358 cairo_t * cr = (cairo_t *) compat;
359 GdkRectangle rect;
360 GdkRectangle * area = &rect;
361 gdk_cairo_get_clip_rectangle (cr, area);
362 #else // gtk2, espose_ event
363 GdkEventExpose * event = (GdkEventExpose *) compat;
364 cairo_t * cr = gdk_cairo_create (gtk_widget_get_window (widget));
365 GdkRectangle * area = &(event->area);
366 #endif
367
368 int width = gtk_widget_get_allocated_width (widget);
369 if (area->width == width) {
370 cairo_set_source_surface (cr, pixsurf, 0, 0);
371 cairo_paint (cr);
372 } else {
373 //cairo_save (cr);
374 cairo_translate (cr, area->x, area->y);
375 cairo_set_source_surface (cr, pixsurf, -area->x, -area->y);
376 cairo_rectangle (cr, 0, 0, area->width, area->height);
377 cairo_fill (cr);
378 //cairo_restore (cr);
379 }
380
381 #if GTK_MAJOR_VERSION == 2
382 cairo_destroy (cr);
383 #endif
384 return FALSE;
385 }
386
387 void find_pawnum_and_direction(gint pawx, gint pawy, gint x, gint y, gint *pawnum, gint *direction)
388 {
389 if (pawy < y) {
390 *pawnum = 2;
391 if (*direction == 1) *pawnum = 6;
392 if (*direction == 3) *pawnum = 7;
393 *direction = 2; /* up */
394 } else if (pawy > y) {
395 *pawnum = 0;
396 if (*direction == 1) *pawnum = 4;
397 if (*direction == 3) *pawnum = 5;
398 *direction = 0; /* down */
399 } else if (pawx < x) {
400 *pawnum = 1;
401 if (*direction == 0) *pawnum = 4;
402 if (*direction == 2) *pawnum = 6;
403 *direction = 1; /* left */
404 } else if (pawx > x) {
405 *pawnum = 3;
406 if (*direction == 0) *pawnum = 5;
407 if (*direction == 2) *pawnum = 7;
408 *direction = 3; /* right */
409 }
410 }
411
412 gint find_direction(gint pawx, gint pawy, gint x, gint y) {
413 if (pawy < y) return 2; /* up */
414 if (pawy > y) return 0; /* down */
415 if (pawx < x) return 1; /* left */
416 if (pawx > x) return 3; /* right */
417 return -1; /* should never happens */
418 }
419
420 /* FIXME: remake it properly =/ */
421 gboolean have_path(gint source_ball, gint target_ball) {
422 gint nodes[rules_get_width() * rules_get_height()];
423 gint path[rules_get_width() * rules_get_height()];
424 gint *np = nodes;
425 gint i;
426
427 for (i = 0; i < rules_get_height() * rules_get_width(); i++) {
428 *np++ = board_get_at_node(i) > 0 ? -1 : 0;
429 }
430 nodes[source_ball] = nodes[target_ball] = 0;
431 if (!find_path(nodes, source_ball, target_ball, path, rules_get_width(), rules_get_height())) {
432 return 0;
433 }
434 return 1;
435 }
436
437 void move_ball(gint source_ball, gint target_ball)
438 {
439 gint nodes[rules_get_width() * rules_get_height()];
440 gint path[rules_get_width() * rules_get_height()];
441 gint pawx = -1, pawy = -1;
442 gint i, j, k, blah;
443 gint x, y, color, tbx, tby, bc = board_get_at_node(_jumping_ball);
444 gint direction, pawnum;
445 gint *np = nodes;
446 struct timeval tvs, tve;
447
448 g_assert(board_get_at_node(target_ball) == 0);
449 find_x_y_of_the_node(&tbx, &tby, target_ball, rules_get_width(), rules_get_height());
450
451 for (blah = 0; blah < rules_get_height() * rules_get_width(); blah++) {
452 *np++ = board_get_at_node(blah) > 0 ? -1 : 0;
453 }
454
455 nodes[source_ball] = nodes[target_ball] = 0;
456 g_assert(find_path(nodes, source_ball, target_ball, path, rules_get_width(), rules_get_height()) == 1);
457
458 find_x_y_of_the_node(&x, &y, source_ball, rules_get_width(), rules_get_height());
459 color = board_get_at_node(source_ball);
460 board_set_at_node(source_ball, 0);
461
462 draw_ball(0, x, y, 0, 0);
463 if (pref_get_show_path())
464 {
465 lock_actions(1);
466 for (k = path[0] - 1; k; k--)
467 {
468 gettimeofday(&tvs, NULL);
469 find_x_y_of_the_node(&i, &j, path[k], rules_get_width(), rules_get_height());
470 if (k == path[0] - 1) {
471 /* x and y are the coordinates of starting position */
472 pawx = x;
473 pawy = y;
474 } else {
475 find_x_y_of_the_node(&pawx, &pawy, path[k + 1], rules_get_width(), rules_get_height());
476 }
477 if (k != path[0] - 1) {
478 find_pawnum_and_direction(pawx, pawy, i, j, &pawnum, &direction);
479 } else {
480 pawnum = direction = find_direction(pawx, pawy, i, j);
481 }
482 if (pref_get_show_footprints()) {
483 board_set_at_xy(pawx, pawy, -1 - pawnum);
484 draw_ball(-1 - pawnum, pawx, pawy, 0, 0);
485 }
486 board_set_at_xy(i, j, bc);
487 draw_ball(bc, i, j, 0, 0);
488 do {
489 gtk_main_iteration_do(0);
490 gettimeofday(&tve, NULL);
491 } while ((tve.tv_sec - tvs.tv_sec) * 1000000 + tve.tv_usec - tvs.tv_usec < 100000);
492 board_set_at_xy(i, j, 0);
493 draw_ball(0, i, j, 0, 0);
494 }
495 lock_actions(0);
496 if (k == path[0] - 1) {
497 /* x and y are the coordinates of starting position */
498 pawx = x;
499 pawy = y;
500 } else {
501 find_x_y_of_the_node(&pawx, &pawy, path[k + 1], rules_get_width(), rules_get_height());
502 }
503 if (pref_get_show_footprints()) {
504 find_pawnum_and_direction(pawx, pawy, tbx, tby, &pawnum, &direction);
505 board_set_at_xy(pawx, pawy, -1 - pawnum);
506 draw_ball(-1 - pawnum, pawx, pawy, 0, 0);
507 }
508 }
509 if (pref_get_show_path() && pref_get_show_footprints()) {
510 board_set_at_node(source_ball, 0);
511 for (k = path[0] - 1; k; k--) {
512 board_set_at_node(path[k], 0);
513 }
514 }
515 board_set_at_node(target_ball, color);
516 draw_ball(color, tbx, tby, 0, 0);
517 }