| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include <stdlib.h> |
| 19 |
#include <time.h> |
| 20 |
#include "cards.h" |
| 21 |
|
| 22 |
char *suits = "hdcs"; |
| 23 |
char *values = "a234567890jqk"; |
| 24 |
|
| 25 |
Picture *cards[52]; |
| 26 |
int cx[52]; |
| 27 |
int cy[52]; |
| 28 |
|
| 29 |
Picture *empty; |
| 30 |
|
| 31 |
#define W CARD_WIDTH |
| 32 |
#define H CARD_HEIGHT |
| 33 |
#define M CARD_MARGIN |
| 34 |
|
| 35 |
int |
| 36 |
main(int argc, char **argv) |
| 37 |
{ |
| 38 |
init_table(argc, argv, 13*W+14*M, 5*H+6*M); |
| 39 |
table_loop(); |
| 40 |
} |
| 41 |
|
| 42 |
void |
| 43 |
init() |
| 44 |
{ |
| 45 |
int s, v, t; |
| 46 |
char name[30]; |
| 47 |
int shuffle[52]; |
| 48 |
|
| 49 |
for (s=0; s<4; s++) |
| 50 |
for (v=0; v<13; v++) |
| 51 |
{ |
| 52 |
sprintf(name, "%c%c", values[v], suits[s]); |
| 53 |
cards[v*4+s] = get_picture(name); |
| 54 |
cx[v*4+s] = M+v*(M+W); |
| 55 |
cy[v*4+s] = M+s*(M+H); |
| 56 |
} |
| 57 |
empty = get_picture("empty"); |
| 58 |
|
| 59 |
#if 0 |
| 60 |
|
| 61 |
srand(time(0)); |
| 62 |
for (s=0; s<52; s++) |
| 63 |
{ |
| 64 |
v = (rand() % (52-s)) + s; |
| 65 |
t = cx[v]; |
| 66 |
cx[v] = cx[s]; |
| 67 |
cx[s] = t; |
| 68 |
t = cy[v]; |
| 69 |
cy[v] = cy[s]; |
| 70 |
cy[s] = t; |
| 71 |
} |
| 72 |
#endif |
| 73 |
} |
| 74 |
|
| 75 |
void |
| 76 |
redraw() |
| 77 |
{ |
| 78 |
int c; |
| 79 |
put_picture(empty, M, M, 0, 0, W, H); |
| 80 |
for (c=0; c<52; c++) |
| 81 |
put_picture(cards[c], cx[c], cy[c], 0, 0, W, H); |
| 82 |
} |
| 83 |
|
| 84 |
int selected_card = -1; |
| 85 |
int offset_x, offset_y; |
| 86 |
|
| 87 |
int |
| 88 |
xy2card(int x, int y) |
| 89 |
{ |
| 90 |
int i; |
| 91 |
for (i=51; i>=0; i--) |
| 92 |
if (cx[i] <= x && x < cx[i]+W |
| 93 |
&& cy[i] <= y && y < cy[i]+H) |
| 94 |
return i; |
| 95 |
return -1; |
| 96 |
} |
| 97 |
|
| 98 |
void |
| 99 |
click(int x, int y, int b) |
| 100 |
{ |
| 101 |
int i; |
| 102 |
int old_x, old_y; |
| 103 |
Picture *c; |
| 104 |
selected_card = xy2card(x, y); |
| 105 |
if (selected_card == -1) |
| 106 |
return; |
| 107 |
c = cards[selected_card]; |
| 108 |
old_x = cx[selected_card]; |
| 109 |
old_y = cy[selected_card]; |
| 110 |
for (i=selected_card; i<51; i++) |
| 111 |
{ |
| 112 |
cards[i] = cards[i+1]; |
| 113 |
cx[i] = cx[i+1]; |
| 114 |
cy[i] = cy[i+1]; |
| 115 |
} |
| 116 |
cards[51] = c; |
| 117 |
cx[51] = old_x; |
| 118 |
cy[51] = old_y; |
| 119 |
selected_card = 51; |
| 120 |
put_picture(cards[selected_card], |
| 121 |
cx[selected_card], cy[selected_card], |
| 122 |
0, 0, W, H); |
| 123 |
offset_x = x - cx[selected_card]; |
| 124 |
offset_y = y - cy[selected_card]; |
| 125 |
} |
| 126 |
|
| 127 |
void double_click(int x, int y, int b) |
| 128 |
{ |
| 129 |
int old_x, old_y, i; |
| 130 |
Picture *c; |
| 131 |
selected_card = xy2card(x, y); |
| 132 |
if (selected_card == -1) |
| 133 |
return; |
| 134 |
c = cards[selected_card]; |
| 135 |
old_x = cx[selected_card]; |
| 136 |
old_y = cy[selected_card]; |
| 137 |
for (i=selected_card; i>0; i--) |
| 138 |
{ |
| 139 |
cards[i] = cards[i-1]; |
| 140 |
cx[i] = cx[i-1]; |
| 141 |
cy[i] = cy[i-1]; |
| 142 |
} |
| 143 |
cards[0] = c; |
| 144 |
cx[0] = old_x; |
| 145 |
cy[0] = old_y; |
| 146 |
selected_card = 0; |
| 147 |
invalidate(cx[0], cy[0], W, H); |
| 148 |
} |
| 149 |
|
| 150 |
void |
| 151 |
drag(int x, int y, int b) |
| 152 |
{ |
| 153 |
int old_x, old_y; |
| 154 |
if (selected_card == -1) |
| 155 |
return; |
| 156 |
old_x = cx[selected_card]; |
| 157 |
old_y = cy[selected_card]; |
| 158 |
cx[selected_card] = x - offset_x; |
| 159 |
cy[selected_card] = y - offset_y; |
| 160 |
invalidate_exposure(old_x, old_y, W, H, |
| 161 |
cx[selected_card], cy[selected_card], W, H); |
| 162 |
if (selected_card == 51) |
| 163 |
put_picture(cards[selected_card], cx[selected_card], cy[selected_card], 0, 0, W, H); |
| 164 |
else |
| 165 |
invalidate_nc(cx[selected_card], cy[selected_card], W, H); |
| 166 |
} |
| 167 |
|
| 168 |
void |
| 169 |
drop(int x, int y, int b) |
| 170 |
{ |
| 171 |
if (selected_card == -1) |
| 172 |
return; |
| 173 |
snap_to_grid(&x, &y, |
| 174 |
M+W, M+H, |
| 175 |
M+offset_x, M+offset_y, |
| 176 |
15); |
| 177 |
drag(x, y, b); |
| 178 |
selected_card = -1; |
| 179 |
} |
| 180 |
|
| 181 |
void |
| 182 |
key(int k, int x, int y) |
| 183 |
{ |
| 184 |
if (k == 3 || k == 27 || k == 'q') |
| 185 |
exit(0); |
| 186 |
} |