///////////////////////////////////////////////////////////////////////////// // Name: suit.cpp // tag: A class to draw and manipulate a card. // Author: David Roundy // Created: 3/2003 // Copyright: (c) 2003 David Roundy // Licence: GPL /* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ // For compilers that support precompilation, includes . #include #ifndef WX_PRECOMP #include #include #endif #include "colors.h" #include "suit.h" static wxImage anti_alias(wxImage &im) { // Dimensions of im must be 4*w x 4*h; unsigned char *data = im.GetData(); int h = im.GetWidth() >> 2, w = im.GetHeight() >> 2; wxImage tm(h, w); unsigned char *newdata = (unsigned char *)malloc(h*w*3); for (int i=0;i> 4; newdata[(i + j*h)*3+1] = green >> 4; newdata[(i + j*h)*3+2] = blue >> 4; } } tm.SetData(newdata); return tm; } void DrawNumber(wxDC &dc, const wxString &num, bool is_red, bool usd, int x, int y, int w, int h) { dc.SetBackgroundMode(wxTRANSPARENT); wxBitmap weemap(w, 2*h); // Leave room for drooping letters. wxMemoryDC temp_dc; temp_dc.SelectObject(weemap); if (usd) temp_dc.Blit(0,0,w,2*h, &dc, x, y-h); else temp_dc.Blit(0,0,w,2*h, &dc, x, y); wxImage myim(weemap); myim.Rescale(4*w, 4*2*h); if (usd) myim = myim.Rotate90().Rotate90(); wxBitmap bigmap = myim.ConvertToBitmap(); temp_dc.SelectObject(bigmap); if (is_red) temp_dc.SetTextForeground( *wxRED ); else temp_dc.SetTextForeground( *wxBLACK ); int i = 7, textw, texth, des,lead; do { wxFont *f = wxTheFontList->FindOrCreateFont(++i, wxSWISS, wxNORMAL, wxNORMAL); temp_dc.SetFont(*f); temp_dc.GetTextExtent(num, &textw, &texth, &des, &lead); texth -= (des + lead); } while (textw <= 4*w && texth <= 4*h); wxFont *f = wxTheFontList->FindOrCreateFont(--i, wxSWISS, wxNORMAL, wxNORMAL); temp_dc.SetFont(*f); temp_dc.DrawText(num, 0, -lead); wxImage tm_im(bigmap); if (usd) tm_im = tm_im.Rotate90().Rotate90(); wxBitmap aabm = anti_alias(tm_im).ConvertToBitmap(); wxMemoryDC aadc; aadc.SelectObject(aabm); if (usd) dc.Blit(x,y-h, w, 2*h, &aadc, 0, 0, wxAND); else dc.Blit(x,y, w, 2*h, &aadc, 0, 0, wxAND); } void DrawSuit(wxDC &dc, Suit m_suit, int usd, int x, int y, int w, int h) { dc.SetBackgroundMode(wxTRANSPARENT); wxBitmap weemap(w, h); wxMemoryDC temp_dc; temp_dc.SelectObject(weemap); temp_dc.Blit(0,0,w,h, &dc, x, y); wxImage myim(weemap); myim.Rescale(4*w, 4*h); wxBitmap bigmap = myim.ConvertToBitmap(); temp_dc.SelectObject(bigmap); DrawSuitOnBM(temp_dc, m_suit, 4*w, 4*h); wxImage tm_im(bigmap); if (usd) tm_im.Mirror(false); wxBitmap aabm = anti_alias(tm_im).ConvertToBitmap(); wxMemoryDC aadc; aadc.SelectObject(aabm); dc.Blit(x,y, w, h, &aadc, 0, 0, wxCOPY); } void DrawSuitOnBM(wxDC &dc, Suit m_suit, int w, int h) { dc.SetUserScale(w/32.0,h/32.0); int d; wxPoint dia[] = {wxPoint(16,0),wxPoint(26,16), wxPoint(16,32),wxPoint(6,16) }; wxPoint hea[] = {wxPoint(0,8),wxPoint(32,8), wxPoint(16,32) }; wxPoint spa[] = {wxPoint(0,18),wxPoint(16,0), wxPoint(32,18),wxPoint(17,18), wxPoint(24,32),wxPoint(8,32), wxPoint(15,18) }; switch (m_suit) { case clubs: dc.SetBrush(* wxBLACK_BRUSH); dc.SetPen(* wxTRANSPARENT_PEN); dc.DrawEllipse(9,0,13,13); dc.DrawEllipse(0 ,14,13,13); dc.DrawEllipse(32-13,14,13,13); dc.SetPen(* wxBLACK_PEN); dc.DrawRectangle(15,0,2,32); dc.DrawRectangle(0,20,32,2); dc.DrawRectangle(8,30,16,2); break; case diamonds: dc.SetBrush(* wxRED_BRUSH); dc.SetPen(* wxTRANSPARENT_PEN); dc.DrawPolygon(4, dia); break; case hearts: dc.SetBrush(* wxRED_BRUSH); dc.SetPen(* wxTRANSPARENT_PEN); dc.DrawEllipse(16,0,16,16); dc.DrawEllipse(0,0,16,16); dc.DrawPolygon(3, hea); break; case spades: dc.SetBrush(* wxBLACK_BRUSH); dc.SetPen(* wxTRANSPARENT_PEN); dc.DrawEllipse(16,10,16,16); dc.DrawEllipse(0,10,16,16); dc.DrawPolygon(7, spa); break; } }