]>
git.saurik.com Git - wxWidgets.git/blob - demos/forty/pile.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Forty Thieves patience game
4 // Author: Chris Breeze
7 // Copyright: (c) 1993-1998 Chris Breeze
8 // Licence: wxWindows licence
9 //---------------------------------------------------------------------------
10 // Last modified: 22nd July 1998 - ported to wxWidgets 2.0
11 /////////////////////////////////////////////////////////////////////////////
12 //+-------------------------------------------------------------+
14 //| The base class for holding piles of playing cards. |
15 //+-------------------------------------------------------------+
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
39 //+-------------------------------------------------------------+
41 //+-------------------------------------------------------------+
43 //| Initialise the pile to be empty of cards. |
44 //+-------------------------------------------------------------+
45 Pile::Pile(int x
, int y
, int dx
, int dy
)
51 for (m_topCard
= 0; m_topCard
< NumCards
; m_topCard
++)
53 m_cards
[m_topCard
] = 0;
55 m_topCard
= -1; // i.e. empty
59 //+-------------------------------------------------------------+
61 //+-------------------------------------------------------------+
63 //| Redraw the pile on the screen. If the pile is empty |
64 //| just draw a NULL card as a place holder for the pile. |
65 //| Otherwise draw the pile from the bottom up, starting |
66 //| at the origin of the pile, shifting each subsequent |
67 //| card by the pile's x and y offsets. |
68 //+-------------------------------------------------------------+
69 void Pile::Redraw(wxDC
& dc
)
71 FortyFrame
*frame
= (FortyFrame
*) wxTheApp
->GetTopWindow();
72 wxWindow
*canvas
= (wxWindow
*) NULL
;
75 canvas
= frame
->GetCanvas();
80 if (m_dx
== 0 && m_dy
== 0)
82 if ((canvas
) && (canvas
->IsExposed(m_x
,m_y
,(int)(Card::GetScale()*60),(int)(Card::GetScale()*200))))
83 m_cards
[m_topCard
]->Draw(dc
, m_x
, m_y
);
89 for (int i
= 0; i
<= m_topCard
; i
++)
91 if ((canvas
) && (canvas
->IsExposed(x
,y
,(int)(Card::GetScale()*60),(int)(Card::GetScale()*200))))
92 m_cards
[i
]->Draw(dc
, x
, y
);
93 x
+= (int)Card::GetScale()*m_dx
;
94 y
+= (int)Card::GetScale()*m_dy
;
100 if ((canvas
) && (canvas
->IsExposed(m_x
,m_y
,(int)(Card::GetScale()*60),(int)(Card::GetScale()*200))))
101 Card::DrawNullCard(dc
, m_x
, m_y
);
106 //+-------------------------------------------------------------+
107 //| Pile::GetTopCard() |
108 //+-------------------------------------------------------------+
110 //| Return a pointer to the top card in the pile or NULL |
111 //| if the pile is empty. |
112 //| NB: Gets a copy of the card without removing it from the |
114 //+-------------------------------------------------------------+
115 Card
* Pile::GetTopCard()
121 card
= m_cards
[m_topCard
];
127 //+-------------------------------------------------------------+
128 //| Pile::RemoveTopCard() |
129 //+-------------------------------------------------------------+
131 //| If the pile is not empty, remove the top card from the |
132 //| pile and return the pointer to the removed card. |
133 //| If the pile is empty return a NULL pointer. |
134 //+-------------------------------------------------------------+
135 Card
* Pile::RemoveTopCard()
141 card
= m_cards
[m_topCard
--];
147 //+-------------------------------------------------------------+
148 //| Pile::RemoveTopCard() |
149 //+-------------------------------------------------------------+
151 //| As RemoveTopCard() but also redraw the top of the pile |
152 //| after the card has been removed. |
153 //| NB: the offset allows for the redrawn area to be in a |
154 //| bitmap ready for 'dragging' cards acrosss the screen. |
155 //+-------------------------------------------------------------+
156 Card
* Pile::RemoveTopCard(wxDC
& dc
, int xOffset
, int yOffset
)
158 int topX
, topY
, x
, y
;
160 GetTopCardPos(topX
, topY
);
161 Card
* card
= RemoveTopCard();
165 card
->Erase(dc
, topX
- xOffset
, topY
- yOffset
);
169 Card::DrawNullCard(dc
, x
- xOffset
, y
- yOffset
);
173 m_cards
[m_topCard
]->Draw(dc
, x
- xOffset
, y
- yOffset
);
181 void Pile::GetTopCardPos(int& x
, int& y
)
190 x
= m_x
+ (int)Card::GetScale()*m_dx
* m_topCard
;
191 y
= m_y
+ (int)Card::GetScale()*m_dy
* m_topCard
;
195 void Pile::AddCard(Card
* card
)
197 if (m_topCard
< -1) m_topCard
= -1;
199 m_cards
[++m_topCard
] = card
;
202 void Pile::AddCard(wxDC
& dc
, Card
* card
)
207 card
->Draw(dc
, x
, y
);
210 // Can the card leave this pile.
211 // If it is a member of the pile then the answer is yes.
212 // Derived classes may override this behaviour to incorporate
213 // the rules of the game
214 bool Pile::CanCardLeave(Card
* card
)
216 for (int i
= 0; i
<= m_topCard
; i
++)
218 if (card
== m_cards
[i
]) return true;
223 // Calculate how far x, y is from top card in the pile
224 // Returns the square of the distance
225 int Pile::CalcDistance(int x
, int y
)
228 GetTopCardPos(cx
, cy
);
229 return ((cx
- x
) * (cx
- x
) + (cy
- y
) * (cy
- y
));
233 // Return the card at x, y. Check the top card first, then
234 // work down the pile. If a card is found then return a pointer
235 // to the card, otherwise return NULL
236 Card
* Pile::GetCard(int x
, int y
)
240 GetTopCardPos(cardX
, cardY
);
242 for (int i
= m_topCard
; i
>= 0; i
--)
244 if (x
>= cardX
&& x
<= cardX
+ Card::GetWidth() &&
245 y
>= cardY
&& y
<= cardY
+ Card::GetHeight())
249 cardX
-= (int)Card::GetScale()*m_dx
;
250 cardY
-= (int)Card::GetScale()*m_dy
;
256 // Return the position of the given card. If it is not a member of this pile
257 // return the origin of the pile.
258 void Pile::GetCardPos(Card
* card
, int& x
, int& y
)
263 for (int i
= 0; i
<= m_topCard
; i
++)
265 if (card
== m_cards
[i
])
269 x
+= (int)Card::GetScale()*m_dx
;
270 y
+= (int)Card::GetScale()*m_dy
;
273 // card not found in pile, return origin of pile
279 bool Pile::Overlap(int x
, int y
)
283 GetTopCardPos(cardX
, cardY
);
285 if (x
>= cardX
- Card::GetWidth() && x
<= cardX
+ Card::GetWidth() &&
286 y
>= cardY
- Card::GetHeight() && y
<= cardY
+ Card::GetHeight())