-
Notifications
You must be signed in to change notification settings - Fork 0
/
card.cpp
155 lines (130 loc) · 3.45 KB
/
card.cpp
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
// Written by Steve Coward
// PSU CS 202 Fall 2009 Project 1
//
// card.cpp: implementation of the CCard class.
//
//////////////////////////////////////////////////////////////////////
#include "DXUT.h"
#include "card.h"
#include <iostream>
// constructor
// initialize card to a reasonable value.
// the card is initially not visible to any player at the table.
CCard::CCard(void)
{
setFrontTexture(NULL);
setBackTexture(NULL);
setRank(Ace);
setSuit(Hearts);
// for debug - set to true
setFaceUp(false);
setSpriteLength(0);
setSpriteWidth(0);
setLocation(0, 0);
setFileName(L"");
return;
}
// destructor
CCard::~CCard(void)
{
// CCard does not release texture since it may be shared by other
// other cards in this deck or other decks.
return;
}
int CCard::translateCard(Rank rank, Suit suit) const
{
int index;
// Entry 0 refers to back face of the card
// Ace of Hearts = 1, King of Spades = 52
index = (suit - CCard::Hearts) * CCard::King + rank;
return(index);
}
// translate the card value to a string.
// return the number of characters written to pRank, excluding
// the terminating '\0'.
int CCard::translateCard(char* pRank, char* pSuit) const
{
int numChars;
if (getFaceUp()) {
// card is visible so print rank and suit
numChars = sprintf(pSuit, "%c", (char)getSuit());
pSuit[numChars] = '\0';
numChars = getCharRank(pRank, 3);
pRank[numChars] = '\0';
}
else {
// card is not visible so display as ###
strcpy(pSuit, "#");
numChars = 1;
pSuit[numChars] = '\0';
strcpy(pRank, "##");
numChars = 2;
pRank[numChars] = '\0';
}
return(numChars);
}
// translate the numeric rank of a card to a meaningful symbol.
// return the number of characters written to pRank, excluding
// the terminating '\0'.
int CCard::getCharRank(char* pRank, int maxLength) const
{
// assume no characters are written to pRank
int numChars = 0;
if ((getRank() >= Ace) && (getRank() <= King)) {
// rank is in range of Rank enum, proceed with translation
// most ranks require a single character
numChars = 1;
switch(getRank())
{
case Ace:
strcpy(pRank, "A");
break;
case Ten:
strcpy(pRank, "10");
// this is only rank that requires 2 characters
numChars = 2;
break;
case Jack:
strcpy(pRank, "J");
break;
case Queen:
strcpy(pRank, "Q");
break;
case King:
strcpy(pRank, "K");
break;
default:
// ranks 2 through 9
numChars = sprintf(pRank, "%d", getRank());
break;
}
}
pRank[numChars] = '\0';
return(numChars);
}
// This draw routine assumes that pSprite->Begin() called before and
// pSprite->End() is called after.
HRESULT CCard::draw(ID3DX10Sprite * pSprite, D3DX10_SPRITE* pSpriteArray) const
{
HRESULT hResult = S_OK;
D3DXMATRIXA16 matScaling;
D3DXMATRIXA16 matTranslation;
// Add one to width since right border of card is not displaying
D3DXMatrixScaling( &matScaling, (float)getSpriteWidth()+1, (float)getSpriteLength(), 1.0f );
// (0, 0) is lower left corner
D3DXMatrixTranslation( &matTranslation, (float)getLocation().x, (float)getLocation().y, 0.1f);
pSpriteArray->matWorld = matScaling * matTranslation;
pSpriteArray->TexCoord.x = 0.0f;
pSpriteArray->TexCoord.y = 0.0f;
pSpriteArray->TexSize.x = 1.0f;
pSpriteArray->TexSize.y = 1.0f;
pSpriteArray->ColorModulate = D3DXCOLOR(1.0f,1.0f,1.0f,1.0f);
if (getFaceUp()) {
pSpriteArray->pTexture = getFrontTexture();
}
else {
pSpriteArray->pTexture = getBackTexture();
}
pSpriteArray->TextureIndex = 0;
return(hResult);
}