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