-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleSprite.cpp
186 lines (162 loc) · 4.93 KB
/
SimpleSprite.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "StdAfx.h"
#include "SimpleSprite.h"
#include "Camera.h"
extern Texture* getTexture(WCHAR*);
extern Camera* mainCamInst;
//constructor that sets all sprites to zero out
SimpleSprite::SimpleSprite()
{
ZeroValues();
spriteType = NotSet;
}
//constructor that creates a sprite based on a file
SimpleSprite::SimpleSprite(ID3D10Device* dxDevice, WCHAR* filename)
{
ZeroValues();
d3ddevice = dxDevice;
SetTexture(filename);
InitSpriteInfo();
spriteType = NotSet;
}
//Sets all the values of the sprite to zero, to avoid
//uninitialized memory errors.
void SimpleSprite::ZeroValues()
{
d3ddevice = 0;
ZeroMemory(&spriteInfo, sizeof(D3DX10_SPRITE));
ZeroMemory(&myTexture, sizeof(Texture));
xPosition = -1;
yPosition = -1;
scaleX = 1.0f;
scaleY = 1.0f;
Visible = true;
}
//sets the 'spriteInfo' object's info appropriately,
//and sets the texture.
void SimpleSprite::InitSpriteInfo()
{
spriteInfo.TexCoord.x = 0.0f;
spriteInfo.TexCoord.y = 0.0f;
spriteInfo.TexSize.x = 1.0f;
spriteInfo.TexSize.y = 1.0f;
spriteInfo.ColorModulate = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
spriteInfo.pTexture = myTexture->texture;
spriteHeight = myTexture->textureHeight;
spriteWidth = myTexture->textureWidth;
spriteInfo.TextureIndex = 0;
scaleX = 1.0f;
scaleY = 1.0f;
}
//retrieves the texture associated with the given name,
//and sets it to the sprite.
void SimpleSprite::SetTexture(WCHAR* textureName)
{
myTexture = getTexture(textureName);
}
//sets the currently assigned texture, by object
void SimpleSprite::SetTexture(Texture* texture)
{
myTexture = texture;
}
//sets the current position of the sprite
void SimpleSprite::SetPosition(float x, float y, float z)
{
//ignore Z since we're not using it
//D3DXMatrixTranslation(&spriteInfo.matWorld, x, y, 0.0f);
xPosition = x;
yPosition = y;
RefreshMatrix();
}
//sets the scaling amount on the current sprite; the same value for both x and y
void SimpleSprite::SetScaling(float scale)
{
scaleX = scale;
scaleY = scale;
RefreshMatrix();
}
//sets the scaling amount on the current sprite
void SimpleSprite::SetScaling(float x, float y)
{
scaleX = x;
scaleY = y;
RefreshMatrix();
}
//multiplies the current scale by the specified amounts.
void SimpleSprite::AdjustScaling(float x, float y)
{
scaleX *= x;
scaleY *= y;
RefreshMatrix();
}
//sets the sprite's current matrix to translation and scaling
//based on the positional values, so it can be drawn.
void SimpleSprite::RefreshMatrix()
{
D3DXMATRIX transform;
if (!(spriteType == MainMenu || spriteType == LobbyMenu || spriteType == TimeMenu || spriteType == InstructMenu || spriteType == Selector))
{
float camX, camY, camXYScale;
D3DXMATRIX camTranslate, camScale, translate, scaling;
::mainCamInst->GetPosition(&camX, &camY);
camXYScale = 1.0f / ::mainCamInst->getScale();
D3DXMatrixTranslation(&translate, (-camX + xPosition) * camXYScale, (-camY + yPosition) * camXYScale, 0.0f);
D3DXMatrixScaling(&camScale, 1.0f / camXYScale, 1.0f / camXYScale, 0.0f);
//D3DXMatrixTranslation(&translate, xPosition, yPosition, 0.0f);
D3DXMatrixScaling(&scaling, scaleX * camXYScale, scaleY * camXYScale, 1.0f);
D3DXMatrixIdentity(&transform);
//D3DXMatrixMultiply(&translate, &camTranslate, &translate);
D3DXMatrixMultiply(&transform, &translate, &transform);
D3DXMatrixMultiply(&transform, &scaling, &transform);
//D3DXMatrixMultiply(&transform, &camTranslate, &transform);
}
else
{
D3DXMATRIX scaling;
// just apply scaling
D3DXMatrixTranslation(&transform, xPosition, yPosition, 0.0f);
D3DXMatrixScaling(&scaling, scaleX, scaleY, 1.0f);
D3DXMatrixMultiply(&transform, &scaling, &transform);
}
spriteInfo.matWorld = transform;
/*
D3DXMATRIX translation, scaling;
D3DXMatrixTranslation(&translation, xPosition, yPosition, 0.0f);
if (!(spriteType == MainMenu || spriteType == TimeMenu || spriteType == InstructMenu || spriteType == Selector))
{
float camX, camY;
D3DXMATRIX camTranslate;
::mainCamInst->GetPosition(&camX, &camY);
D3DXMatrixTranslation(&camTranslate, -camX, -camY, 0.0f);
D3DXMatrixMultiply(&translation, &camTranslate, &translation);
}
D3DXMatrixScaling(&scaling, scaleX, scaleY, 1.0f);
D3DXMatrixMultiply(&spriteInfo.matWorld, &scaling, &translation);
*/
}
//draws the sprite
void SimpleSprite::Draw()
{
if (myTexture != NULL && Visible)
{
RefreshMatrix();
ID3DX10Sprite* sprite = myTexture->sprite;
sprite->Begin(D3DX10_SPRITE_SORT_TEXTURE);
sprite->DrawSpritesBuffered(&spriteInfo, 1);
sprite->Flush();
sprite->End();
}
}
// Lets us set which type of sprite this is
void SimpleSprite::SetSpriteType(SpriteType type)
{
spriteType = type;
}
// Getter that lets other know what type of sprite this is
SpriteType SimpleSprite::GetSpriteType()
{
return spriteType;
}
//deconstructor
SimpleSprite::~SimpleSprite(void)
{
}