]>
git.saurik.com Git - wxWidgets.git/blob - samples/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
46 //+-------------------------------------------------------------+
48 //+-------------------------------------------------------------+
50 //| Initialise the pile to be empty of cards. |
51 //+-------------------------------------------------------------+
52 Pile::Pile(int x
, int y
, int dx
, int dy
)
58 for (m_topCard
= 0; m_topCard
< NumCards
; m_topCard
++)
60 m_cards
[m_topCard
] = 0;
62 m_topCard
= -1; // i.e. empty
66 //+-------------------------------------------------------------+
68 //+-------------------------------------------------------------+
70 //| Redraw the pile on the screen. If the pile is empty |
71 //| just draw a NULL card as a place holder for the pile. |
72 //| Otherwise draw the pile from the bottom up, starting |
73 //| at the origin of the pile, shifting each subsequent |
74 //| card by the pile's x and y offsets. |
75 //+-------------------------------------------------------------+
76 void Pile::Redraw(wxDC
& dc
)
78 wxWindow
*frame
= wxTheApp
->GetTopWindow();
79 wxWindow
*canvas
= (wxWindow
*) NULL
;
82 wxNode
*node
= frame
->GetChildren()->First();
83 if (node
) canvas
= (wxWindow
*)node
->Data();
88 if (m_dx
== 0 && m_dy
== 0)
90 if ((canvas
) && (canvas
->IsExposed(m_x
,m_y
,60,200)))
91 m_cards
[m_topCard
]->Draw(dc
, m_x
, m_y
);
97 for (int i
= 0; i
<= m_topCard
; i
++)
99 if ((canvas
) && (canvas
->IsExposed(x
,y
,60,200)))
100 m_cards
[i
]->Draw(dc
, x
, y
);
108 if ((canvas
) && (canvas
->IsExposed(m_x
,m_y
,60,200)))
109 Card::DrawNullCard(dc
, m_x
, m_y
);
114 //+-------------------------------------------------------------+
115 //| Pile::GetTopCard() |
116 //+-------------------------------------------------------------+
118 //| Return a pointer to the top card in the pile or NULL |
119 //| if the pile is empty. |
120 //| NB: Gets a copy of the card without removing it from the |
122 //+-------------------------------------------------------------+
123 Card
* Pile::GetTopCard()
129 card
= m_cards
[m_topCard
];
135 //+-------------------------------------------------------------+
136 //| Pile::RemoveTopCard() |
137 //+-------------------------------------------------------------+
139 //| If the pile is not empty, remove the top card from the |
140 //| pile and return the pointer to the removed card. |
141 //| If the pile is empty return a NULL pointer. |
142 //+-------------------------------------------------------------+
143 Card
* Pile::RemoveTopCard()
149 card
= m_cards
[m_topCard
--];
155 //+-------------------------------------------------------------+
156 //| Pile::RemoveTopCard() |
157 //+-------------------------------------------------------------+
159 //| As RemoveTopCard() but also redraw the top of the pile |
160 //| after the card has been removed. |
161 //| NB: the offset allows for the redrawn area to be in a |
162 //| bitmap ready for 'dragging' cards acrosss the screen. |
163 //+-------------------------------------------------------------+
164 Card
* Pile::RemoveTopCard(wxDC
& dc
, int xOffset
, int yOffset
)
166 int topX
, topY
, x
, y
;
168 GetTopCardPos(topX
, topY
);
169 Card
* card
= RemoveTopCard();
173 card
->Erase(dc
, topX
- xOffset
, topY
- yOffset
);
177 Card::DrawNullCard(dc
, x
- xOffset
, y
- yOffset
);
181 m_cards
[m_topCard
]->Draw(dc
, x
- xOffset
, y
- yOffset
);
189 void Pile::GetTopCardPos(int& x
, int& y
)
198 x
= m_x
+ m_dx
* m_topCard
;
199 y
= m_y
+ m_dy
* m_topCard
;
203 void Pile::AddCard(Card
* card
)
205 if (m_topCard
< -1) m_topCard
= -1;
207 m_cards
[++m_topCard
] = card
;
210 void Pile::AddCard(wxDC
& dc
, Card
* card
)
215 card
->Draw(dc
, x
, y
);
218 // Can the card leave this pile.
219 // If it is a member of the pile then the answer is yes.
220 // Derived classes may override this behaviour to incorporate
221 // the rules of the game
222 bool Pile::CanCardLeave(Card
* card
)
224 for (int i
= 0; i
<= m_topCard
; i
++)
226 if (card
== m_cards
[i
]) return TRUE
;
231 // Calculate how far x, y is from top card in the pile
232 // Returns the square of the distance
233 int Pile::CalcDistance(int x
, int y
)
236 GetTopCardPos(cx
, cy
);
237 return ((cx
- x
) * (cx
- x
) + (cy
- y
) * (cy
- y
));
241 // Return the card at x, y. Check the top card first, then
242 // work down the pile. If a card is found then return a pointer
243 // to the card, otherwise return NULL
244 Card
* Pile::GetCard(int x
, int y
)
248 GetTopCardPos(cardX
, cardY
);
250 for (int i
= m_topCard
; i
>= 0; i
--)
252 if (x
>= cardX
&& x
<= cardX
+ CardWidth
&&
253 y
>= cardY
&& y
<= cardY
+ CardHeight
)
264 // Return the position of the given card. If it is not a member of this pile
265 // return the origin of the pile.
266 void Pile::GetCardPos(Card
* card
, int& x
, int& y
)
271 for (int i
= 0; i
<= m_topCard
; i
++)
273 if (card
== m_cards
[i
])
281 // card not found in pile, return origin of pile
287 bool Pile::Overlap(int x
, int y
)
291 GetTopCardPos(cardX
, cardY
);
293 if (x
>= cardX
- CardWidth
&& x
<= cardX
+ CardWidth
&&
294 y
>= cardY
- CardHeight
&& y
<= cardY
+ CardHeight
)
304 // nothing special at the moment