Debian Codegraph @

tests/test5.c

raw symbols SCIP
1 /* The Ace of Penguins - test5.c
2 Copyright (C) 1998, 2001 DJ Delorie
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
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 #define DOWN 0
27
28 static char *suits = "hdcs";
29 static char *values = "a234567890jqk";
30
31 static Picture *cards[52];
32
33 static Picture *empty, *back, *nodrop;
34
35 static Stack *st[4];
36
37 #define W CARD_WIDTH
38 #define H CARD_HEIGHT
39 #define M CARD_MARGIN
40
41 static void
42 init()
43 {
44 int s, v, t;
45 char name[30];
46
47 for (s=0; s<4; s++)
48 for (v=0; v<13; v++)
49 {
50 sprintf(name, "%c%c", values[v], suits[s]);
51 cards[v*4+s] = get_picture(name);
52 }
53 empty = get_picture("empty");
54 back = get_picture("back");
55 nodrop = get_picture("no-drop");
56 stack_set_pictures(cards, back);
57
58 for (s=0; s<4; s++)
59 {
60 #if DOWN
61 st[s] = stack_create(CARD_MARGIN+s*(CARD_MARGIN+CARD_WIDTH), CARD_MARGIN);
62 stack_set_offset(st[s], STACK_OFFSET_DOWN);
63 #else
64 st[s] = stack_create(CARD_MARGIN, CARD_MARGIN+s*(CARD_MARGIN+CARD_HEIGHT));
65 stack_set_offset(st[s], STACK_OFFSET_RIGHT);
66 #endif
67 for (v=0; v<13; v++)
68 stack_add_card(st[s], MAKE_CARD(s, v, FACEUP));
69 }
70 }
71
72 static void
73 redraw()
74 {
75 int c;
76 stack_redraw();
77 }
78
79 static void
80 key(int k, int x, int y)
81 {
82 if (k == 3 || k == 27 || k == 'q')
83 exit(0);
84 }
85
86 static Stack *src_stack = 0;
87 static int src_n = 0, src_maxn = 0;
88
89 static void
90 click(int x, int y, int b)
91 {
92 if (stack_find(x, y, &src_stack, &src_n))
93 {
94 src_maxn = stack_count_cards(src_stack);
95 stack_begin_drag(src_stack, src_n, x, y);
96 }
97 else
98 src_stack = 0;
99 }
100
101 static void
102 drag(int x, int y, int b)
103 {
104 stack_continue_drag((y/2)%(src_maxn+5), x, y);
105 }
106
107 static void
108 drop(int x, int y, int b)
109 {
110 Stack *dest_stack;
111 if (src_stack && stack_drag_find(x, y, &dest_stack))
112 stack_drop(dest_stack, src_n, 0);
113 else
114 stack_drop(src_stack, src_n, 0);
115 }
116
117 static FunctionMapping fmap[] = {
118 { "click", (void *)click },
119 { "drag", (void *)drag },
120 { "drop", (void *)drop },
121 { "init", (void *)init },
122 { "key", (void *)key },
123 { "redraw", (void *)redraw },
124 { 0, 0 }
125 };
126
127 int
128 main(int argc, char **argv)
129 {
130 init_ace(argc, argv, fmap);
131 #if DOWN
132 init_table(4*W+5*M, 2*M+51*CARD_FAN_DOWN+H);
133 #else
134 init_table(2*M+51*CARD_FAN_RIGHT+W, 4*H+5*M);
135 #endif
136 table_loop();
137 }