| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551 |
- /////////////////////////////////////////////////////////////////////////////
- // Name: card.cpp
- // Purpose: Forty Thieves patience game
- // Author: Chris Breeze
- // Modified by:
- // Created: 21/07/97
- // Copyright: (c) 1993-1998 Chris Breeze
- // Licence: wxWindows licence
- //---------------------------------------------------------------------------
- // Last modified: 22nd July 1998 - ported to wxWidgets 2.0
- /////////////////////////////////////////////////////////////////////////////
- //+-------------------------------------------------------------+
- //| Description
- //| A class for drawing playing cards.
- //| Currently assumes that the card symbols have been
- //| loaded into hbmap_symbols and the pictures for the
- //| Jack, Queen and King have been loaded into
- //| hbmap_pictures.
- //+-------------------------------------------------------------+
- // For compilers that support precompilation, includes "wx/wx.h".
- #include "wx/wxprec.h"
- #ifdef __BORLANDC__
- #pragma hdrstop
- #endif
- #ifndef WX_PRECOMP
- #include "wx/wx.h"
- #endif
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include "forty.h"
- #include "card.h"
- #include "pictures.xpm"
- #include "symbols.xpm"
- wxBitmap* Card::m_pictureBmap = 0;
- wxBitmap* Card::m_symbolBmap = 0;
- double Card::m_scale = 1.0;
- int Card::m_width = 50;
- int Card::m_height = 70;
- //+-------------------------------------------------------------+
- //| Card::Card() |
- //+-------------------------------------------------------------+
- //| Description: |
- //| Constructor for a playing card. |
- //| Checks that the value is in the range 1..52 and then |
- //| initialises the suit, colour, pipValue and wayUp. |
- //+-------------------------------------------------------------+
- Card::Card(int value, WayUp way_up) :
- m_wayUp(way_up)
- {
- if (!m_symbolBmap)
- {
- m_symbolBmap = new wxBitmap(symbols_xpm);
- if (!m_symbolBmap->IsOk())
- {
- ::wxMessageBox(wxT("Failed to load bitmap CardSymbols"), wxT("Error"));
- }
- }
- if (!m_pictureBmap)
- {
- m_pictureBmap = new wxBitmap(Pictures);
- if (!m_pictureBmap->IsOk())
- {
- ::wxMessageBox(wxT("Failed to load bitmap CardPictures"), wxT("Error"));
- }
- }
- if (value >= 1 && value <= PackSize)
- {
- switch ((value - 1) / 13)
- {
- case 0:
- m_suit = clubs;
- m_colour = black;
- break;
- case 1:
- m_suit = diamonds;
- m_colour = red;
- break;
- case 2:
- m_suit = hearts;
- m_colour = red;
- break;
- case 3:
- m_suit = spades;
- m_colour = black;
- break;
- }
- m_pipValue = 1 + (value - 1) % 13;
- m_status = true;
- }
- else
- {
- m_status = false;
- }
- } // Card::Card()
- //+-------------------------------------------------------------+
- //| Card::SetScale() |
- //+-------------------------------------------------------------+
- //| Description: |
- //| Scales the cards |
- //+-------------------------------------------------------------+
- void Card::SetScale(double scale)
- {
- m_scale = scale;
- m_width = int(50*scale);
- m_height = int(70*scale);
- }
- //+-------------------------------------------------------------+
- //| Card::Erase() |
- //+-------------------------------------------------------------+
- //| Description: |
- //| Erase the card at (x, y) by drawing a rectangle in the |
- //| background colour. |
- //+-------------------------------------------------------------+
- void Card::Erase(wxDC& dc, int x, int y)
- {
- wxPen* pen = wxThePenList->FindOrCreatePen(
- FortyApp::BackgroundColour(),
- 1,
- wxSOLID
- );
- dc.SetPen(* pen);
- dc.SetBrush(FortyApp::BackgroundBrush());
- dc.DrawRectangle(x, y, m_width, m_height);
- } // Card::Erase()
- //+-------------------------------------------------------------+
- //| Card::Draw() |
- //+-------------------------------------------------------------+
- //| Description: |
- //| Draw the card at (x, y). |
- //| If the card is facedown draw the back of the card. |
- //| If the card is faceup draw the front of the card. |
- //| Cards are not held in bitmaps, instead they are drawn |
- //| from their constituent parts when required. |
- //| hbmap_symbols contains large and small suit symbols and |
- //| pip values. These are copied to the appropriate part of |
- //| the card. Picture cards use the pictures defined in |
- //| hbmap_pictures. Note that only one picture is defined |
- //| for the Jack, Queen and King, unlike a real pack where |
- //| each suit is different. |
- //| |
- //| WARNING: |
- //| The locations of these symbols is 'hard-wired' into the |
- //| code. Editing the bitmaps or the numbers below will |
- //| result in the wrong symbols being displayed. |
- //+-------------------------------------------------------------+
- void Card::Draw(wxDC& dc, int x, int y)
- {
- wxBrush backgroundBrush( dc.GetBackground() );
- dc.SetBrush(* wxWHITE_BRUSH);
- dc.SetPen(* wxBLACK_PEN);
- dc.DrawRoundedRectangle(x, y, m_width, m_height, 4);
- if (m_wayUp == facedown)
- {
- dc.SetBackground(* wxRED_BRUSH);
- dc.SetBackgroundMode(wxSOLID);
- wxBrush* brush = wxTheBrushList->FindOrCreateBrush(
- *wxBLACK, wxCROSSDIAG_HATCH
- );
- dc.SetBrush(* brush);
- dc.DrawRoundedRectangle(
- x + 4, y + 4,
- m_width - 8, m_height - 8,
- 2
- );
- }
- else
- {
- wxMemoryDC memoryDC;
- memoryDC.SelectObject(*m_symbolBmap);
- // dc.SetBackgroundMode(wxTRANSPARENT);
- dc.SetTextBackground(*wxWHITE);
- switch (m_suit)
- {
- case spades:
- case clubs:
- dc.SetTextForeground(*wxBLACK);
- break;
- case diamonds:
- case hearts:
- dc.SetTextForeground(*wxRED);
- break;
- }
- int symsize = 11;
- int sympos = 14;
- int sympos2 = 25;
- int symdist = 5;
- int symdist2 = 6;
- int pipsize,pippos,valueheight,valuewidth;
- int valuepos;
- if (m_scale > 1.2)
- {
- pipsize = symsize;
- pippos = sympos;
- valueheight = 10;
- valuewidth = 9;
- valuepos = 50;
- }
- else
- {
- pipsize = 7;
- pippos = 0;
- valueheight = 7;
- valuewidth = 6;
- valuepos = 36;
- }
- // Draw the value
- dc.Blit((wxCoord)(x + m_scale*3),
- (wxCoord)(y + m_scale*3),
- valuewidth,
- valueheight,
- &memoryDC,
- valuewidth * (m_pipValue - 1),
- valuepos,
- wxCOPY);
- dc.Blit((wxCoord)(x + m_width - m_scale*3 - valuewidth),
- (wxCoord)(y + m_height - valueheight - m_scale*3),
- valuewidth,
- valueheight,
- &memoryDC,
- valuewidth * (m_pipValue - 1),
- valuepos+valueheight,
- wxCOPY);
- // Draw the pips
- dc.Blit((wxCoord)(x + m_scale*3 + valuewidth+2),
- (wxCoord)(y + m_scale*3),
- pipsize,
- pipsize,
- &memoryDC,
- pipsize * m_suit,
- pippos,
- wxCOPY);
- dc.Blit((wxCoord)(x + m_width - m_scale*3-valuewidth-pipsize-2),
- (wxCoord)(y + m_height - pipsize - m_scale*3),
- pipsize,
- pipsize,
- &memoryDC,
- pipsize * m_suit,
- pipsize+pippos,
- wxCOPY);
- switch (m_pipValue)
- {
- case 1:
- dc.Blit((wxCoord)(x - symdist + m_width / 2),
- (wxCoord)(y - m_scale*5 + m_height / 2),
- symsize,
- symsize,
- &memoryDC,
- symsize * m_suit,
- sympos,
- wxCOPY);
- break;
- case 3:
- dc.Blit((wxCoord)(x - symdist + m_width / 2),
- (wxCoord)(y - symdist + m_height / 2),
- symsize,
- symsize,
- &memoryDC,
- symsize * m_suit,
- sympos,
- wxCOPY);
- case 2:
- dc.Blit((wxCoord)(x - symdist + m_width / 2),
- (wxCoord)(y - symdist + m_height / 4),
- symsize,
- symsize,
- &memoryDC,
- symsize * m_suit,
- sympos,
- wxCOPY);
- dc.Blit((wxCoord)(x - symdist + m_width / 2),
- (wxCoord)(y - symdist + 3 * m_height / 4),
- symsize,
- symsize,
- &memoryDC,
- symsize * m_suit,
- sympos2,
- wxCOPY);
- break;
- case 5:
- dc.Blit((wxCoord)(x - symdist + m_width / 2),
- (wxCoord)(y - symdist + m_height / 2),
- symsize,
- symsize,
- &memoryDC,
- symsize * m_suit,
- sympos,
- wxCOPY);
- case 4:
- dc.Blit((wxCoord)(x - symdist + m_width / 4),
- (wxCoord)(y - symdist + m_height / 4),
- symsize,
- symsize,
- &memoryDC,
- symsize * m_suit,
- sympos,
- wxCOPY);
- dc.Blit((wxCoord)(x - symdist + m_width / 4),
- (wxCoord)(y - symdist + 3 * m_height / 4),
- symsize,
- symsize,
- &memoryDC,
- symsize * m_suit,
- sympos2,
- wxCOPY);
- dc.Blit((wxCoord)(x - symdist + 3 * m_width / 4),
- (wxCoord)(y - symdist + m_height / 4),
- symsize,
- symsize,
- &memoryDC,
- symsize * m_suit,
- sympos,
- wxCOPY);
- dc.Blit((wxCoord)(x - symdist + 3 * m_width / 4),
- (wxCoord)(y - symdist + 3 * m_height / 4),
- symsize,
- symsize,
- &memoryDC,
- symsize * m_suit,
- sympos2,
- wxCOPY);
- break;
- case 8:
- dc.Blit((wxCoord)(x - symdist + 5 * m_width / 10),
- (wxCoord)(y - symdist + 5 * m_height / 8),
- symsize,
- symsize,
- &memoryDC,
- symsize * m_suit,
- sympos2,
- wxCOPY);
- case 7:
- dc.Blit((wxCoord)(x - symdist + 5 * m_width / 10),
- (wxCoord)(y - symdist + 3 * m_height / 8),
- symsize,
- symsize,
- &memoryDC,
- symsize * m_suit,
- sympos,
- wxCOPY);
- case 6:
- dc.Blit((wxCoord)(x - symdist + m_width / 4),
- (wxCoord)(y - symdist + m_height / 4),
- symsize,
- symsize,
- &memoryDC, symsize * m_suit, sympos, wxCOPY);
- dc.Blit((wxCoord)(x - symdist + m_width / 4),
- (wxCoord)(y - symdist + m_height / 2),
- symsize,
- symsize,
- &memoryDC,
- symsize * m_suit,
- sympos,
- wxCOPY);
- dc.Blit((wxCoord)(x - symdist + m_width / 4),
- (wxCoord)(y - symdist + 3 * m_height / 4),
- symsize,
- symsize,
- &memoryDC,
- symsize * m_suit,
- sympos2,
- wxCOPY);
- dc.Blit((wxCoord)(x - symdist + 3 * m_width / 4),
- (wxCoord)(y - symdist + m_height / 4),
- symsize,
- symsize,
- &memoryDC,
- symsize * m_suit,
- sympos,
- wxCOPY);
- dc.Blit((wxCoord)(x - symdist + 3 * m_width / 4),
- (wxCoord)(y - symdist + m_height / 2),
- symsize,
- symsize,
- &memoryDC,
- symsize * m_suit,
- sympos,
- wxCOPY);
- dc.Blit((wxCoord)(x - symdist + 3 * m_width / 4),
- (wxCoord)(y - symdist + 3 * m_height / 4),
- symsize,
- symsize,
- &memoryDC,
- symsize * m_suit,
- sympos2,
- wxCOPY);
- break;
- case 10:
- dc.Blit((wxCoord)(x - symdist + m_width / 2),
- (wxCoord)(y - symdist + 2 * m_height / 3),
- symsize,
- symsize,
- &memoryDC,
- symsize * m_suit,
- sympos2,
- wxCOPY);
- case 9:
- dc.Blit((wxCoord)(x - symdist + m_width / 4),
- (wxCoord)(y - symdist2 + m_height / 4),
- symsize,
- symsize,
- &memoryDC,
- symsize * m_suit,
- sympos,
- wxCOPY);
- dc.Blit((wxCoord)(x - symdist + m_width / 4),
- (wxCoord)(y - symdist2 + 5 * m_height / 12),
- symsize,
- symsize,
- &memoryDC,
- symsize * m_suit,
- sympos,
- wxCOPY);
- dc.Blit((wxCoord)(x - symdist + m_width / 4),
- (wxCoord)(y - symdist + 7 * m_height / 12),
- symsize,
- symsize,
- &memoryDC,
- symsize * m_suit,
- sympos2,
- wxCOPY);
- dc.Blit((wxCoord)(x - symdist + m_width / 4),
- (wxCoord)(y - symdist + 3 * m_height / 4),
- symsize,
- symsize,
- &memoryDC,
- symsize * m_suit,
- sympos2,
- wxCOPY);
- dc.Blit((wxCoord)(x - symdist + 3 * m_width / 4),
- (wxCoord)(y - symdist2 + m_height / 4),
- symsize,
- symsize,
- &memoryDC,
- symsize * m_suit,
- sympos,
- wxCOPY);
- dc.Blit((wxCoord)(x - symdist + 3 * m_width / 4),
- (wxCoord)(y - symdist2 + 5 * m_height / 12),
- symsize,
- symsize,
- &memoryDC,
- symsize * m_suit,
- sympos,
- wxCOPY);
- dc.Blit((wxCoord)(x - symdist + 3 * m_width / 4),
- (wxCoord)(y - symdist + 7 * m_height / 12),
- symsize,
- symsize,
- &memoryDC,
- symsize * m_suit,
- sympos2,
- wxCOPY);
- dc.Blit((wxCoord)(x - symdist + 3 * m_width / 4),
- (wxCoord)(y - symdist + 3 * m_height / 4),
- symsize,
- symsize,
- &memoryDC,
- symsize * m_suit,
- sympos2,
- wxCOPY);
- dc.Blit((wxCoord)(x - symdist + m_width / 2),
- (wxCoord)(y - symdist + m_height / 3),
- symsize,
- symsize,
- &memoryDC,
- symsize * m_suit,
- sympos,
- wxCOPY);
- break;
- case 11:
- case 12:
- case 13:
- memoryDC.SelectObject(*m_pictureBmap);
- int picwidth = 40,picheight = 45;
- dc.Blit((wxCoord)(x + (m_width-picwidth)/2),
- (wxCoord)(y - picheight/2 + m_height/2),
- picwidth,
- picheight,
- &memoryDC,
- picwidth * (m_pipValue - 11),
- 0,
- wxCOPY);
- memoryDC.SelectObject(*m_symbolBmap);
- dc.Blit((wxCoord)(x + m_width-(m_width-picwidth)/2-symsize-3),
- (wxCoord)(y - picheight/2+m_height/2+1),
- symsize,
- symsize,
- &memoryDC,
- symsize * m_suit,
- sympos,
- wxCOPY);
- dc.Blit((wxCoord)(x + (m_width-picwidth)/2+2),
- (wxCoord)(y + picheight/2 + m_height/2-symsize),
- symsize,
- symsize,
- &memoryDC,
- symsize * m_suit,
- sympos2,
- wxCOPY);
- break;
- }
- }
- dc.SetBackground( backgroundBrush );
- } // Card:Draw()
- //+-------------------------------------------------------------+
- //| Card::DrawNullCard() |
- //+-------------------------------------------------------------+
- //| Description: |
- //| Draws the outline of a card at (x, y). |
- //| Used to draw place holders for empty piles of cards. |
- //+-------------------------------------------------------------+
- void Card::DrawNullCard(wxDC& dc, int x, int y)
- {
- wxPen* pen = wxThePenList->FindOrCreatePen(FortyApp::TextColour(), 1, wxSOLID);
- dc.SetBrush(FortyApp::BackgroundBrush());
- dc.SetPen(*pen);
- dc.DrawRoundedRectangle(x, y, m_width, m_height, 4);
- } // Card::DrawNullCard()
|