]>
git.saurik.com Git - wxWidgets.git/blob - demos/forty/pile.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Forty Thieves patience game
4 // Author: Chris Breeze
8 // Copyright: (c) 1993-1998 Chris Breeze
9 // Licence: wxWindows licence
10 //---------------------------------------------------------------------------
11 // Last modified: 22nd July 1998 - ported to wxWindows 2.0
12 /////////////////////////////////////////////////////////////////////////////
13 //+-------------------------------------------------------------+
15 //| The base class for holding piles of playing cards. |
16 //+-------------------------------------------------------------+
19 #pragma implementation
23 // For compilers that support precompilation, includes "wx/wx.h".
24 #include "wx/wxprec.h"
34 #pragma implementation
48 //+-------------------------------------------------------------+
50 //+-------------------------------------------------------------+
52 //| Initialise the pile to be empty of cards. |
53 //+-------------------------------------------------------------+
54 Pile::Pile(int x
, int y
, int dx
, int dy
)
60 for (m_topCard
= 0; m_topCard
< NumCards
; m_topCard
++)
62 m_cards
[m_topCard
] = 0;
64 m_topCard
= -1; // i.e. empty
68 //+-------------------------------------------------------------+
70 //+-------------------------------------------------------------+
72 //| Redraw the pile on the screen. If the pile is empty |
73 //| just draw a NULL card as a place holder for the pile. |
74 //| Otherwise draw the pile from the bottom up, starting |
75 //| at the origin of the pile, shifting each subsequent |
76 //| card by the pile's x and y offsets. |
77 //+-------------------------------------------------------------+
78 void Pile::Redraw(wxDC
& dc
)
80 FortyFrame
*frame
= (FortyFrame
*) wxTheApp
->GetTopWindow();
81 wxWindow
*canvas
= (wxWindow
*) NULL
;
84 canvas
= frame
->GetCanvas();
89 if (m_dx
== 0 && m_dy
== 0)
91 if ((canvas
) && (canvas
->IsExposed(m_x
,m_y
,(int)(Card::GetScale()*60),(int)(Card::GetScale()*200))))
92 m_cards
[m_topCard
]->Draw(dc
, m_x
, m_y
);
98 for (int i
= 0; i
<= m_topCard
; i
++)
100 if ((canvas
) && (canvas
->IsExposed(x
,y
,(int)(Card::GetScale()*60),(int)(Card::GetScale()*200))))
101 m_cards
[i
]->Draw(dc
, x
, y
);
102 x
+= (int)Card::GetScale()*m_dx
;
103 y
+= (int)Card::GetScale()*m_dy
;
109 if ((canvas
) && (canvas
->IsExposed(m_x
,m_y
,(int)(Card::GetScale()*60),(int)(Card::GetScale()*200))))
110 Card::DrawNullCard(dc
, m_x
, m_y
);
115 //+-------------------------------------------------------------+
116 //| Pile::GetTopCard() |
117 //+-------------------------------------------------------------+
119 //| Return a pointer to the top card in the pile or NULL |
120 //| if the pile is empty. |
121 //| NB: Gets a copy of the card without removing it from the |
123 //+-------------------------------------------------------------+
124 Card
* Pile::GetTopCard()
130 card
= m_cards
[m_topCard
];
136 //+-------------------------------------------------------------+
137 //| Pile::RemoveTopCard() |
138 //+-------------------------------------------------------------+
140 //| If the pile is not empty, remove the top card from the |
141 //| pile and return the pointer to the removed card. |
142 //| If the pile is empty return a NULL pointer. |
143 //+-------------------------------------------------------------+
144 Card
* Pile::RemoveTopCard()
150 card
= m_cards
[m_topCard
--];
156 //+-------------------------------------------------------------+
157 //| Pile::RemoveTopCard() |
158 //+-------------------------------------------------------------+
160 //| As RemoveTopCard() but also redraw the top of the pile |
161 //| after the card has been removed. |
162 //| NB: the offset allows for the redrawn area to be in a |
163 //| bitmap ready for 'dragging' cards acrosss the screen. |
164 //+-------------------------------------------------------------+
165 Card
* Pile::RemoveTopCard(wxDC
& dc
, int xOffset
, int yOffset
)
167 int topX
, topY
, x
, y
;
169 GetTopCardPos(topX
, topY
);
170 Card
* card
= RemoveTopCard();
174 card
->Erase(dc
, topX
- xOffset
, topY
- yOffset
);
178 Card::DrawNullCard(dc
, x
- xOffset
, y
- yOffset
);
182 m_cards
[m_topCard
]->Draw(dc
, x
- xOffset
, y
- yOffset
);
190 void Pile::GetTopCardPos(int& x
, int& y
)
199 x
= m_x
+ (int)Card::GetScale()*m_dx
* m_topCard
;
200 y
= m_y
+ (int)Card::GetScale()*m_dy
* m_topCard
;
204 void Pile::AddCard(Card
* card
)
206 if (m_topCard
< -1) m_topCard
= -1;
208 m_cards
[++m_topCard
] = card
;
211 void Pile::AddCard(wxDC
& dc
, Card
* card
)
216 card
->Draw(dc
, x
, y
);
219 // Can the card leave this pile.
220 // If it is a member of the pile then the answer is yes.
221 // Derived classes may override this behaviour to incorporate
222 // the rules of the game
223 bool Pile::CanCardLeave(Card
* card
)
225 for (int i
= 0; i
<= m_topCard
; i
++)
227 if (card
== m_cards
[i
]) return TRUE
;
232 // Calculate how far x, y is from top card in the pile
233 // Returns the square of the distance
234 int Pile::CalcDistance(int x
, int y
)
237 GetTopCardPos(cx
, cy
);
238 return ((cx
- x
) * (cx
- x
) + (cy
- y
) * (cy
- y
));
242 // Return the card at x, y. Check the top card first, then
243 // work down the pile. If a card is found then return a pointer
244 // to the card, otherwise return NULL
245 Card
* Pile::GetCard(int x
, int y
)
249 GetTopCardPos(cardX
, cardY
);
251 for (int i
= m_topCard
; i
>= 0; i
--)
253 if (x
>= cardX
&& x
<= cardX
+ Card::GetWidth() &&
254 y
>= cardY
&& y
<= cardY
+ Card::GetHeight())
258 cardX
-= (int)Card::GetScale()*m_dx
;
259 cardY
-= (int)Card::GetScale()*m_dy
;
265 // Return the position of the given card. If it is not a member of this pile
266 // return the origin of the pile.
267 void Pile::GetCardPos(Card
* card
, int& x
, int& y
)
272 for (int i
= 0; i
<= m_topCard
; i
++)
274 if (card
== m_cards
[i
])
278 x
+= (int)Card::GetScale()*m_dx
;
279 y
+= (int)Card::GetScale()*m_dy
;
282 // card not found in pile, return origin of pile
288 bool Pile::Overlap(int x
, int y
)
292 GetTopCardPos(cardX
, cardY
);
294 if (x
>= cardX
- Card::GetWidth() && x
<= cardX
+ Card::GetWidth() &&
295 y
>= cardY
- Card::GetHeight() && y
<= cardY
+ Card::GetHeight())
305 // nothing special at the moment