]> git.saurik.com Git - wxWidgets.git/blame - demos/forty/pile.cpp
Elliptic arc fix for getting bounds
[wxWidgets.git] / demos / forty / pile.cpp
CommitLineData
63cafd27
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: pile.cpp
3// Purpose: Forty Thieves patience game
4// Author: Chris Breeze
5// Modified by:
6// Created: 21/07/97
7// RCS-ID: $Id$
8// Copyright: (c) 1993-1998 Chris Breeze
010216e3 9// Licence: wxWindows licence
63cafd27 10//---------------------------------------------------------------------------
be5a51fb 11// Last modified: 22nd July 1998 - ported to wxWidgets 2.0
63cafd27
JS
12/////////////////////////////////////////////////////////////////////////////
13//+-------------------------------------------------------------+
010216e3
WS
14//| Description: |
15//| The base class for holding piles of playing cards. |
63cafd27
JS
16//+-------------------------------------------------------------+
17
18#ifdef __GNUG__
19#pragma implementation
20#pragma interface
21#endif
22
23// For compilers that support precompilation, includes "wx/wx.h".
24#include "wx/wxprec.h"
25
26#ifdef __BORLANDC__
27#pragma hdrstop
28#endif
29
30#ifndef WX_PRECOMP
31#include "wx/wx.h"
32#endif
33#ifdef __GNUG__
34#pragma implementation
35#endif
36
37#include <stdlib.h>
38#include <stdio.h>
39#include <time.h>
40#include <string.h>
41#include "card.h"
42#include "pile.h"
868741e9
JS
43#include "forty.h"
44#include "canvas.h"
63cafd27 45
e3e65dac 46#include "wx/app.h"
63cafd27
JS
47
48//+-------------------------------------------------------------+
010216e3 49//| Pile::Pile() |
63cafd27 50//+-------------------------------------------------------------+
010216e3
WS
51//| Description: |
52//| Initialise the pile to be empty of cards. |
63cafd27
JS
53//+-------------------------------------------------------------+
54Pile::Pile(int x, int y, int dx, int dy)
55{
56 m_x = x;
57 m_y = y;
58 m_dx = dx;
59 m_dy = dy;
60 for (m_topCard = 0; m_topCard < NumCards; m_topCard++)
61 {
010216e3 62 m_cards[m_topCard] = 0;
63cafd27 63 }
010216e3 64 m_topCard = -1; // i.e. empty
63cafd27
JS
65}
66
67
68//+-------------------------------------------------------------+
010216e3 69//| Pile::Redraw() |
63cafd27 70//+-------------------------------------------------------------+
010216e3
WS
71//| Description: |
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. |
63cafd27 77//+-------------------------------------------------------------+
e3e65dac 78void Pile::Redraw(wxDC& dc )
63cafd27 79{
010216e3
WS
80 FortyFrame *frame = (FortyFrame*) wxTheApp->GetTopWindow();
81 wxWindow *canvas = (wxWindow *) NULL;
82 if (frame)
83 {
84 canvas = frame->GetCanvas();
85 }
86
87 if (m_topCard >= 0)
88 {
89 if (m_dx == 0 && m_dy == 0)
90 {
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);
93 }
94 else
95 {
96 int x = m_x;
97 int y = m_y;
98 for (int i = 0; i <= m_topCard; i++)
99 {
100 if ((canvas) && (canvas->IsExposed(x,y,(int)(Card::GetScale()*60),(int)(Card::GetScale()*200))))
101 m_cards[i]->Draw(dc, x, y);
fc799548
JS
102 x += (int)Card::GetScale()*m_dx;
103 y += (int)Card::GetScale()*m_dy;
010216e3
WS
104 }
105 }
106 }
107 else
108 {
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);
111 }
63cafd27
JS
112}
113
114
115//+-------------------------------------------------------------+
010216e3 116//| Pile::GetTopCard() |
63cafd27 117//+-------------------------------------------------------------+
010216e3
WS
118//| Description: |
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 |
122//| pile. |
63cafd27
JS
123//+-------------------------------------------------------------+
124Card* Pile::GetTopCard()
125{
126 Card* card = 0;
127
128 if (m_topCard >= 0)
129 {
010216e3 130 card = m_cards[m_topCard];
63cafd27
JS
131 }
132 return card;
133}
134
135
136//+-------------------------------------------------------------+
137//| Pile::RemoveTopCard() |
138//+-------------------------------------------------------------+
010216e3
WS
139//| Description: |
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. |
63cafd27
JS
143//+-------------------------------------------------------------+
144Card* Pile::RemoveTopCard()
145{
146 Card* card = 0;
147
148 if (m_topCard >= 0)
149 {
010216e3 150 card = m_cards[m_topCard--];
63cafd27
JS
151 }
152 return card;
153}
154
155
156//+-------------------------------------------------------------+
157//| Pile::RemoveTopCard() |
158//+-------------------------------------------------------------+
010216e3
WS
159//| Description: |
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. |
63cafd27
JS
164//+-------------------------------------------------------------+
165Card* Pile::RemoveTopCard(wxDC& dc, int xOffset, int yOffset)
166{
010216e3
WS
167 int topX, topY, x, y;
168
169 GetTopCardPos(topX, topY);
170 Card* card = RemoveTopCard();
171
172 if (card)
173 {
174 card->Erase(dc, topX - xOffset, topY - yOffset);
175 GetTopCardPos(x, y);
176 if (m_topCard < 0)
177 {
178 Card::DrawNullCard(dc, x - xOffset, y - yOffset);
179 }
180 else
181 {
182 m_cards[m_topCard]->Draw(dc, x - xOffset, y - yOffset);
183 }
184 }
185
186 return card;
63cafd27
JS
187}
188
189
190void Pile::GetTopCardPos(int& x, int& y)
191{
010216e3
WS
192 if (m_topCard < 0)
193 {
194 x = m_x;
195 y = m_y;
196 }
197 else
198 {
199 x = m_x + (int)Card::GetScale()*m_dx * m_topCard;
200 y = m_y + (int)Card::GetScale()*m_dy * m_topCard;
201 }
63cafd27
JS
202}
203
204void Pile::AddCard(Card* card)
205{
206 if (m_topCard < -1) m_topCard = -1;
207
208 m_cards[++m_topCard] = card;
209}
210
211void Pile::AddCard(wxDC& dc, Card* card)
212{
213 AddCard(card);
214 int x, y;
215 GetTopCardPos(x, y);
216 card->Draw(dc, x, y);
217}
218
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
223bool Pile::CanCardLeave(Card* card)
224{
225 for (int i = 0; i <= m_topCard; i++)
226 {
010216e3 227 if (card == m_cards[i]) return true;
63cafd27 228 }
e0b5519a 229 return false;
63cafd27
JS
230}
231
232// Calculate how far x, y is from top card in the pile
233// Returns the square of the distance
234int Pile::CalcDistance(int x, int y)
235{
236 int cx, cy;
237 GetTopCardPos(cx, cy);
238 return ((cx - x) * (cx - x) + (cy - y) * (cy - y));
239}
240
241
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
245Card* Pile::GetCard(int x, int y)
246{
010216e3
WS
247 int cardX;
248 int cardY;
249 GetTopCardPos(cardX, cardY);
250
251 for (int i = m_topCard; i >= 0; i--)
252 {
253 if (x >= cardX && x <= cardX + Card::GetWidth() &&
254 y >= cardY && y <= cardY + Card::GetHeight())
255 {
256 return m_cards[i];
257 }
258 cardX -= (int)Card::GetScale()*m_dx;
259 cardY -= (int)Card::GetScale()*m_dy;
260 }
261 return 0;
63cafd27
JS
262}
263
264
265// Return the position of the given card. If it is not a member of this pile
266// return the origin of the pile.
267void Pile::GetCardPos(Card* card, int& x, int& y)
268{
010216e3
WS
269 x = m_x;
270 y = m_y;
271
272 for (int i = 0; i <= m_topCard; i++)
273 {
274 if (card == m_cards[i])
275 {
276 return;
277 }
278 x += (int)Card::GetScale()*m_dx;
279 y += (int)Card::GetScale()*m_dy;
280 }
281
282 // card not found in pile, return origin of pile
283 x = m_x;
284 y = m_y;
63cafd27
JS
285}
286
287
288bool Pile::Overlap(int x, int y)
289{
290 int cardX;
291 int cardY;
292 GetTopCardPos(cardX, cardY);
293
fc799548
JS
294 if (x >= cardX - Card::GetWidth() && x <= cardX + Card::GetWidth() &&
295 y >= cardY - Card::GetHeight() && y <= cardY + Card::GetHeight())
63cafd27 296 {
010216e3 297 return true;
63cafd27 298 }
e0b5519a 299 return false;
63cafd27 300}