]> git.saurik.com Git - wxWidgets.git/blame - demos/forty/card.cpp
avoid creating and immediately destroying a wxGraphicsContext when creating a wxDC...
[wxWidgets.git] / demos / forty / card.cpp
CommitLineData
63cafd27
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: card.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//+-------------------------------------------------------------+
e3e65dac 14//| Description
010216e3
WS
15//| A class for drawing playing cards.
16//| Currently assumes that the card symbols have been
17//| loaded into hbmap_symbols and the pictures for the
18//| Jack, Queen and King have been loaded into
19//| hbmap_pictures.
63cafd27
JS
20//+-------------------------------------------------------------+
21
63cafd27
JS
22// For compilers that support precompilation, includes "wx/wx.h".
23#include "wx/wxprec.h"
24
25#ifdef __BORLANDC__
26#pragma hdrstop
27#endif
28
29#ifndef WX_PRECOMP
30#include "wx/wx.h"
31#endif
32
33#include <stdlib.h>
34#include <stdio.h>
35#include <string.h>
36#include "forty.h"
37#include "card.h"
38
63cafd27 39#include "pictures.xpm"
91feb0d1 40#include "symbols.xpm"
63cafd27
JS
41
42wxBitmap* Card::m_pictureBmap = 0;
43wxBitmap* Card::m_symbolBmap = 0;
44
fc799548
JS
45double Card::m_scale = 1.0;
46int Card::m_width = 50;
47int Card::m_height = 70;
63cafd27
JS
48
49//+-------------------------------------------------------------+
010216e3 50//| Card::Card() |
63cafd27 51//+-------------------------------------------------------------+
010216e3
WS
52//| Description: |
53//| Constructor for a playing card. |
54//| Checks that the value is in the range 1..52 and then |
55//| initialises the suit, colour, pipValue and wayUp. |
63cafd27
JS
56//+-------------------------------------------------------------+
57Card::Card(int value, WayUp way_up) :
010216e3 58 m_wayUp(way_up)
63cafd27 59{
010216e3
WS
60 if (!m_symbolBmap)
61 {
91feb0d1 62 m_symbolBmap = new wxBitmap(symbols_xpm);
a1b806b9 63 if (!m_symbolBmap->IsOk())
010216e3 64 {
9a83f860 65 ::wxMessageBox(wxT("Failed to load bitmap CardSymbols"), wxT("Error"));
010216e3
WS
66 }
67 }
68 if (!m_pictureBmap)
69 {
010216e3 70 m_pictureBmap = new wxBitmap(Pictures);
a1b806b9 71 if (!m_pictureBmap->IsOk())
010216e3 72 {
9a83f860 73 ::wxMessageBox(wxT("Failed to load bitmap CardPictures"), wxT("Error"));
010216e3
WS
74 }
75 }
63cafd27
JS
76
77 if (value >= 1 && value <= PackSize)
78 {
010216e3
WS
79 switch ((value - 1) / 13)
80 {
81 case 0:
82 m_suit = clubs;
83 m_colour = black;
84 break;
85 case 1:
86 m_suit = diamonds;
87 m_colour = red;
88 break;
89 case 2:
90 m_suit = hearts;
91 m_colour = red;
92 break;
93 case 3:
94 m_suit = spades;
95 m_colour = black;
96 break;
97 }
98 m_pipValue = 1 + (value - 1) % 13;
99 m_status = true;
63cafd27
JS
100 }
101 else
102 {
e0b5519a 103 m_status = false;
63cafd27
JS
104 }
105} // Card::Card()
106
107
fc799548 108//+-------------------------------------------------------------+
010216e3 109//| Card::SetScale() |
fc799548 110//+-------------------------------------------------------------+
010216e3
WS
111//| Description: |
112//| Scales the cards |
fc799548
JS
113//+-------------------------------------------------------------+
114void Card::SetScale(double scale)
115{
116 m_scale = scale;
117 m_width = int(50*scale);
118 m_height = int(70*scale);
119}
120
63cafd27 121//+-------------------------------------------------------------+
010216e3 122//| Card::Erase() |
63cafd27 123//+-------------------------------------------------------------+
010216e3
WS
124//| Description: |
125//| Erase the card at (x, y) by drawing a rectangle in the |
126//| background colour. |
63cafd27
JS
127//+-------------------------------------------------------------+
128void Card::Erase(wxDC& dc, int x, int y)
129{
010216e3
WS
130 wxPen* pen = wxThePenList->FindOrCreatePen(
131 FortyApp::BackgroundColour(),
132 1,
133 wxSOLID
134 );
135 dc.SetPen(* pen);
136 dc.SetBrush(FortyApp::BackgroundBrush());
fc799548 137 dc.DrawRectangle(x, y, m_width, m_height);
63cafd27
JS
138} // Card::Erase()
139
140
141//+-------------------------------------------------------------+
010216e3 142//| Card::Draw() |
63cafd27 143//+-------------------------------------------------------------+
010216e3
WS
144//| Description: |
145//| Draw the card at (x, y). |
146//| If the card is facedown draw the back of the card. |
147//| If the card is faceup draw the front of the card. |
148//| Cards are not held in bitmaps, instead they are drawn |
149//| from their constituent parts when required. |
150//| hbmap_symbols contains large and small suit symbols and |
151//| pip values. These are copied to the appropriate part of |
152//| the card. Picture cards use the pictures defined in |
153//| hbmap_pictures. Note that only one picture is defined |
154//| for the Jack, Queen and King, unlike a real pack where |
155//| each suit is different. |
156//| |
157//| WARNING: |
158//| The locations of these symbols is 'hard-wired' into the |
159//| code. Editing the bitmaps or the numbers below will |
160//| result in the wrong symbols being displayed. |
63cafd27
JS
161//+-------------------------------------------------------------+
162void Card::Draw(wxDC& dc, int x, int y)
163{
010216e3
WS
164 wxBrush backgroundBrush( dc.GetBackground() );
165 dc.SetBrush(* wxWHITE_BRUSH);
166 dc.SetPen(* wxBLACK_PEN);
fc799548 167 dc.DrawRoundedRectangle(x, y, m_width, m_height, 4);
010216e3
WS
168 if (m_wayUp == facedown)
169 {
170 dc.SetBackground(* wxRED_BRUSH);
171 dc.SetBackgroundMode(wxSOLID);
172 wxBrush* brush = wxTheBrushList->FindOrCreateBrush(
19bc1514 173 *wxBLACK, wxCROSSDIAG_HATCH
010216e3
WS
174 );
175 dc.SetBrush(* brush);
176
177 dc.DrawRoundedRectangle(
178 x + 4, y + 4,
179 m_width - 8, m_height - 8,
180 2
181 );
182 }
183 else
184 {
185 wxMemoryDC memoryDC;
186
187 memoryDC.SelectObject(*m_symbolBmap);
188
189// dc.SetBackgroundMode(wxTRANSPARENT);
190
191 dc.SetTextBackground(*wxWHITE);
192 switch (m_suit)
193 {
194 case spades:
195 case clubs:
196 dc.SetTextForeground(*wxBLACK);
197 break;
198 case diamonds:
199 case hearts:
200 dc.SetTextForeground(*wxRED);
201 break;
202 }
203
204 int symsize = 11;
205 int sympos = 14;
206 int sympos2 = 25;
207 int symdist = 5;
208 int symdist2 = 6;
209
210 int pipsize,pippos,valueheight,valuewidth;
211 int valuepos;
212 if (m_scale > 1.2)
213 {
214 pipsize = symsize;
215 pippos = sympos;
216 valueheight = 10;
217 valuewidth = 9;
218 valuepos = 50;
219 }
220 else
221 {
222 pipsize = 7;
223 pippos = 0;
224 valueheight = 7;
225 valuewidth = 6;
226 valuepos = 36;
227 }
228
229 // Draw the value
230 dc.Blit((wxCoord)(x + m_scale*3),
231 (wxCoord)(y + m_scale*3),
254a2129 232 valuewidth,
010216e3 233 valueheight,
254a2129
WS
234 &memoryDC,
235 valuewidth * (m_pipValue - 1),
236 valuepos,
010216e3 237 wxCOPY);
254a2129 238 dc.Blit((wxCoord)(x + m_width - m_scale*3 - valuewidth),
010216e3 239 (wxCoord)(y + m_height - valueheight - m_scale*3),
254a2129 240 valuewidth,
010216e3 241 valueheight,
254a2129
WS
242 &memoryDC,
243 valuewidth * (m_pipValue - 1),
244 valuepos+valueheight,
010216e3
WS
245 wxCOPY);
246
247 // Draw the pips
254a2129
WS
248 dc.Blit((wxCoord)(x + m_scale*3 + valuewidth+2),
249 (wxCoord)(y + m_scale*3),
010216e3 250 pipsize,
254a2129
WS
251 pipsize,
252 &memoryDC,
253 pipsize * m_suit,
254 pippos,
010216e3 255 wxCOPY);
254a2129 256 dc.Blit((wxCoord)(x + m_width - m_scale*3-valuewidth-pipsize-2),
010216e3 257 (wxCoord)(y + m_height - pipsize - m_scale*3),
010216e3 258 pipsize,
254a2129
WS
259 pipsize,
260 &memoryDC,
261 pipsize * m_suit,
262 pipsize+pippos,
010216e3
WS
263 wxCOPY);
264
265 switch (m_pipValue)
266 {
267 case 1:
254a2129
WS
268 dc.Blit((wxCoord)(x - symdist + m_width / 2),
269 (wxCoord)(y - m_scale*5 + m_height / 2),
270 symsize,
010216e3 271 symsize,
254a2129
WS
272 &memoryDC,
273 symsize * m_suit,
274 sympos,
010216e3
WS
275 wxCOPY);
276 break;
277
278 case 3:
254a2129
WS
279 dc.Blit((wxCoord)(x - symdist + m_width / 2),
280 (wxCoord)(y - symdist + m_height / 2),
010216e3 281 symsize,
254a2129
WS
282 symsize,
283 &memoryDC,
284 symsize * m_suit,
285 sympos,
010216e3
WS
286 wxCOPY);
287 case 2:
288 dc.Blit((wxCoord)(x - symdist + m_width / 2),
254a2129
WS
289 (wxCoord)(y - symdist + m_height / 4),
290 symsize,
010216e3 291 symsize,
254a2129
WS
292 &memoryDC,
293 symsize * m_suit,
294 sympos,
010216e3
WS
295 wxCOPY);
296 dc.Blit((wxCoord)(x - symdist + m_width / 2),
254a2129
WS
297 (wxCoord)(y - symdist + 3 * m_height / 4),
298 symsize,
010216e3 299 symsize,
254a2129
WS
300 &memoryDC,
301 symsize * m_suit,
302 sympos2,
010216e3
WS
303 wxCOPY);
304 break;
305
306 case 5:
254a2129
WS
307 dc.Blit((wxCoord)(x - symdist + m_width / 2),
308 (wxCoord)(y - symdist + m_height / 2),
309 symsize,
010216e3 310 symsize,
254a2129
WS
311 &memoryDC,
312 symsize * m_suit,
313 sympos,
010216e3
WS
314 wxCOPY);
315 case 4:
316 dc.Blit((wxCoord)(x - symdist + m_width / 4),
254a2129
WS
317 (wxCoord)(y - symdist + m_height / 4),
318 symsize,
010216e3 319 symsize,
254a2129
WS
320 &memoryDC,
321 symsize * m_suit,
322 sympos,
010216e3
WS
323 wxCOPY);
324 dc.Blit((wxCoord)(x - symdist + m_width / 4),
254a2129 325 (wxCoord)(y - symdist + 3 * m_height / 4),
010216e3 326 symsize,
254a2129
WS
327 symsize,
328 &memoryDC,
329 symsize * m_suit,
330 sympos2,
010216e3
WS
331 wxCOPY);
332 dc.Blit((wxCoord)(x - symdist + 3 * m_width / 4),
254a2129
WS
333 (wxCoord)(y - symdist + m_height / 4),
334 symsize,
010216e3 335 symsize,
254a2129
WS
336 &memoryDC,
337 symsize * m_suit,
338 sympos,
010216e3
WS
339 wxCOPY);
340 dc.Blit((wxCoord)(x - symdist + 3 * m_width / 4),
254a2129 341 (wxCoord)(y - symdist + 3 * m_height / 4),
010216e3 342 symsize,
254a2129
WS
343 symsize,
344 &memoryDC,
345 symsize * m_suit,
346 sympos2,
010216e3
WS
347 wxCOPY);
348 break;
349
350 case 8:
351 dc.Blit((wxCoord)(x - symdist + 5 * m_width / 10),
254a2129
WS
352 (wxCoord)(y - symdist + 5 * m_height / 8),
353 symsize,
010216e3 354 symsize,
254a2129
WS
355 &memoryDC,
356 symsize * m_suit,
357 sympos2,
010216e3
WS
358 wxCOPY);
359 case 7:
360 dc.Blit((wxCoord)(x - symdist + 5 * m_width / 10),
254a2129
WS
361 (wxCoord)(y - symdist + 3 * m_height / 8),
362 symsize,
010216e3 363 symsize,
254a2129
WS
364 &memoryDC,
365 symsize * m_suit,
366 sympos,
010216e3
WS
367 wxCOPY);
368 case 6:
369 dc.Blit((wxCoord)(x - symdist + m_width / 4),
254a2129
WS
370 (wxCoord)(y - symdist + m_height / 4),
371 symsize,
010216e3
WS
372 symsize,
373 &memoryDC, symsize * m_suit, sympos, wxCOPY);
374 dc.Blit((wxCoord)(x - symdist + m_width / 4),
254a2129
WS
375 (wxCoord)(y - symdist + m_height / 2),
376 symsize,
010216e3 377 symsize,
254a2129
WS
378 &memoryDC,
379 symsize * m_suit,
380 sympos,
010216e3
WS
381 wxCOPY);
382 dc.Blit((wxCoord)(x - symdist + m_width / 4),
254a2129
WS
383 (wxCoord)(y - symdist + 3 * m_height / 4),
384 symsize,
010216e3 385 symsize,
254a2129
WS
386 &memoryDC,
387 symsize * m_suit,
388 sympos2,
010216e3
WS
389 wxCOPY);
390 dc.Blit((wxCoord)(x - symdist + 3 * m_width / 4),
254a2129 391 (wxCoord)(y - symdist + m_height / 4),
010216e3 392 symsize,
254a2129
WS
393 symsize,
394 &memoryDC,
395 symsize * m_suit,
396 sympos,
010216e3
WS
397 wxCOPY);
398 dc.Blit((wxCoord)(x - symdist + 3 * m_width / 4),
254a2129
WS
399 (wxCoord)(y - symdist + m_height / 2),
400 symsize,
010216e3 401 symsize,
254a2129
WS
402 &memoryDC,
403 symsize * m_suit,
404 sympos,
010216e3
WS
405 wxCOPY);
406 dc.Blit((wxCoord)(x - symdist + 3 * m_width / 4),
254a2129
WS
407 (wxCoord)(y - symdist + 3 * m_height / 4),
408 symsize,
010216e3 409 symsize,
254a2129
WS
410 &memoryDC,
411 symsize * m_suit,
412 sympos2,
010216e3
WS
413 wxCOPY);
414 break;
415
416 case 10:
417 dc.Blit((wxCoord)(x - symdist + m_width / 2),
254a2129 418 (wxCoord)(y - symdist + 2 * m_height / 3),
010216e3 419 symsize,
254a2129
WS
420 symsize,
421 &memoryDC,
422 symsize * m_suit,
423 sympos2,
010216e3
WS
424 wxCOPY);
425 case 9:
426 dc.Blit((wxCoord)(x - symdist + m_width / 4),
254a2129
WS
427 (wxCoord)(y - symdist2 + m_height / 4),
428 symsize,
010216e3 429 symsize,
254a2129
WS
430 &memoryDC,
431 symsize * m_suit,
432 sympos,
010216e3
WS
433 wxCOPY);
434 dc.Blit((wxCoord)(x - symdist + m_width / 4),
254a2129 435 (wxCoord)(y - symdist2 + 5 * m_height / 12),
010216e3 436 symsize,
254a2129
WS
437 symsize,
438 &memoryDC,
439 symsize * m_suit,
440 sympos,
010216e3
WS
441 wxCOPY);
442 dc.Blit((wxCoord)(x - symdist + m_width / 4),
254a2129
WS
443 (wxCoord)(y - symdist + 7 * m_height / 12),
444 symsize,
010216e3 445 symsize,
254a2129
WS
446 &memoryDC,
447 symsize * m_suit,
448 sympos2,
010216e3
WS
449 wxCOPY);
450 dc.Blit((wxCoord)(x - symdist + m_width / 4),
254a2129
WS
451 (wxCoord)(y - symdist + 3 * m_height / 4),
452 symsize,
010216e3 453 symsize,
254a2129
WS
454 &memoryDC,
455 symsize * m_suit,
456 sympos2,
010216e3
WS
457 wxCOPY);
458
459 dc.Blit((wxCoord)(x - symdist + 3 * m_width / 4),
254a2129 460 (wxCoord)(y - symdist2 + m_height / 4),
010216e3 461 symsize,
254a2129
WS
462 symsize,
463 &memoryDC,
464 symsize * m_suit,
465 sympos,
010216e3
WS
466 wxCOPY);
467 dc.Blit((wxCoord)(x - symdist + 3 * m_width / 4),
254a2129
WS
468 (wxCoord)(y - symdist2 + 5 * m_height / 12),
469 symsize,
010216e3 470 symsize,
254a2129
WS
471 &memoryDC,
472 symsize * m_suit,
473 sympos,
010216e3
WS
474 wxCOPY);
475 dc.Blit((wxCoord)(x - symdist + 3 * m_width / 4),
254a2129 476 (wxCoord)(y - symdist + 7 * m_height / 12),
010216e3 477 symsize,
254a2129
WS
478 symsize,
479 &memoryDC,
480 symsize * m_suit,
481 sympos2,
010216e3
WS
482 wxCOPY);
483 dc.Blit((wxCoord)(x - symdist + 3 * m_width / 4),
254a2129
WS
484 (wxCoord)(y - symdist + 3 * m_height / 4),
485 symsize,
010216e3 486 symsize,
254a2129
WS
487 &memoryDC,
488 symsize * m_suit,
489 sympos2,
010216e3
WS
490 wxCOPY);
491 dc.Blit((wxCoord)(x - symdist + m_width / 2),
254a2129
WS
492 (wxCoord)(y - symdist + m_height / 3),
493 symsize,
010216e3 494 symsize,
254a2129
WS
495 &memoryDC,
496 symsize * m_suit,
497 sympos,
010216e3
WS
498 wxCOPY);
499 break;
500 case 11:
501 case 12:
502 case 13:
503 memoryDC.SelectObject(*m_pictureBmap);
504 int picwidth = 40,picheight = 45;
254a2129 505 dc.Blit((wxCoord)(x + (m_width-picwidth)/2),
010216e3 506 (wxCoord)(y - picheight/2 + m_height/2),
254a2129 507 picwidth,
010216e3 508 picheight,
254a2129
WS
509 &memoryDC,
510 picwidth * (m_pipValue - 11),
511 0,
010216e3
WS
512 wxCOPY);
513
514 memoryDC.SelectObject(*m_symbolBmap);
515 dc.Blit((wxCoord)(x + m_width-(m_width-picwidth)/2-symsize-3),
254a2129 516 (wxCoord)(y - picheight/2+m_height/2+1),
010216e3 517 symsize,
254a2129
WS
518 symsize,
519 &memoryDC,
520 symsize * m_suit,
521 sympos,
010216e3
WS
522 wxCOPY);
523 dc.Blit((wxCoord)(x + (m_width-picwidth)/2+2),
254a2129
WS
524 (wxCoord)(y + picheight/2 + m_height/2-symsize),
525 symsize,
010216e3 526 symsize,
254a2129
WS
527 &memoryDC,
528 symsize * m_suit,
529 sympos2,
010216e3
WS
530 wxCOPY);
531 break;
532 }
533
534 }
535 dc.SetBackground( backgroundBrush );
63cafd27
JS
536} // Card:Draw()
537
538
539//+-------------------------------------------------------------+
010216e3 540//| Card::DrawNullCard() |
63cafd27 541//+-------------------------------------------------------------+
010216e3
WS
542//| Description: |
543//| Draws the outline of a card at (x, y). |
544//| Used to draw place holders for empty piles of cards. |
63cafd27
JS
545//+-------------------------------------------------------------+
546void Card::DrawNullCard(wxDC& dc, int x, int y)
547{
010216e3
WS
548 wxPen* pen = wxThePenList->FindOrCreatePen(FortyApp::TextColour(), 1, wxSOLID);
549 dc.SetBrush(FortyApp::BackgroundBrush());
550 dc.SetPen(*pen);
551 dc.DrawRoundedRectangle(x, y, m_width, m_height, 4);
63cafd27 552} // Card::DrawNullCard()