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