-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcard.cpp
More file actions
252 lines (195 loc) · 6.35 KB
/
Copy pathcard.cpp
File metadata and controls
252 lines (195 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/***************************************************************************/
/** Microsoft Windows **/
/** Copyright(c) Microsoft Corp., 1991, 1992 **/
/***************************************************************************/
/****************************************************************************
card.cpp -- wxWidgets port
Methods for card objects. Card drawing uses wxBitmap loaded from bmp/.
****************************************************************************/
#include "card.h"
#include "main.h"
#include <cstdlib>
/* Declare (and initialize) static members */
wxBitmap card::m_bmFgnd;
wxBitmap card::m_bmBgnd2;
wxBitmap card::m_bmCard[52];
wxBitmap card::m_bmBack;
wxBitmap card::m_bmGhost;
bool card::m_bitmapsLoaded = false;
bool card::bConstructed = false;
int card::dxCrd = 71;
int card::dyCrd = 96;
wxBitmap card::m_bmBgnd;
int card::count = 0;
int card::stepsize = 15;
card::card(int n) : id(n), state(NORMAL)
{
loc.x = 0;
loc.y = 0;
if (count == 0)
{
bConstructed = false;
if (!LoadCardBitmaps())
return;
bConstructed = true;
/* Create working bitmaps for animation */
m_bmBgnd = wxBitmap(dxCrd, dyCrd);
m_bmFgnd = wxBitmap(dxCrd, dyCrd);
m_bmBgnd2 = wxBitmap(dxCrd, dyCrd);
}
count++;
}
card::~card()
{
count--;
if (count == 0)
{
m_bmBgnd = wxNullBitmap;
m_bmFgnd = wxNullBitmap;
m_bmBgnd2 = wxNullBitmap;
}
}
bool card::Draw(wxDC &dc, int x, int y, int mode, bool bUpdateLoc)
{
if (bUpdateLoc)
{
loc.x = x;
loc.y = y;
}
if (id == EMPTY)
return false;
return DrawCard(dc, x, y, dxCrd, dyCrd,
mode == FACEDOWN ? CARDBACK : id, mode);
}
bool card::CleanDraw(wxDC &dc)
{
if (id == EMPTY)
return false;
wxBitmap bitmap(dxCrd, dyCrd);
wxMemoryDC memDC;
memDC.SelectObject(bitmap);
DrawCard(memDC, 0, 0, dxCrd, dyCrd, id, FACEUP);
dc.Blit(loc.x, loc.y, dxCrd, dyCrd, &memDC, 0, 0);
memDC.SelectObject(wxNullBitmap);
return true;
}
bool card::PopDraw(wxDC &dc)
{
if (id == EMPTY)
return false;
int y = loc.y;
if (state == SELECTED)
y -= POPSPACING;
return DrawCard(dc, loc.x, y, dxCrd, dyCrd, id, FACEUP);
}
void card::Glide(wxDC &dc, int xEnd, int yEnd)
{
(void)dc; // animation draws into CMainWindow::m_backbuffer; the
// DC passed by callers (historically a wxClientDC on
// the main window) is no longer used -- see GlideStep.
/* Suppress scene re-render during animation so it doesn't overwrite
the frames GlideStep is writing into the backbuffer. */
if (pMainWnd)
pMainWnd->bAnimating = true;
/* Draw card face into fgnd bitmap, then release it */
{
wxMemoryDC memFg;
memFg.SelectObject(m_bmFgnd);
DrawCard(memFg, 0, 0, dxCrd, dyCrd, id, FACEUP);
memFg.SelectObject(wxNullBitmap);
}
long dx = xEnd - loc.x;
long dy = yEnd - loc.y;
int distance = IntSqrt(dx*dx + dy*dy);
int steps = distance / stepsize;
if ((steps % 2) == 1)
steps++;
int x1 = loc.x;
int y1 = loc.y;
for (int i = 1; i < steps; i++)
{
int x2 = loc.x + (int)(((long)i * dx) / (long)steps);
int y2 = loc.y + (int)(((long)i * dy) / (long)steps);
GlideStep(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
}
/* Last step lands exactly on target */
GlideStep(x1, y1, xEnd, yEnd);
loc.x = xEnd;
loc.y = yEnd;
/* Animation finished. Clear bAnimating and force one more paint
so OnPaint re-renders the scene from state (card now in its new
logical position). Without this, the backbuffer would keep the
last animation frame until the next state-change-driven repaint,
which may include stale overlap artifacts. */
if (pMainWnd)
{
pMainWnd->bAnimating = false;
pMainWnd->Refresh(false);
pMainWnd->Update();
}
}
void card::GlideStep(int x1, int y1, int x2, int y2)
{
if (!pMainWnd || !pMainWnd->m_backbuffer.IsOk())
return;
/* Draw the animation frame directly into the main window's
backbuffer. OnPaint will blit the backbuffer to the screen,
so this is the only path that reliably puts pixels on the
window under wxGTK3 (where wxClientDC writes are not committed
to the backing store that the compositor reads from). */
{
wxMemoryDC dc;
dc.SelectObject(pMainWnd->m_backbuffer);
wxMemoryDC memB, memB2;
memB.SelectObject(m_bmBgnd);
memB2.SelectObject(m_bmBgnd2);
/* Create background of new location */
memB2.Blit(0, 0, dxCrd, dyCrd, &dc, x2, y2);
memB2.Blit(x1-x2, y1-y2, dxCrd, dyCrd, &memB, 0, 0);
/* Draw old background where card was */
dc.Blit(x1, y1, dxCrd, dyCrd, &memB, 0, 0);
/* Draw card at new position */
wxMemoryDC fgDC;
fgDC.SelectObject(m_bmFgnd);
dc.Blit(x2, y2, dxCrd, dyCrd, &fgDC, 0, 0);
fgDC.SelectObject(wxNullBitmap);
memB.SelectObject(wxNullBitmap);
memB2.SelectObject(wxNullBitmap);
dc.SelectObject(wxNullBitmap);
}
/* Flush the backbuffer to screen via OnPaint. bAnimating is
true, so OnPaint skips re-rendering the scene and just blits
the backbuffer we just wrote to. Refresh only the rectangle
covering both old and new card positions to minimise work. */
int xMin = (x1 < x2) ? x1 : x2;
int yMin = (y1 < y2) ? y1 : y2;
int xMax = ((x1 > x2) ? x1 : x2) + dxCrd;
int yMax = ((y1 > y2) ? y1 : y2) + dyCrd;
wxRect rect(xMin, yMin, xMax - xMin, yMax - yMin);
pMainWnd->RefreshLogicalRect(rect, false);
pMainWnd->Update();
/* Swap backgrounds */
wxBitmap temp = m_bmBgnd;
m_bmBgnd = m_bmBgnd2;
m_bmBgnd2 = temp;
}
int card::IntSqrt(long square)
{
long lastguess = square;
long guess = std::min(square / 2L, 1024L);
if (guess == 0)
return 0;
while (labs(guess - lastguess) > 3L)
{
lastguess = guess;
guess -= ((guess * guess) - square) / (2L * guess);
}
return (int)guess;
}
wxRect &card::GetRect(wxRect &rect)
{
rect = wxRect(loc.x, loc.y, dxCrd, dyCrd);
return rect;
}