gfx.c/h: consistent indentation

This commit is contained in:
wdlkmpx 2021-01-14 21:03:50 +08:00
parent c1989eec10
commit efce62f435
2 changed files with 375 additions and 358 deletions

717
src/gfx.c
View File

@ -19,7 +19,7 @@
#include "mainwin.h" #include "mainwin.h"
#include "game.h" #include "game.h"
GdkPixmap *_pixmap=NULL; GdkPixmap *_pixmap = NULL;
/* x and y of cell "under pointer" */ /* x and y of cell "under pointer" */
gint _pointer_x = 0, _pointer_y = 0; gint _pointer_x = 0, _pointer_y = 0;
@ -42,453 +42,470 @@ gboolean have_path(gint source_ball, gint target_ball);
void move_ball(gint source_ball, gint target_ball); void move_ball(gint source_ball, gint target_ball);
gint xy_to_cell_number(gint x, gint y) { gint xy_to_cell_number(gint x, gint y) {
return find_node_of_x_y(x, y, rules_get_width()); return find_node_of_x_y(x, y, rules_get_width());
} }
gint get_jump_phase(gint x, gint y) { gint get_jump_phase(gint x, gint y) {
if(!_animation_in_progress || xy_to_cell_number(x, y) != _jumping_ball) if (!_animation_in_progress || xy_to_cell_number(x, y) != _jumping_ball) {
return 0; return 0;
return _phase; }
return _phase;
} }
void set_jump_phase(gint p) { void set_jump_phase(gint p) {
if(!_animation_in_progress) if (!_animation_in_progress) {
return; return;
_phase = p; }
_phase = p;
} }
gint get_destroy_phase(gint x, gint y) { gint get_destroy_phase(gint x, gint y) {
return board_get_destroy_at_xy(x, y); return board_get_destroy_at_xy(x, y);
} }
void update_rectangle(gint x, gint y, gint w, gint h) { void update_rectangle(gint x, gint y, gint w, gint h) {
GtkWidget *widget = mw_get_da(); GtkWidget *widget = mw_get_da();
if(!widget || !_pixmap) { if (!widget || !_pixmap) {
return; return;
} }
gdk_draw_drawable (gtk_widget_get_window (widget), gdk_draw_drawable (gtk_widget_get_window (widget),
widget->style->fg_gc[gtk_widget_get_state(widget)], widget->style->fg_gc[gtk_widget_get_state(widget)],
_pixmap, x, y, x, y, w, h); _pixmap, x, y, x, y, w, h);
} }
void draw_ball_no_update(gint ballcolor, gint x, gint y, gint jumpnum, gint destroynum) { void draw_ball_no_update(gint ballcolor, gint x, gint y, gint jumpnum, gint destroynum)
GtkWidget *widget = mw_get_da(); {
GdkGC *gc = widget->style->fg_gc[gtk_widget_get_state(widget)]; GtkWidget *widget = mw_get_da();
gint cxs = gtkbTheme->emptycell.xsize; GdkGC *gc = widget->style->fg_gc[gtk_widget_get_state(widget)];
gint cys = gtkbTheme->emptycell.ysize; gint cxs = gtkbTheme->emptycell.xsize;
GtkbPixmap *obj; gint cys = gtkbTheme->emptycell.ysize;
gint xr, yr; GtkbPixmap *obj;
gint xr, yr;
if(!widget || !_pixmap) { if (!widget || !_pixmap) {
return; return;
} }
if(pref_get_show_highlight() && x == _pointer_x && y == _pointer_y) { if (pref_get_show_highlight() && x == _pointer_x && y == _pointer_y) {
obj = &gtkbTheme->hemptycell; obj = &gtkbTheme->hemptycell;
} else { } else {
obj = &gtkbTheme->emptycell; obj = &gtkbTheme->emptycell;
} }
gdk_draw_pixbuf (_pixmap, gdk_draw_pixbuf (_pixmap,
gc, gc,
obj->pixbuf, obj->pixbuf,
0, 0, 0, 0,
x * cxs, y * cys, x * cxs, y * cys,
cxs, cys, cxs, cys,
GDK_RGB_DITHER_NONE, 0, 0); GDK_RGB_DITHER_NONE, 0, 0);
if(ballcolor > 0) { /* ball */ if (ballcolor > 0) { /* ball */
if(!jumpnum && !destroynum) { /* still ball */ if (!jumpnum && !destroynum) { /* still ball */
obj = &gtkbTheme->balls[ballcolor - 1].ball; obj = &gtkbTheme->balls[ballcolor - 1].ball;
} else if(jumpnum) { /* jumping ball */ } else if (jumpnum) { /* jumping ball */
obj = &gtkbTheme->balls[ballcolor - 1].jump[jumpnum - 1]; obj = &gtkbTheme->balls[ballcolor - 1].jump[jumpnum - 1];
} else { /* disappearing ball */ } else { /* disappearing ball */
obj = &gtkbTheme->balls[ballcolor - 1].destroy[destroynum - 1]; obj = &gtkbTheme->balls[ballcolor - 1].destroy[destroynum - 1];
} }
} else if(ballcolor < 0) { /* paw */ } else if (ballcolor < 0) { /* paw */
obj = &gtkbTheme->paws[-1 - ballcolor]; obj = &gtkbTheme->paws[-1 - ballcolor];
} else { /* empty cell */ } else { /* empty cell */
return; return;
} }
xr = x * cxs + (cxs - obj->xsize) / 2; xr = x * cxs + (cxs - obj->xsize) / 2;
yr = y * cys + (cys - obj->ysize) / 2; yr = y * cys + (cys - obj->ysize) / 2;
gdk_draw_pixbuf (_pixmap, gdk_draw_pixbuf (_pixmap,
gc, gc,
obj->pixbuf, obj->pixbuf,
0, 0, 0, 0,
xr, yr, xr, yr,
obj->xsize, obj->ysize, obj->xsize, obj->ysize,
GDK_RGB_DITHER_NONE, 0, 0); GDK_RGB_DITHER_NONE, 0, 0);
} }
void draw_ball(gint ballcolor, gint x, gint y, gint jumpnum, gint destroynum) { void draw_ball(gint ballcolor, gint x, gint y, gint jumpnum, gint destroynum) {
gint cxs = gtkbTheme->emptycell.xsize; gint cxs = gtkbTheme->emptycell.xsize;
gint cys = gtkbTheme->emptycell.ysize; gint cys = gtkbTheme->emptycell.ysize;
draw_ball_no_update(ballcolor, x, y, jumpnum, destroynum); draw_ball_no_update(ballcolor, x, y, jumpnum, destroynum);
update_rectangle(x * cxs, y * cys, cxs, cys); update_rectangle(x * cxs, y * cys, cxs, cys);
} }
void redraw_ball(gint x, gint y) { void redraw_ball(gint x, gint y) {
draw_ball(board_get_at_xy(x, y), x, y, get_jump_phase(x, y), get_destroy_phase(x, y)); draw_ball(board_get_at_xy(x, y), x, y, get_jump_phase(x, y), get_destroy_phase(x, y));
} }
void redraw_pointer(void) { void redraw_pointer(void) {
draw_ball(board_get_at_xy(_pointer_x, _pointer_y), draw_ball(board_get_at_xy(_pointer_x, _pointer_y),
_pointer_x, _pointer_y, _pointer_x, _pointer_y,
get_jump_phase(_pointer_x, _pointer_y), get_jump_phase(_pointer_x, _pointer_y),
get_destroy_phase(_pointer_x, _pointer_y)); get_destroy_phase(_pointer_x, _pointer_y));
} }
void draw_board(void) { void draw_board(void) {
gint i, j; gint i, j;
if(!mw_get_da()) { if (!mw_get_da()) {
return; return;
} }
for(j = 0; j < rules_get_height(); j++) { for (j = 0; j < rules_get_height(); j++) {
for(i = 0; i < rules_get_width(); i++) { for (i = 0; i < rules_get_width(); i++) {
if(!_animation_in_progress || ((xy_to_cell_number(i, j)) != _jumping_ball)) { if (!_animation_in_progress || ((xy_to_cell_number(i, j)) != _jumping_ball)) {
draw_ball_no_update(board_get_at_xy(i, j), i, j, 0, 0); draw_ball_no_update(board_get_at_xy(i, j), i, j, 0, 0);
} }
} }
} }
update_rectangle(0, 0, -1, -1); update_rectangle(0, 0, -1, -1);
} }
gint inc_with_limit(gint val, gint lim) { gint inc_with_limit(gint val, gint lim) {
if(val < lim) if (val < lim) {
return val + 1; return val + 1;
return lim; }
return lim;
} }
gint dec_with_limit(gint val, gint lim) { gint dec_with_limit(gint val, gint lim) {
if(val > lim) if (val > lim) {
return val - 1; return val - 1;
return lim; }
return lim;
} }
gboolean animate_ball(gpointer data) { gboolean animate_ball(gpointer data) {
GtkWidget *widget = GTK_WIDGET(data); GtkWidget *widget = GTK_WIDGET(data);
int x, y, bc = board_get_at_node(_jumping_ball); int x, y, bc = board_get_at_node(_jumping_ball);
find_x_y_of_the_node(&x, &y, _jumping_ball, rules_get_width(), rules_get_height()); find_x_y_of_the_node(&x, &y, _jumping_ball, rules_get_width(), rules_get_height());
if(widget != NULL) { if (widget != NULL) {
draw_ball(bc, x, y, _phase + 1, 0); draw_ball(bc, x, y, _phase + 1, 0);
} }
++_phase; ++_phase;
if(_phase >= gtkbTheme->balls[bc - 1].jumpphases) { if (_phase >= gtkbTheme->balls[bc - 1].jumpphases) {
_phase = 0; _phase = 0;
} }
_timer_tag = g_timeout_add(gtkbTheme->balls[bc - 1].jumpdelays[_phase], animate_ball, widget); _timer_tag = g_timeout_add (gtkbTheme->balls[bc - 1].jumpdelays[_phase], animate_ball, widget);
return FALSE; return FALSE;
} }
void stop_jumping_animation(void) { void stop_jumping_animation(void) {
if(_animation_in_progress) { if (_animation_in_progress) {
g_source_remove(_timer_tag); g_source_remove (_timer_tag);
_animation_in_progress = FALSE; _animation_in_progress = FALSE;
} }
} }
void move_pointer_to(gint x, gint y) { void move_pointer_to(gint x, gint y)
gint i, jp, dp, xc = _pointer_x, yc = _pointer_y; {
gint i, jp, dp, xc = _pointer_x, yc = _pointer_y;
if(x >= rules_get_width() || y >= rules_get_height() || x < 0 || y < 0) if (x >= rules_get_width() || y >= rules_get_height() || x < 0 || y < 0) {
/* "boundary check" */ /* "boundary check" */
return; return;
if(_pointer_x == x && _pointer_y == y) }
return; if (_pointer_x == x && _pointer_y == y) {
return;
_pointer_x = x; }
_pointer_y = y; _pointer_x = x;
for(i = 0; i < 2; i++) { _pointer_y = y;
if(i) { for (i = 0; i < 2; i++) {
xc = _pointer_x; if (i) {
yc = _pointer_y; xc = _pointer_x;
} yc = _pointer_y;
jp = get_jump_phase(xc, yc); }
dp = get_destroy_phase(xc, yc); jp = get_jump_phase(xc, yc);
draw_ball(board_get_at_xy(xc, yc), xc, yc, jp, dp); dp = get_destroy_phase(xc, yc);
} draw_ball(board_get_at_xy(xc, yc), xc, yc, jp, dp);
}
} }
void move_pointer(Direction dir) { void move_pointer(Direction dir) {
gint xn = _pointer_x, yn = _pointer_y; gint xn = _pointer_x, yn = _pointer_y;
if(dir == DIR_LEFT || dir == DIR_UP_LEFT || dir == DIR_DOWN_LEFT) if (dir == DIR_LEFT || dir == DIR_UP_LEFT || dir == DIR_DOWN_LEFT) {
xn = dec_with_limit(_pointer_x, 0); xn = dec_with_limit(_pointer_x, 0);
if(dir == DIR_RIGHT || dir == DIR_UP_RIGHT || dir == DIR_DOWN_RIGHT) }
xn = inc_with_limit(_pointer_x, rules_get_width() - 1); if (dir == DIR_RIGHT || dir == DIR_UP_RIGHT || dir == DIR_DOWN_RIGHT) {
if(dir == DIR_UP || dir == DIR_UP_LEFT || dir == DIR_UP_RIGHT) xn = inc_with_limit(_pointer_x, rules_get_width() - 1);
yn = dec_with_limit(_pointer_y, 0); }
if(dir == DIR_DOWN || dir == DIR_DOWN_LEFT || dir == DIR_DOWN_RIGHT) if (dir == DIR_UP || dir == DIR_UP_LEFT || dir == DIR_UP_RIGHT) {
yn = inc_with_limit(_pointer_y, rules_get_height() - 1); yn = dec_with_limit(_pointer_y, 0);
}
if (dir == DIR_DOWN || dir == DIR_DOWN_LEFT || dir == DIR_DOWN_RIGHT) {
yn = inc_with_limit(_pointer_y, rules_get_height() - 1);
}
if (xn == _pointer_x && yn == _pointer_y) {
return;
}
if(xn == _pointer_x && yn == _pointer_y) move_pointer_to(xn, yn);
return;
move_pointer_to(xn, yn);
} }
void pointer_pressed(gint x, gint y) { void pointer_pressed(gint x, gint y) {
gint xn, yn, node, bc; gint xn, yn, node, bc;
if(x == -1 || y == -1) { if (x == -1 || y == -1) {
x = _pointer_x; x = _pointer_x;
y = _pointer_y; y = _pointer_y;
} }
node = xy_to_cell_number(x, y); node = xy_to_cell_number(x, y);
if(_animation_in_progress && node == _jumping_ball) { if (_animation_in_progress && node == _jumping_ball) {
/* already selected ball clicked */ /* already selected ball clicked */
return; return;
} }
if(board_get_at_node(node) > 0) { if (board_get_at_node(node) > 0) {
/* cell with ball pressed */ /* cell with ball pressed */
if(_animation_in_progress) { if (_animation_in_progress) {
/* another ball already jumping, stop him */ /* another ball already jumping, stop him */
stop_jumping_animation(); stop_jumping_animation();
find_x_y_of_the_node(&xn, &yn, _jumping_ball, rules_get_width(), rules_get_height()); find_x_y_of_the_node(&xn, &yn, _jumping_ball, rules_get_width(), rules_get_height());
draw_ball(board_get_at_node(_jumping_ball), xn, yn, 0, 0); draw_ball(board_get_at_node(_jumping_ball), xn, yn, 0, 0);
} }
/* select ball at x,y and start it jumping */ /* select ball at x,y and start it jumping */
_jumping_ball = node; _jumping_ball = node;
bc = board_get_at_node(node); bc = board_get_at_node(node);
_animation_in_progress = TRUE; _animation_in_progress = TRUE;
_phase = 0; _phase = 0;
_timer_tag = g_timeout_add(gtkbTheme->balls[bc - 1].jumpdelays[_phase], animate_ball, mw_get_da()); _timer_tag = g_timeout_add(gtkbTheme->balls[bc - 1].jumpdelays[_phase], animate_ball, mw_get_da());
} else if(_animation_in_progress && have_path(_jumping_ball, node)) { } else if (_animation_in_progress && have_path(_jumping_ball, node)) {
/* cell without ball pressed while ball selected */ /* cell without ball pressed while ball selected */
game_save_state_for_undo(); game_save_state_for_undo();
menu_set_sensitive_undo(TRUE); menu_set_sensitive_undo(TRUE);
stop_jumping_animation(); stop_jumping_animation();
move_ball(_jumping_ball, node); move_ball(_jumping_ball, node);
if(!destroy_lines(TRUE)) { if (!destroy_lines(TRUE)) {
new_turn(rules_get_next(), FALSE); new_turn(rules_get_next(), FALSE);
} }
timer_start(); timer_start();
} }
} }
void reinit_board(gint *newboard, gint *newnext, gint score, gint oldnext) { void reinit_board(gint *newboard, gint *newnext, gint score, gint oldnext) {
struct score_board sb[10]; struct score_board sb[10];
gint cells = rules_get_width() * rules_get_height() - 1; gint cells = rules_get_width() * rules_get_height() - 1;
stop_jumping_animation(); stop_jumping_animation();
if(_pointer_x >= rules_get_width()) { if (_pointer_x >= rules_get_width()) {
_pointer_x = rules_get_width() - 1; _pointer_x = rules_get_width() - 1;
} }
if(_pointer_y >= rules_get_height()) { if (_pointer_y >= rules_get_height()) {
_pointer_y = rules_get_height() - 1; _pointer_y = rules_get_height() - 1;
} }
if(rules_get_destroy() > cells) { if (rules_get_destroy() > cells) {
rules_set_destroy(cells); rules_set_destroy(cells);
} }
if(rules_get_next() > cells) { if (rules_get_next() > cells) {
rules_set_next(cells); rules_set_next(cells);
} }
game_init_game(newboard, newnext); game_init_game(newboard, newnext);
read_score(sb, NULL, NULL); read_score(sb, NULL, NULL);
mw_set_hi_score(score > sb[0].score ? score : sb[0].score); mw_set_hi_score(score > sb[0].score ? score : sb[0].score);
mw_set_user_score(score); mw_set_user_score(score);
remake_board(oldnext, newnext ? 1 : 0); remake_board(oldnext, newnext ? 1 : 0);
if(!newboard) { if (!newboard) {
new_game(); new_game();
} else { } else {
menu_set_sensitive_undo(FALSE); menu_set_sensitive_undo(FALSE);
draw_next_balls(); draw_next_balls();
} }
} }
void remake_board(gint numoldchilds, gboolean isnextvalid) { void remake_board(gint numoldchilds, gboolean isnextvalid) {
gint cxs, cys, i; gint cxs, cys, i;
if(numoldchilds && numoldchilds != rules_get_next()) { if (numoldchilds && numoldchilds != rules_get_next())
if(_small_balls) { {
for(i = 0; i < numoldchilds; i++) { if (_small_balls) {
gtk_widget_destroy(_small_balls[i]); for (i = 0; i < numoldchilds; i++) {
} gtk_widget_destroy(_small_balls[i]);
} }
_small_balls = g_realloc(_small_balls, rules_get_next() * sizeof(GtkWidget *)); }
for(i = 0; i < rules_get_next(); i++) { _small_balls = g_realloc(_small_balls, rules_get_next() * sizeof(GtkWidget *));
if(isnextvalid) { for (i = 0; i < rules_get_next(); i++) {
_small_balls[i] = gtk_image_new_from_pixbuf(gtkbTheme->balls[next_get(i) - 1].small.pixbuf); if (isnextvalid) {
} else { _small_balls[i] = gtk_image_new_from_pixbuf(gtkbTheme->balls[next_get(i) - 1].small.pixbuf);
_small_balls[i] = gtk_image_new_from_pixbuf(gtkbTheme->balls[0].small.pixbuf); } else {
} _small_balls[i] = gtk_image_new_from_pixbuf(gtkbTheme->balls[0].small.pixbuf);
mw_small_balls_add(_small_balls[i]); }
} mw_small_balls_add(_small_balls[i]);
mw_show_hide_next_balls(pref_get_show_next_colors()); }
} mw_show_hide_next_balls(pref_get_show_next_colors());
}
cxs = gtkbTheme->emptycell.xsize; cxs = gtkbTheme->emptycell.xsize;
cys = gtkbTheme->emptycell.ysize; cys = gtkbTheme->emptycell.ysize;
gtk_widget_set_size_request(mw_get_da(), rules_get_width() * cxs, rules_get_height() * cys); gtk_widget_set_size_request(mw_get_da(), rules_get_width() * cxs, rules_get_height() * cys);
if(_pixmap) { if (_pixmap) {
g_object_unref(_pixmap); g_object_unref(_pixmap);
} }
_pixmap = gdk_pixmap_new(mw_get_da()->window, rules_get_width() * cxs, rules_get_height() * cys, -1); _pixmap = gdk_pixmap_new(mw_get_da()->window, rules_get_width() * cxs, rules_get_height() * cys, -1);
draw_board(); draw_board();
} }
void draw_next_balls(void) { void draw_next_balls(void) {
gint i; gint i;
for(i = 0; i < rules_get_next(); i++) { for (i = 0; i < rules_get_next(); i++) {
gtk_image_set_from_pixbuf(GTK_IMAGE(_small_balls[i]), gtkbTheme->balls[next_get(i) - 1].small.pixbuf); gtk_image_set_from_pixbuf(GTK_IMAGE(_small_balls[i]), gtkbTheme->balls[next_get(i) - 1].small.pixbuf);
} }
} }
/* Refill the screen from the backing pixmap */ /* Refill the screen from the backing pixmap */
gint expose_event(GtkWidget *widget, GdkEventExpose *event) { gint expose_event(GtkWidget *widget, GdkEventExpose *event) {
GdkRectangle *rects; GdkRectangle *rects;
int n_rects; int n_rects;
int i; int i;
gdk_region_get_rectangles(event->region, &rects, &n_rects); gdk_region_get_rectangles(event->region, &rects, &n_rects);
for(i = 0; i < n_rects; i++) { for (i = 0; i < n_rects; i++) {
gdk_draw_drawable (gtk_widget_get_window (widget), gdk_draw_drawable (gtk_widget_get_window (widget),
widget->style->fg_gc[gtk_widget_get_state(widget)], widget->style->fg_gc[gtk_widget_get_state(widget)],
_pixmap, rects[i].x, rects[i].y, rects[i].x, rects[i].y, _pixmap, rects[i].x, rects[i].y, rects[i].x, rects[i].y,
rects[i].width, rects[i].height); rects[i].width, rects[i].height);
} }
g_free (rects); g_free (rects);
return FALSE; return FALSE;
} }
void find_pawnum_and_direction(gint pawx, gint pawy, gint x, gint y, gint *pawnum, gint *direction) { void find_pawnum_and_direction(gint pawx, gint pawy, gint x, gint y, gint *pawnum, gint *direction)
if(pawy < y) { {
*pawnum = 2; if (pawy < y) {
if(*direction == 1) *pawnum = 6; *pawnum = 2;
if(*direction == 3) *pawnum = 7; if (*direction == 1) *pawnum = 6;
*direction = 2; /* up */ if (*direction == 3) *pawnum = 7;
} else if(pawy > y) { *direction = 2; /* up */
*pawnum = 0; } else if (pawy > y) {
if(*direction == 1) *pawnum = 4; *pawnum = 0;
if(*direction == 3) *pawnum = 5; if (*direction == 1) *pawnum = 4;
*direction = 0; /* down */ if (*direction == 3) *pawnum = 5;
} else if(pawx < x) { *direction = 0; /* down */
*pawnum = 1; } else if (pawx < x) {
if(*direction == 0) *pawnum = 4; *pawnum = 1;
if(*direction == 2) *pawnum = 6; if (*direction == 0) *pawnum = 4;
*direction = 1; /* left */ if (*direction == 2) *pawnum = 6;
} else if(pawx > x) { *direction = 1; /* left */
*pawnum = 3; } else if (pawx > x) {
if(*direction == 0) *pawnum = 5; *pawnum = 3;
if(*direction == 2) *pawnum = 7; if (*direction == 0) *pawnum = 5;
*direction = 3; /* right */ if (*direction == 2) *pawnum = 7;
} *direction = 3; /* right */
}
} }
gint find_direction(gint pawx, gint pawy, gint x, gint y) { gint find_direction(gint pawx, gint pawy, gint x, gint y) {
if(pawy < y) return 2; /* up */ if (pawy < y) return 2; /* up */
if(pawy > y) return 0; /* down */ if (pawy > y) return 0; /* down */
if(pawx < x) return 1; /* left */ if (pawx < x) return 1; /* left */
if(pawx > x) return 3; /* right */ if (pawx > x) return 3; /* right */
return -1; /* should never happens */ return -1; /* should never happens */
} }
/* FIXME: remake it properly =/ */ /* FIXME: remake it properly =/ */
gboolean have_path(gint source_ball, gint target_ball) { gboolean have_path(gint source_ball, gint target_ball) {
gint nodes[rules_get_width() * rules_get_height()]; gint nodes[rules_get_width() * rules_get_height()];
gint path[rules_get_width() * rules_get_height()]; gint path[rules_get_width() * rules_get_height()];
gint *np = nodes; gint *np = nodes;
gint i; gint i;
for(i = 0; i < rules_get_height() * rules_get_width(); i++) { for (i = 0; i < rules_get_height() * rules_get_width(); i++) {
*np++ = board_get_at_node(i) > 0 ? -1 : 0; *np++ = board_get_at_node(i) > 0 ? -1 : 0;
} }
nodes[source_ball] = nodes[target_ball] = 0; nodes[source_ball] = nodes[target_ball] = 0;
if(!find_path(nodes, source_ball, target_ball, path, rules_get_width(), rules_get_height())) { if (!find_path(nodes, source_ball, target_ball, path, rules_get_width(), rules_get_height())) {
return 0; return 0;
} }
return 1; return 1;
} }
void move_ball(gint source_ball, gint target_ball) { void move_ball(gint source_ball, gint target_ball)
gint nodes[rules_get_width() * rules_get_height()]; {
gint path[rules_get_width() * rules_get_height()]; gint nodes[rules_get_width() * rules_get_height()];
gint pawx = -1, pawy = -1; gint path[rules_get_width() * rules_get_height()];
gint i, j, k, phase, blah; gint pawx = -1, pawy = -1;
gint x, y, color, tbx, tby, bc = board_get_at_node(_jumping_ball); gint i, j, k, phase, blah;
gint direction, pawnum; gint x, y, color, tbx, tby, bc = board_get_at_node(_jumping_ball);
gint *np = nodes; gint direction, pawnum;
struct timeval tvs, tve; gint *np = nodes;
struct timeval tvs, tve;
g_assert(board_get_at_node(target_ball) == 0); g_assert(board_get_at_node(target_ball) == 0);
find_x_y_of_the_node(&tbx, &tby, target_ball, rules_get_width(), rules_get_height()); find_x_y_of_the_node(&tbx, &tby, target_ball, rules_get_width(), rules_get_height());
for(blah = 0; blah < rules_get_height() * rules_get_width(); blah++) { for (blah = 0; blah < rules_get_height() * rules_get_width(); blah++) {
*np++ = board_get_at_node(blah) > 0 ? -1 : 0; *np++ = board_get_at_node(blah) > 0 ? -1 : 0;
} }
nodes[source_ball] = nodes[target_ball] = 0; nodes[source_ball] = nodes[target_ball] = 0;
g_assert(find_path(nodes, source_ball, target_ball, path, rules_get_width(), rules_get_height()) == 1); g_assert(find_path(nodes, source_ball, target_ball, path, rules_get_width(), rules_get_height()) == 1);
find_x_y_of_the_node(&x, &y, source_ball, rules_get_width(), rules_get_height()); find_x_y_of_the_node(&x, &y, source_ball, rules_get_width(), rules_get_height());
color = board_get_at_node(source_ball); color = board_get_at_node(source_ball);
board_set_at_node(source_ball, 0); board_set_at_node(source_ball, 0);
phase = 0; phase = 0;
draw_ball(0, x, y, 0, 0); draw_ball(0, x, y, 0, 0);
if(pref_get_show_path()) { if (pref_get_show_path())
lock_actions(1); {
for(k = path[0] - 1; k; k--) { lock_actions(1);
gettimeofday(&tvs, NULL); for (k = path[0] - 1; k; k--)
find_x_y_of_the_node(&i, &j, path[k], rules_get_width(), rules_get_height()); {
if(k == path[0] - 1) { gettimeofday(&tvs, NULL);
/* x and y are the coordinates of starting position */ find_x_y_of_the_node(&i, &j, path[k], rules_get_width(), rules_get_height());
pawx = x; if (k == path[0] - 1) {
pawy = y; /* x and y are the coordinates of starting position */
} else { pawx = x;
find_x_y_of_the_node(&pawx, &pawy, path[k + 1], rules_get_width(), rules_get_height()); pawy = y;
} } else {
if(k != path[0] - 1) { find_x_y_of_the_node(&pawx, &pawy, path[k + 1], rules_get_width(), rules_get_height());
find_pawnum_and_direction(pawx, pawy, i, j, &pawnum, &direction); }
} else { if (k != path[0] - 1) {
pawnum = direction = find_direction(pawx, pawy, i, j); find_pawnum_and_direction(pawx, pawy, i, j, &pawnum, &direction);
} } else {
if(pref_get_show_footprints()) { pawnum = direction = find_direction(pawx, pawy, i, j);
board_set_at_xy(pawx, pawy, -1 - pawnum); }
draw_ball(-1 - pawnum, pawx, pawy, 0, 0); if (pref_get_show_footprints()) {
} board_set_at_xy(pawx, pawy, -1 - pawnum);
board_set_at_xy(i, j, bc); draw_ball(-1 - pawnum, pawx, pawy, 0, 0);
draw_ball(bc, i, j, 0, 0); }
do { board_set_at_xy(i, j, bc);
gtk_main_iteration_do(0); draw_ball(bc, i, j, 0, 0);
gettimeofday(&tve, NULL); do {
} while((tve.tv_sec - tvs.tv_sec) * 1000000 + tve.tv_usec - tvs.tv_usec < 100000); gtk_main_iteration_do(0);
board_set_at_xy(i, j, 0); gettimeofday(&tve, NULL);
draw_ball(0, i, j, 0, 0); } while ((tve.tv_sec - tvs.tv_sec) * 1000000 + tve.tv_usec - tvs.tv_usec < 100000);
} board_set_at_xy(i, j, 0);
lock_actions(0); draw_ball(0, i, j, 0, 0);
if(k == path[0] - 1) { }
/* x and y are the coordinates of starting position */ lock_actions(0);
pawx = x; if (k == path[0] - 1) {
pawy = y; /* x and y are the coordinates of starting position */
} else { pawx = x;
find_x_y_of_the_node(&pawx, &pawy, path[k + 1], rules_get_width(), rules_get_height()); pawy = y;
} } else {
if (pref_get_show_footprints()) { find_x_y_of_the_node(&pawx, &pawy, path[k + 1], rules_get_width(), rules_get_height());
find_pawnum_and_direction(pawx, pawy, tbx, tby, &pawnum, &direction); }
board_set_at_xy(pawx, pawy, -1 - pawnum); if (pref_get_show_footprints()) {
draw_ball(-1 - pawnum, pawx, pawy, 0, 0); find_pawnum_and_direction(pawx, pawy, tbx, tby, &pawnum, &direction);
} board_set_at_xy(pawx, pawy, -1 - pawnum);
} draw_ball(-1 - pawnum, pawx, pawy, 0, 0);
if(pref_get_show_path() && pref_get_show_footprints()) { }
board_set_at_node(source_ball, 0); }
for(k = path[0] - 1; k; k--) { if (pref_get_show_path() && pref_get_show_footprints()) {
board_set_at_node(path[k], 0); board_set_at_node(source_ball, 0);
} for (k = path[0] - 1; k; k--) {
} board_set_at_node(path[k], 0);
board_set_at_node(target_ball, color); }
draw_ball(color, tbx, tby, 0, 0); }
board_set_at_node(target_ball, color);
draw_ball(color, tbx, tby, 0, 0);
} }

View File

@ -2,14 +2,14 @@
#define __GFX__H #define __GFX__H
typedef enum { typedef enum {
DIR_LEFT, DIR_LEFT,
DIR_RIGHT, DIR_RIGHT,
DIR_UP, DIR_UP,
DIR_DOWN, DIR_DOWN,
DIR_UP_LEFT, DIR_UP_LEFT,
DIR_DOWN_LEFT, DIR_DOWN_LEFT,
DIR_UP_RIGHT, DIR_UP_RIGHT,
DIR_DOWN_RIGHT DIR_DOWN_RIGHT
} Direction; } Direction;
void draw_next_balls(void); void draw_next_balls(void);