| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include <stdio.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <string.h> |
| 21 |
#include <time.h> |
| 22 |
#include <sys/time.h> |
| 23 |
#include <sys/types.h> |
| 24 |
#include <unistd.h> |
| 25 |
#include <math.h> |
| 26 |
|
| 27 |
typedef struct Stack { |
| 28 |
struct Stack *prev, *next; |
| 29 |
int x, y; |
| 30 |
int w, h; |
| 31 |
int num_cards; |
| 32 |
int max_cards; |
| 33 |
int *cards; |
| 34 |
int fan_type; |
| 35 |
int dx, dy; |
| 36 |
struct Picture *empty_picture; |
| 37 |
} Stack; |
| 38 |
|
| 39 |
#define STACK_DEF |
| 40 |
#include "imagelib.h" |
| 41 |
#include "table.h" |
| 42 |
#include "cards.h" |
| 43 |
|
| 44 |
static Picture **fronts=0; |
| 45 |
static Picture *back=0; |
| 46 |
static Picture *nodrop=0; |
| 47 |
static int card_width = CARD_WIDTH, card_height = CARD_HEIGHT; |
| 48 |
static int stack_fan_down=CARD_FAN_DOWN, stack_fan_right=CARD_FAN_RIGHT; |
| 49 |
static int stack_fan_tbdown=CARD_FAN_TBDOWN, stack_fan_tbright=CARD_FAN_TBRIGHT; |
| 50 |
|
| 51 |
#define INIT_NUM 10 |
| 52 |
#define STEP_NUM 10 |
| 53 |
|
| 54 |
static Stack *stacks=0; |
| 55 |
|
| 56 |
static Stack *dragging_os = 0; |
| 57 |
static Stack *dragging_s = 0; |
| 58 |
static int dragging_n = 0; |
| 59 |
static int dragging_dx, dragging_dy; |
| 60 |
|
| 61 |
typedef struct Undo { |
| 62 |
Stack *src; |
| 63 |
Stack *dest; |
| 64 |
int num; |
| 65 |
int flag; |
| 66 |
} Undo; |
| 67 |
|
| 68 |
static Undo *undo=0; |
| 69 |
static int max_undo=0, num_undo=0; |
| 70 |
static int doing_undo = 0; |
| 71 |
|
| 72 |
#define UNDO_FLAG_AUTO 1 |
| 73 |
#define UNDO_FLAG_FLIP 2 |
| 74 |
static void stack_note_undo(Stack *src, Stack *dest, int num, int flag); |
| 75 |
|
| 76 |
Stack * |
| 77 |
stack_create(int x, int y) |
| 78 |
{ |
| 79 |
Stack *s = (Stack *)malloc(sizeof(Stack)); |
| 80 |
if (s == 0) |
| 81 |
return 0; |
| 82 |
memset(s, 0, sizeof(Stack)); |
| 83 |
s->max_cards = INIT_NUM; |
| 84 |
s->cards = (int *)malloc(s->max_cards * sizeof(int)); |
| 85 |
if (s->cards == 0) |
| 86 |
{ |
| 87 |
free(s); |
| 88 |
return 0; |
| 89 |
} |
| 90 |
|
| 91 |
s->x = x; |
| 92 |
s->y = y; |
| 93 |
|
| 94 |
s->next = stacks; |
| 95 |
stacks = s; |
| 96 |
if (s->next) |
| 97 |
s->next->prev = s; |
| 98 |
|
| 99 |
return s; |
| 100 |
} |
| 101 |
|
| 102 |
void |
| 103 |
stack_destroy(Stack *s) |
| 104 |
{ |
| 105 |
if (s->next) |
| 106 |
s->next->prev = s->prev; |
| 107 |
if (s->prev) |
| 108 |
s->prev->next = s->next; |
| 109 |
else |
| 110 |
stacks = s->next; |
| 111 |
free(s->cards); |
| 112 |
free(s); |
| 113 |
} |
| 114 |
|
| 115 |
static void |
| 116 |
stack_show_change(Stack *s, int a, int b) |
| 117 |
{ |
| 118 |
int w, h, num_cards; |
| 119 |
num_cards = s->num_cards; |
| 120 |
if (s == dragging_os && dragging_n < num_cards) |
| 121 |
num_cards = dragging_n; |
| 122 |
if (a > b) |
| 123 |
w = a, a = b, b = w; |
| 124 |
if (s->dx == 0 && s->dy == 0 && num_cards>0) |
| 125 |
invalidate_nc(s->x, s->y, card_width, card_height); |
| 126 |
else |
| 127 |
invalidate(s->x+a*s->dx, s->y+a*s->dy, |
| 128 |
(b-a)*s->dx+card_width, (b-a)*s->dy+card_height); |
| 129 |
} |
| 130 |
|
| 131 |
static void |
| 132 |
stack_expand(Stack *s, int n) |
| 133 |
{ |
| 134 |
if (s->max_cards <= n+1) |
| 135 |
{ |
| 136 |
s->max_cards = n + STEP_NUM; |
| 137 |
s->cards = (int *)realloc(s->cards, s->max_cards * sizeof(int)); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
void |
| 142 |
stack_set_pictures(Picture **f, Picture *b) |
| 143 |
{ |
| 144 |
fronts = f; |
| 145 |
back = b; |
| 146 |
nodrop = get_picture("no-drop"); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
void |
| 151 |
stack_load_standard_deck() |
| 152 |
{ |
| 153 |
stack_set_card_size (73, 97); |
| 154 |
} |
| 155 |
|
| 156 |
void |
| 157 |
stack_move(Stack *s, int x, int y) |
| 158 |
{ |
| 159 |
int ox = x; |
| 160 |
int oy = y; |
| 161 |
if (s->x == x && s->y == y) |
| 162 |
return; |
| 163 |
s->x = x; |
| 164 |
s->y = y; |
| 165 |
invalidate_exposure(ox, oy, s->w, s->h, x, y, s->w, s->h); |
| 166 |
invalidate(x, y, s->w, s->h); |
| 167 |
} |
| 168 |
|
| 169 |
static void |
| 170 |
stack_recalculate_size(Stack *s) |
| 171 |
{ |
| 172 |
if (s->num_cards > 0) |
| 173 |
{ |
| 174 |
s->w = (s->num_cards-1) * s->dx + card_width; |
| 175 |
s->h = (s->num_cards-1) * s->dy + card_height; |
| 176 |
} |
| 177 |
else |
| 178 |
{ |
| 179 |
s->w = card_width; |
| 180 |
s->h = card_height; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
void |
| 185 |
stack_set_offset(Stack *s, int which_offset) |
| 186 |
{ |
| 187 |
int ow = s->w; |
| 188 |
int oh = s->h; |
| 189 |
s->fan_type = which_offset; |
| 190 |
switch (which_offset) |
| 191 |
{ |
| 192 |
case STACK_OFFSET_RIGHT: |
| 193 |
s->dx = stack_fan_right; |
| 194 |
s->dy = 0; |
| 195 |
break; |
| 196 |
case STACK_OFFSET_DOWN: |
| 197 |
s->dx = 0; |
| 198 |
s->dy = stack_fan_down; |
| 199 |
break; |
| 200 |
case STACK_OFFSET_TBRIGHT: |
| 201 |
s->dx = stack_fan_tbright; |
| 202 |
s->dy = 0; |
| 203 |
break; |
| 204 |
case STACK_OFFSET_TBDOWN: |
| 205 |
s->dx = 0; |
| 206 |
s->dy = stack_fan_tbdown; |
| 207 |
break; |
| 208 |
default: |
| 209 |
s->dx = 0; |
| 210 |
s->dy = 0; |
| 211 |
break; |
| 212 |
} |
| 213 |
stack_recalculate_size(s); |
| 214 |
invalidate_exposure(s->x, s->y, ow, oh, s->x, s->y, s->w, s->h); |
| 215 |
invalidate(s->x, s->y, s->w, s->h); |
| 216 |
} |
| 217 |
|
| 218 |
void |
| 219 |
stack_set_card_size(int width, int height) |
| 220 |
{ |
| 221 |
static char *suits = "hdcs"; |
| 222 |
static char *values = " a234567890jqk"; |
| 223 |
int s, v, t; |
| 224 |
char name[30]; |
| 225 |
Stack *st; |
| 226 |
image *ak; |
| 227 |
|
| 228 |
if (!fronts) |
| 229 |
fronts = (Picture **)calloc(56, sizeof(Picture *)); |
| 230 |
for (s=0; s<4; s++) |
| 231 |
for (v=1; v<=13; v++) |
| 232 |
{ |
| 233 |
sprintf(name, "%c%c", values[v], suits[s]); |
| 234 |
fronts[v*4+s] = (Picture *)get_image(name, width, height, 0); |
| 235 |
} |
| 236 |
get_picture_default_width = card_width = fronts[1*4+0]->w; |
| 237 |
get_picture_default_height = card_height = fronts[1*4+0]->h; |
| 238 |
back = (Picture *)get_image("back", card_width, card_height, 0); |
| 239 |
nodrop = (Picture *)get_image("no-drop", width, height, 0); |
| 240 |
|
| 241 |
ak = get_image ("a-k", width*4/11, width*26/11, 0); |
| 242 |
|
| 243 |
stack_fan_right = ak->width/ak->list->across + 4; |
| 244 |
if (stack_fan_right > card_width/3) |
| 245 |
stack_fan_right = card_width/3; |
| 246 |
stack_fan_down = ak->height/ak->list->down + 7; |
| 247 |
if (stack_fan_down > card_height*2/5) |
| 248 |
stack_fan_down = card_height*2/5; |
| 249 |
stack_fan_tbright = 6; |
| 250 |
stack_fan_tbdown = 6; |
| 251 |
|
| 252 |
for (st=stacks; st; st=st->next) |
| 253 |
stack_set_offset(st, st->fan_type); |
| 254 |
} |
| 255 |
|
| 256 |
void |
| 257 |
stack_get_card_size(int *width, int *height) |
| 258 |
{ |
| 259 |
if (fronts) |
| 260 |
{ |
| 261 |
*width = fronts[MAKE_CARD(SUIT_SPADES, 1, FACEUP)]->w; |
| 262 |
*height = fronts[MAKE_CARD(SUIT_SPADES, 1, FACEUP)]->h; |
| 263 |
} |
| 264 |
else |
| 265 |
{ |
| 266 |
*width = 0; |
| 267 |
*height = 0; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
void |
| 272 |
stack_get_fans (int *down, int *right, int *tbdown, int *tbright) |
| 273 |
{ |
| 274 |
if (down) *down = stack_fan_down; |
| 275 |
if (right) *right = stack_fan_right; |
| 276 |
if (tbdown) *tbdown = stack_fan_tbdown; |
| 277 |
if (tbright) *tbright = stack_fan_tbright; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
int |
| 282 |
stack_count_cards(Stack *s) |
| 283 |
{ |
| 284 |
return s->num_cards; |
| 285 |
} |
| 286 |
|
| 287 |
int |
| 288 |
stack_get_card(Stack *s, int n) |
| 289 |
{ |
| 290 |
if (n >= 0 && n < s->num_cards) |
| 291 |
return s->cards[n]; |
| 292 |
return -1; |
| 293 |
} |
| 294 |
|
| 295 |
void |
| 296 |
stack_add_card(Stack *s, int c) |
| 297 |
{ |
| 298 |
stack_expand(s, s->num_cards+1); |
| 299 |
put_picture(FACEDOWNP(c) ? back : fronts[c], |
| 300 |
s->x+s->dx*s->num_cards, s->y+s->dy*s->num_cards, |
| 301 |
0, 0, card_width, card_height); |
| 302 |
s->cards[s->num_cards] = c; |
| 303 |
s->num_cards++; |
| 304 |
stack_recalculate_size(s); |
| 305 |
} |
| 306 |
|
| 307 |
int |
| 308 |
stack_take_card(Stack *s) |
| 309 |
{ |
| 310 |
int rv = -1; |
| 311 |
if (s->num_cards > 0) |
| 312 |
{ |
| 313 |
rv = s->cards[--s->num_cards]; |
| 314 |
stack_show_change(s, s->num_cards, s->num_cards-1); |
| 315 |
} |
| 316 |
return rv; |
| 317 |
} |
| 318 |
|
| 319 |
void |
| 320 |
stack_change_card(Stack *s, int n, int c) |
| 321 |
{ |
| 322 |
if (n < 0 || n>= s->num_cards) |
| 323 |
return; |
| 324 |
put_picture(FACEDOWNP(c) ? back : fronts[c], |
| 325 |
s->x+s->dx*n, s->y+s->dy*n, |
| 326 |
0, 0, card_width, card_height); |
| 327 |
s->cards[n] = c; |
| 328 |
} |
| 329 |
|
| 330 |
void |
| 331 |
stack_shuffle(Stack *s) |
| 332 |
{ |
| 333 |
int n, c, t; |
| 334 |
static int randomized = 0; |
| 335 |
if (! randomized) |
| 336 |
{ |
| 337 |
srand(time(0)); |
| 338 |
randomized = 1; |
| 339 |
} |
| 340 |
for (n=0; n<s->num_cards; n++) |
| 341 |
{ |
| 342 |
c = (rand() % (s->num_cards-n)) + n; |
| 343 |
t = s->cards[c]; |
| 344 |
s->cards[c] = s->cards[n]; |
| 345 |
s->cards[n] = t; |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
void |
| 351 |
stack_set_empty_picture(Stack *s, Picture *p) |
| 352 |
{ |
| 353 |
s->empty_picture = p; |
| 354 |
if (s->num_cards == 0) |
| 355 |
put_picture(p, s->x, s->y, 0, 0, card_width, card_height); |
| 356 |
} |
| 357 |
|
| 358 |
#define P(n) (FACEDOWNP(s->cards[n]) ? back : fronts[s->cards[n] & 63]) |
| 359 |
|
| 360 |
static void |
| 361 |
stack_redraw_stack(Stack *s) |
| 362 |
{ |
| 363 |
int n; |
| 364 |
int num_cards = s->num_cards; |
| 365 |
if (s == dragging_os && dragging_n < num_cards) |
| 366 |
num_cards = dragging_n; |
| 367 |
|
| 368 |
if (num_cards == 0) |
| 369 |
{ |
| 370 |
if (s->empty_picture) |
| 371 |
put_picture(s->empty_picture, |
| 372 |
s->x, s->y, 0, 0, card_width, card_height); |
| 373 |
} |
| 374 |
else |
| 375 |
{ |
| 376 |
if (s->dx) |
| 377 |
for (n=0; n<num_cards-1; n++) |
| 378 |
put_picture(P(n), s->x+s->dx*n, s->y, |
| 379 |
0, 0, s->dx, card_height); |
| 380 |
if (s->dy) |
| 381 |
for (n=0; n<num_cards-1; n++) |
| 382 |
put_picture(P(n), s->x, s->y+s->dy*n, |
| 383 |
0, 0, card_width, s->dy); |
| 384 |
|
| 385 |
n = num_cards-1; |
| 386 |
put_picture(P(n), s->x+s->dx*n, s->y+s->dy*n, |
| 387 |
0, 0, card_width, card_height); |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
static int nodrop_x, nodrop_y, nodrop_showing=0; |
| 392 |
|
| 393 |
static void |
| 394 |
stack_show_nodrop(int x, int y) |
| 395 |
{ |
| 396 |
int oldx = nodrop_x; |
| 397 |
int oldy = nodrop_y; |
| 398 |
nodrop_x = x-dragging_dx+card_width/2-nodrop->w/2; |
| 399 |
nodrop_y = y-dragging_dy+card_height/2-nodrop->h/2; |
| 400 |
if (nodrop_showing) |
| 401 |
{ |
| 402 |
nodrop_showing = 0; |
| 403 |
invalidate_exposure(oldx, oldy, nodrop->w, nodrop->h, |
| 404 |
nodrop_x, nodrop_y, nodrop->w, nodrop->h); |
| 405 |
} |
| 406 |
nodrop_showing = 1; |
| 407 |
put_picture(nodrop, nodrop_x, nodrop_y, 0, 0, nodrop->w, nodrop->h); |
| 408 |
} |
| 409 |
|
| 410 |
static void |
| 411 |
stack_hide_nodrop() |
| 412 |
{ |
| 413 |
if (nodrop_showing) |
| 414 |
{ |
| 415 |
nodrop_showing = 0; |
| 416 |
invalidate(nodrop_x, nodrop_y, nodrop->w, nodrop->h); |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
void |
| 421 |
stack_redraw() |
| 422 |
{ |
| 423 |
Stack *s; |
| 424 |
|
| 425 |
for (s=stacks; s; s=s->next) |
| 426 |
stack_redraw_stack(s); |
| 427 |
if (nodrop_showing) |
| 428 |
put_picture(nodrop, nodrop_x, nodrop_y, 0, 0, nodrop->w, nodrop->h); |
| 429 |
} |
| 430 |
|
| 431 |
void |
| 432 |
stack_peek_card(Stack *s, int n, int show) |
| 433 |
{ |
| 434 |
int x, y; |
| 435 |
if (n < 0 || n > s->num_cards) |
| 436 |
return; |
| 437 |
|
| 438 |
x = s->x + s->dx * n; |
| 439 |
y = s->y + s->dy * n; |
| 440 |
if (show) |
| 441 |
put_picture(P(n), x, y, 0, 0, card_width, card_height); |
| 442 |
else |
| 443 |
invalidate(x, y, card_width, card_height); |
| 444 |
} |
| 445 |
|
| 446 |
int |
| 447 |
stack_find(int x, int y, Stack **stack_ret, int *n_ret) |
| 448 |
{ |
| 449 |
Stack *s; |
| 450 |
int n; |
| 451 |
|
| 452 |
for (s=stacks; s; s=s->next) |
| 453 |
{ |
| 454 |
if (s == dragging_s) continue; |
| 455 |
for (n=s->num_cards-1; n>=0; n--) |
| 456 |
{ |
| 457 |
int cx = s->x + s->dx * n; |
| 458 |
int cy = s->y + s->dy * n; |
| 459 |
if (cx <= x && x < cx+card_width |
| 460 |
&& cy <= y && y < cy+card_height) |
| 461 |
{ |
| 462 |
*stack_ret = s; |
| 463 |
*n_ret = n; |
| 464 |
return 1; |
| 465 |
} |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
for (s=stacks; s; s=s->next) |
| 470 |
{ |
| 471 |
if (s == dragging_s) continue; |
| 472 |
if (s->x <= x && x < s->x+card_width |
| 473 |
&& s->y <= y && y < s->y+card_height) |
| 474 |
{ |
| 475 |
*stack_ret = s; |
| 476 |
*n_ret = -1; |
| 477 |
return 1; |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
for (s=stacks; s; s=s->next) |
| 482 |
{ |
| 483 |
if (s == dragging_s) continue; |
| 484 |
if (s->dx > 0 |
| 485 |
&& s->y <= y && y < s->y+card_height |
| 486 |
&& s->x < x) |
| 487 |
{ |
| 488 |
*stack_ret = s; |
| 489 |
*n_ret = -1; |
| 490 |
return 1; |
| 491 |
} |
| 492 |
if (s->dy > 0 |
| 493 |
&& s->y <= y |
| 494 |
&& s->x < x && x < s->x+card_width) |
| 495 |
{ |
| 496 |
*stack_ret = s; |
| 497 |
*n_ret = -1; |
| 498 |
return 1; |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
return 0; |
| 503 |
} |
| 504 |
|
| 505 |
int |
| 506 |
stack_drag_find(int x, int y, Stack **stack_ret) |
| 507 |
{ |
| 508 |
int n_ret; |
| 509 |
return stack_find(x-dragging_dx+card_width/2, y-dragging_dy+card_height/2, |
| 510 |
stack_ret, &n_ret); |
| 511 |
} |
| 512 |
|
| 513 |
int |
| 514 |
stack_card_posn(Stack *s, int n, int *x, int *y) |
| 515 |
{ |
| 516 |
if (s->num_cards == 0) |
| 517 |
{ |
| 518 |
*x = s->x; |
| 519 |
*y = s->y; |
| 520 |
return 1; |
| 521 |
} |
| 522 |
if (n < 0 || n >= s->num_cards) |
| 523 |
return 0; |
| 524 |
*x = s->x + s->dx * n; |
| 525 |
*y = s->y + s->dy * n; |
| 526 |
return 1; |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
void |
| 531 |
stack_move_cards(Stack *src, Stack *dest, int num, int flag) |
| 532 |
{ |
| 533 |
if (num <= 0 || num > src->num_cards) |
| 534 |
return; |
| 535 |
|
| 536 |
stack_note_undo(src, dest, num, flag ? UNDO_FLAG_AUTO : 0); |
| 537 |
|
| 538 |
stack_expand(dest, dest->num_cards + num); |
| 539 |
|
| 540 |
memcpy(dest->cards + dest->num_cards, |
| 541 |
src->cards + src->num_cards - num, |
| 542 |
num * sizeof(int)); |
| 543 |
|
| 544 |
src->num_cards -= num; |
| 545 |
stack_recalculate_size(src); |
| 546 |
stack_show_change(src, src->num_cards, src->num_cards + num); |
| 547 |
|
| 548 |
dest->num_cards += num; |
| 549 |
stack_recalculate_size(dest); |
| 550 |
stack_show_change(dest, dest->num_cards, dest->num_cards - num); |
| 551 |
} |
| 552 |
|
| 553 |
void |
| 554 |
stack_move_card(Stack *src, Stack *dest, int flag) |
| 555 |
{ |
| 556 |
stack_move_cards(src, dest, 1, flag); |
| 557 |
} |
| 558 |
|
| 559 |
void |
| 560 |
stack_flip_cards(Stack *src, Stack *dest, int num, int flag) |
| 561 |
{ |
| 562 |
int i; |
| 563 |
if (num <= 0 || num > src->num_cards) |
| 564 |
return; |
| 565 |
stack_note_undo(src, dest, num, flag ? UNDO_FLAG_AUTO | UNDO_FLAG_FLIP : UNDO_FLAG_FLIP); |
| 566 |
if (src == dest) |
| 567 |
{ |
| 568 |
if (num == 1) |
| 569 |
src->cards[src->num_cards-1] ^= FACEDOWN; |
| 570 |
else |
| 571 |
{ |
| 572 |
int *temp = alloca(num * sizeof(int)); |
| 573 |
for (i = 0; i < num; i++) |
| 574 |
temp[i] = src->cards[src->num_cards - i - 1] ^ FACEDOWN; |
| 575 |
memcpy(src->cards + src->num_cards - num, temp, num * sizeof(int)); |
| 576 |
} |
| 577 |
stack_show_change(src, src->num_cards, src->num_cards - num); |
| 578 |
} |
| 579 |
else |
| 580 |
{ |
| 581 |
stack_expand(dest, dest->num_cards + num); |
| 582 |
for (i = 0; i < num; i++) |
| 583 |
dest->cards[dest->num_cards++] = src->cards[--src->num_cards] ^ FACEDOWN; |
| 584 |
stack_recalculate_size(src); |
| 585 |
stack_recalculate_size(dest); |
| 586 |
stack_show_change(src, src->num_cards, src->num_cards + num); |
| 587 |
stack_show_change(dest, dest->num_cards, dest->num_cards - num); |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
void |
| 592 |
stack_flip_card(Stack *src, Stack *dest, int flag) |
| 593 |
{ |
| 594 |
stack_flip_cards(src, dest, 1, flag); |
| 595 |
} |
| 596 |
|
| 597 |
void |
| 598 |
stack_flip_stack(Stack *src, Stack *dest, int flag) |
| 599 |
{ |
| 600 |
stack_flip_cards(src, dest, src->num_cards, flag); |
| 601 |
} |
| 602 |
|
| 603 |
void |
| 604 |
stack_flip_move_stack(Stack *src, Stack *dest, int flag) |
| 605 |
{ |
| 606 |
int i; |
| 607 |
for (i = 0; i < src->num_cards && FACEDOWNP(src->cards[i]); i++); |
| 608 |
stack_flip_cards(src, dest, src->num_cards - i, flag); |
| 609 |
stack_move_cards(src, dest, src->num_cards, 1); |
| 610 |
} |
| 611 |
|
| 612 |
static void |
| 613 |
stack_note_undo(Stack *src, Stack *dest, int num, int flag) |
| 614 |
{ |
| 615 |
if (doing_undo) return; |
| 616 |
if (num_undo >= max_undo) |
| 617 |
{ |
| 618 |
max_undo += 50; |
| 619 |
if (undo) |
| 620 |
undo = (Undo *)realloc(undo, max_undo * sizeof(Undo)); |
| 621 |
else |
| 622 |
undo = (Undo *)malloc(max_undo * sizeof(Undo)); |
| 623 |
} |
| 624 |
if (src->num_cards < 1) |
| 625 |
return; |
| 626 |
undo[num_undo].src = src; |
| 627 |
undo[num_undo].dest = dest; |
| 628 |
undo[num_undo].num = num; |
| 629 |
undo[num_undo].flag = flag; |
| 630 |
num_undo++; |
| 631 |
} |
| 632 |
|
| 633 |
void |
| 634 |
stack_undo_reset() |
| 635 |
{ |
| 636 |
num_undo = 0; |
| 637 |
} |
| 638 |
|
| 639 |
void |
| 640 |
stack_undo() |
| 641 |
{ |
| 642 |
doing_undo = 1; |
| 643 |
while (num_undo) |
| 644 |
{ |
| 645 |
num_undo--; |
| 646 |
if (undo[num_undo].flag & UNDO_FLAG_FLIP) |
| 647 |
stack_flip_cards(undo[num_undo].dest, undo[num_undo].src, |
| 648 |
undo[num_undo].num, 0); |
| 649 |
else |
| 650 |
stack_move_cards(undo[num_undo].dest, undo[num_undo].src, |
| 651 |
undo[num_undo].num, 0); |
| 652 |
if (!(undo[num_undo].flag & UNDO_FLAG_AUTO)) |
| 653 |
break; |
| 654 |
} |
| 655 |
doing_undo = 0; |
| 656 |
} |
| 657 |
|
| 658 |
void |
| 659 |
stack_begin_drag(Stack *s, int n, int x, int y) |
| 660 |
{ |
| 661 |
if (dragging_s == 0) |
| 662 |
{ |
| 663 |
|
| 664 |
dragging_s = (Stack *)malloc(sizeof(Stack)); |
| 665 |
memset(dragging_s, 0, sizeof(Stack)); |
| 666 |
if (stacks) |
| 667 |
{ |
| 668 |
Stack *tmp; |
| 669 |
for (tmp=stacks; tmp->next; tmp=tmp->next); |
| 670 |
tmp->next = dragging_s; |
| 671 |
dragging_s->prev = tmp; |
| 672 |
} |
| 673 |
else |
| 674 |
stacks = dragging_s; |
| 675 |
} |
| 676 |
dragging_s->dx = s->dx; |
| 677 |
dragging_s->dy = s->dy; |
| 678 |
|
| 679 |
dragging_os = s; |
| 680 |
|
| 681 |
if (n < 0) |
| 682 |
n = 0; |
| 683 |
|
| 684 |
dragging_s->cards = s->cards+n; |
| 685 |
dragging_s->num_cards = s->num_cards-n; |
| 686 |
dragging_s->x = s->x + s->dx * n; |
| 687 |
dragging_s->y = s->y + s->dy * n; |
| 688 |
dragging_dx = x - dragging_s->x; |
| 689 |
dragging_dy = y - dragging_s->y; |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
dragging_n = s->num_cards; |
| 694 |
} |
| 695 |
|
| 696 |
void |
| 697 |
stack_continue_drag(int n, int x, int y) |
| 698 |
{ |
| 699 |
int old_x, old_y, old_w, old_h; |
| 700 |
int new_x, new_y, new_w, new_h; |
| 701 |
int old_n = dragging_n; |
| 702 |
|
| 703 |
if (!dragging_os) return; |
| 704 |
|
| 705 |
if (n < 0) n = 0; |
| 706 |
if (n > dragging_os->num_cards) n = dragging_os->num_cards; |
| 707 |
|
| 708 |
old_x = dragging_s->x; |
| 709 |
old_y = dragging_s->y; |
| 710 |
old_w = dragging_s->w; |
| 711 |
old_h = dragging_s->h; |
| 712 |
|
| 713 |
|
| 714 |
dragging_s->x = x - dragging_dx; |
| 715 |
dragging_s->y = y - dragging_dy; |
| 716 |
dragging_s->cards = dragging_os->cards + n; |
| 717 |
dragging_s->num_cards = dragging_os->num_cards - n; |
| 718 |
dragging_s->w = (dragging_s->num_cards-1)*dragging_s->dx + card_width; |
| 719 |
dragging_s->h = (dragging_s->num_cards-1)*dragging_s->dy + card_height; |
| 720 |
|
| 721 |
if (n != old_n) |
| 722 |
{ |
| 723 |
dragging_n = n; |
| 724 |
stack_show_change(dragging_os, n, old_n); |
| 725 |
} |
| 726 |
|
| 727 |
if (dragging_s->num_cards > 0) |
| 728 |
{ |
| 729 |
stack_hide_nodrop(); |
| 730 |
invalidate_exposure(old_x, old_y, old_w, old_h, |
| 731 |
dragging_s->x, dragging_s->y, |
| 732 |
dragging_s->w, dragging_s->h); |
| 733 |
stack_redraw_stack(dragging_s); |
| 734 |
} |
| 735 |
else |
| 736 |
{ |
| 737 |
stack_show_nodrop(x, y); |
| 738 |
if (old_n != dragging_os->num_cards) |
| 739 |
invalidate(old_x, old_y, old_w, old_h); |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
void |
| 744 |
stack_drop(Stack *onto, int n, int flag) |
| 745 |
{ |
| 746 |
Stack *os = dragging_os; |
| 747 |
if (!dragging_os) return; |
| 748 |
|
| 749 |
if (n < 0) n = 0; |
| 750 |
if (n > dragging_os->num_cards) n = dragging_os->num_cards; |
| 751 |
|
| 752 |
stack_hide_nodrop(); |
| 753 |
dragging_os = 0; |
| 754 |
dragging_s->num_cards = 0; |
| 755 |
invalidate(dragging_s->x, dragging_s->y, dragging_s->w, dragging_s->h); |
| 756 |
|
| 757 |
if (onto == os || n == os->num_cards) |
| 758 |
{ |
| 759 |
stack_show_change(os, dragging_n, os->num_cards); |
| 760 |
} |
| 761 |
else |
| 762 |
{ |
| 763 |
stack_move_cards(os, onto, os->num_cards - n, flag); |
| 764 |
stack_show_change(os, dragging_n, os->num_cards); |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
static int |
| 769 |
isqrt(unsigned int i) |
| 770 |
{ |
| 771 |
unsigned int min = 0; |
| 772 |
unsigned int max ; |
| 773 |
for (max=1; max*max<i; max<<=1); |
| 774 |
while (min < max-1) |
| 775 |
{ |
| 776 |
unsigned int try = (min+max)/2; |
| 777 |
unsigned int prod = try*try; |
| 778 |
#if 0 |
| 779 |
printf("isqrt(%u): %u < %u (=%u) < %u\n", i, min, try, prod, max); |
| 780 |
#endif |
| 781 |
if (prod < i) |
| 782 |
min = try; |
| 783 |
else |
| 784 |
max = try; |
| 785 |
} |
| 786 |
#if 0 |
| 787 |
printf("isqrt(%u) = %u (%d,%d,%d)\n", i, min, (min-1)*(min-1)-i, min*min-i, |
| 788 |
(min+1)*(min+1)-i); |
| 789 |
#endif |
| 790 |
return min; |
| 791 |
} |
| 792 |
|
| 793 |
static inline int |
| 794 |
iabs(int a) |
| 795 |
{ |
| 796 |
return a<0 ? -a : a; |
| 797 |
} |
| 798 |
|
| 799 |
static int |
| 800 |
ms_time() |
| 801 |
{ |
| 802 |
struct timeval tp; |
| 803 |
gettimeofday(&tp, 0); |
| 804 |
return tp.tv_sec * 1000 + tp.tv_usec / 1000; |
| 805 |
|
| 806 |
} |
| 807 |
|
| 808 |
static int ms_pause_amount = 0; |
| 809 |
|
| 810 |
static void |
| 811 |
ms_pause() |
| 812 |
{ |
| 813 |
struct timeval tv; |
| 814 |
if (ms_pause_amount == 0) |
| 815 |
{ |
| 816 |
|
| 817 |
int t1, t2, t3; |
| 818 |
t1 = ms_time(); |
| 819 |
do { t2 = ms_time(); } while (t1 == t2); |
| 820 |
do { t3 = ms_time(); } while (t2 == t3); |
| 821 |
ms_pause_amount = t3 - t2; |
| 822 |
|
| 823 |
} |
| 824 |
tv.tv_sec = 0; |
| 825 |
tv.tv_usec = ms_pause_amount; |
| 826 |
select(0, 0, 0, 0, &tv); |
| 827 |
} |
| 828 |
|
| 829 |
void |
| 830 |
stack_animate(Stack *s, Stack *d, int flag) |
| 831 |
{ |
| 832 |
int x1, y1, x2, y2, x, y, ox, oy; |
| 833 |
double dx, dist; |
| 834 |
int start, last, now; |
| 835 |
#if 0 |
| 836 |
int frames=0; |
| 837 |
#endif |
| 838 |
int sn, dn; |
| 839 |
|
| 840 |
last = start = ms_time(); |
| 841 |
|
| 842 |
sn = stack_count_cards(s); |
| 843 |
dn = stack_count_cards(d); |
| 844 |
stack_card_posn(s, sn-1, &x1, &y1); |
| 845 |
stack_card_posn(d, dn-1, &x2, &y2); |
| 846 |
if (d->num_cards) |
| 847 |
{ |
| 848 |
x2 += d->dx; |
| 849 |
y2 += d->dy; |
| 850 |
} |
| 851 |
stack_begin_drag(s, sn-1, x1, y1); |
| 852 |
flush(); |
| 853 |
|
| 854 |
|
| 855 |
dist = isqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)); |
| 856 |
dist = dist * 213 / table_width; |
| 857 |
|
| 858 |
ox = y1; |
| 859 |
oy = x1; |
| 860 |
while (1) |
| 861 |
{ |
| 862 |
now = ms_time(); |
| 863 |
if (now == last) |
| 864 |
{ |
| 865 |
ms_pause(); |
| 866 |
continue; |
| 867 |
} |
| 868 |
|
| 869 |
dx = (now - start) / dist; |
| 870 |
if (dx >= 1) |
| 871 |
break; |
| 872 |
x = (int)(x1+dx*(x2-x1)); |
| 873 |
y = (int)(y1+dx*(y2-y1)); |
| 874 |
if (x != ox || y != oy) |
| 875 |
{ |
| 876 |
stack_continue_drag(sn-1, x, y); |
| 877 |
ox = x; |
| 878 |
oy = y; |
| 879 |
flushsync(); |
| 880 |
#if 0 |
| 881 |
frames++; |
| 882 |
#endif |
| 883 |
} |
| 884 |
else |
| 885 |
ms_pause(); |
| 886 |
} |
| 887 |
stack_drop(d, sn-1, flag); |
| 888 |
flush(); |
| 889 |
now = ms_time(); |
| 890 |
last = now - start; |
| 891 |
if (last < 1) last = 1; |
| 892 |
#if 0 |
| 893 |
printf("%d frames in %d milliseconds = %g ms/f\n", |
| 894 |
frames, last, (double)last/frames); |
| 895 |
#endif |
| 896 |
} |
| 897 |
|
| 898 |
#if 0 |
| 899 |
void |
| 900 |
old_stack_animate(Stack *s, Stack *d) |
| 901 |
{ |
| 902 |
int x1, y1, x2, y2, x, y, ox, oy; |
| 903 |
double dx, ddx; |
| 904 |
int sn, dn, frames=0; |
| 905 |
double start, end; |
| 906 |
static double fps_factor = 5.0; |
| 907 |
static double fps_factors[5] = { 15, 15, 15, 15, 15 }; |
| 908 |
static int factor_slot=0; |
| 909 |
|
| 910 |
sn = stack_count_cards(s); |
| 911 |
dn = stack_count_cards(d); |
| 912 |
stack_card_posn(s, sn-1, &x1, &y1); |
| 913 |
stack_card_posn(d, dn-1, &x2, &y2); |
| 914 |
if (d->num_cards) |
| 915 |
{ |
| 916 |
x2 += d->dx; |
| 917 |
y2 += d->dy; |
| 918 |
} |
| 919 |
stack_begin_drag(s, sn-1, x1, y1); |
| 920 |
flush(); |
| 921 |
ddx = fps_factor/(iabs(x1-x2) + iabs(y1-y2)); |
| 922 |
if (ddx > 0.25) ddx = 0.25; |
| 923 |
ox = y1; |
| 924 |
oy = x1; |
| 925 |
start = ms_time(); |
| 926 |
for (dx=0; dx<1; dx+=ddx) |
| 927 |
{ |
| 928 |
x = (int)(x1+dx*(x2-x1)); |
| 929 |
y = (int)(y1+dx*(y2-y1)); |
| 930 |
if (x != ox || y != oy) |
| 931 |
{ |
| 932 |
stack_continue_drag(sn-1, x, y); |
| 933 |
frames++; |
| 934 |
} |
| 935 |
ox = x; |
| 936 |
oy = y; |
| 937 |
flush(); |
| 938 |
} |
| 939 |
stack_drop(d, sn-1, flag); |
| 940 |
flush(); |
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
end = ms_time(); |
| 945 |
if (end > start) |
| 946 |
{ |
| 947 |
#if 0 |
| 948 |
printf("%d frames in %g seconds = %g fps\n", |
| 949 |
frames, end-start, frames / (end-start)); |
| 950 |
#endif |
| 951 |
|
| 952 |
fps_factors[factor_slot] = 4000 / (frames / (end-start)); |
| 953 |
factor_slot = (factor_slot+1)%5; |
| 954 |
|
| 955 |
fps_factor = (fps_factors[0]+fps_factors[1]+fps_factors[2] |
| 956 |
+fps_factors[3]+fps_factors[4])/5; |
| 957 |
#if 0 |
| 958 |
printf("fps factor = %g\n", fps_factor); |
| 959 |
#endif |
| 960 |
} |
| 961 |
|
| 962 |
} |
| 963 |
#endif |