-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprite.cpp
209 lines (149 loc) · 6.59 KB
/
sprite.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//=============================================================================
//
// スプライト処理 [sprite.cpp]
// Author :
//
//=============================================================================
#include "main.h"
#include "renderer.h"
#include "sprite.h"
//*****************************************************************************
// マクロ定義
//*****************************************************************************
//*****************************************************************************
// プロトタイプ宣言
//*****************************************************************************
//*****************************************************************************
// グローバル変数
//*****************************************************************************
//=============================================================================
// 頂点データ設定
//=============================================================================
void SetSprite(ID3D11Buffer *buf, float X, float Y, float Width, float Height, float U, float V, float UW, float VH)
{
D3D11_MAPPED_SUBRESOURCE msr;
GetDeviceContext()->Map(buf, 0, D3D11_MAP_WRITE_DISCARD, 0, &msr);
VERTEX_3D *vertex = (VERTEX_3D*)msr.pData;
float hw, hh;
hw = Width * 0.5f; // コンピューターは割り算が苦手
hh = Height * 0.5f; // 掛け算の方が処理が速い
// 指定された座標を中心に設定するプログラム
// 頂点0番(左上の頂点)
vertex[0].Position = XMFLOAT3(X - hw, Y - hh, 0.0f);
vertex[0].Diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
vertex[0].TexCoord = XMFLOAT2(U, V);
// 頂点1番(右上の頂点)
vertex[1].Position = XMFLOAT3(X + hw, Y - hh, 0.0f);
vertex[1].Diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
vertex[1].TexCoord = XMFLOAT2(U + UW, V);
// 頂点2番(左下の頂点)
vertex[2].Position = XMFLOAT3(X - hw, Y + hh, 0.0f);
vertex[2].Diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
vertex[2].TexCoord = XMFLOAT2(U, V + VH);
// 頂点3番(右下の頂点)
vertex[3].Position = XMFLOAT3(X + hw, Y + hh, 0.0f);
vertex[3].Diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
vertex[3].TexCoord = XMFLOAT2(U + UW, V + VH);
// 左上を原点として設定するプログラム
//vertex[0].Position = XMFLOAT3(X, Y, 0.0f);
//vertex[0].Diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
//vertex[0].TexCoord = D3DXVECTOR2(U, V);
//vertex[1].Position = XMFLOAT3(X + Width, Y, 0.0f);
//vertex[1].Diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
//vertex[1].TexCoord = D3DXVECTOR2(U + UW, V);
//vertex[2].Position = XMFLOAT3(X, Y + Height, 0.0f);
//vertex[2].Diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
//vertex[2].TexCoord = D3DXVECTOR2(U, V + VH);
//vertex[3].Position = XMFLOAT3(X + Width, Y + Height, 0.0f);
//vertex[3].Diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
//vertex[3].TexCoord = D3DXVECTOR2(U + UW, V + VH);
GetDeviceContext()->Unmap(buf, 0);
}
void SetSpriteLeftTop(ID3D11Buffer *buf, float X, float Y, float Width, float Height, float U, float V, float UW, float VH)
{
D3D11_MAPPED_SUBRESOURCE msr;
GetDeviceContext()->Map(buf, 0, D3D11_MAP_WRITE_DISCARD, 0, &msr);
VERTEX_3D *vertex = (VERTEX_3D*)msr.pData;
// 左上を原点として設定するプログラム
vertex[0].Position = XMFLOAT3(X, Y, 0.0f);
vertex[0].Diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
vertex[0].TexCoord = XMFLOAT2(U, V);
vertex[1].Position = XMFLOAT3(X + Width, Y, 0.0f);
vertex[1].Diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
vertex[1].TexCoord = XMFLOAT2(U + UW, V);
vertex[2].Position = XMFLOAT3(X, Y + Height, 0.0f);
vertex[2].Diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
vertex[2].TexCoord = XMFLOAT2(U, V + VH);
vertex[3].Position = XMFLOAT3(X + Width, Y + Height, 0.0f);
vertex[3].Diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
vertex[3].TexCoord = XMFLOAT2(U + UW, V + VH);
GetDeviceContext()->Unmap(buf, 0);
}
void SetSpriteColor(ID3D11Buffer *buf, float X, float Y, float Width, float Height,
float U, float V, float UW, float VH,
XMFLOAT4 color)
{
D3D11_MAPPED_SUBRESOURCE msr;
GetDeviceContext()->Map(buf, 0, D3D11_MAP_WRITE_DISCARD, 0, &msr);
VERTEX_3D *vertex = (VERTEX_3D*)msr.pData;
float hw, hh;
hw = Width * 0.5f; // コンピューターは割り算が苦手
hh = Height * 0.5f; // 掛け算の方が処理が速い
// 指定された座標を中心に設定するプログラム
// 頂点0番(左上の頂点)
vertex[0].Position = XMFLOAT3(X - hw, Y - hh, 0.0f);
vertex[0].Diffuse = color;
vertex[0].TexCoord = XMFLOAT2(U, V);
// 頂点1番(右上の頂点)
vertex[1].Position = XMFLOAT3(X + hw, Y - hh, 0.0f);
vertex[1].Diffuse = color;
vertex[1].TexCoord = XMFLOAT2(U + UW, V);
// 頂点2番(左下の頂点)
vertex[2].Position = XMFLOAT3(X - hw, Y + hh, 0.0f);
vertex[2].Diffuse = color;
vertex[2].TexCoord = XMFLOAT2(U, V + VH);
// 頂点3番(右下の頂点)
vertex[3].Position = XMFLOAT3(X + hw, Y + hh, 0.0f);
vertex[3].Diffuse = color;
vertex[3].TexCoord = XMFLOAT2(U + UW, V + VH);
GetDeviceContext()->Unmap(buf, 0);
}
void SetSpriteColorRotation(ID3D11Buffer *buf, float X, float Y, float Width, float Height,
float U, float V, float UW, float VH,
XMFLOAT4 Color, float Rot)
{
D3D11_MAPPED_SUBRESOURCE msr;
GetDeviceContext()->Map(buf, 0, D3D11_MAP_WRITE_DISCARD, 0, &msr);
VERTEX_3D *vertex = (VERTEX_3D*)msr.pData;
float hw, hh;
hw = Width * 0.5f; // コンピューターは割り算が苦手
hh = Height * 0.5f; // 掛け算の方が処理が速い
// 指定された座標を中心に回転するプログラム
float BaseAngle = atan2f(hh, hw); // 中心点から頂点に対する角度
XMVECTOR temp = { hw, hh, 0.0f, 0.0f };
temp = XMVector2Length(temp); // 中心点から頂点に対する距離
float Radius = 0.0f;
XMStoreFloat(&Radius, temp);
// ここでアフィン変換(sincosのやつ)を使って4頂点を回転させる
float x = X - cosf(BaseAngle + Rot) * Radius;
float y = Y - sinf(BaseAngle + Rot) * Radius;
vertex[0].Position = XMFLOAT3(x, y, 0.0f);
x = X + cosf(BaseAngle - Rot) * Radius;
y = Y - sinf(BaseAngle - Rot) * Radius;
vertex[1].Position = XMFLOAT3(x, y, 0.0f);
x = X - cosf(BaseAngle - Rot) * Radius;
y = Y + sinf(BaseAngle - Rot) * Radius;
vertex[2].Position = XMFLOAT3(x, y, 0.0f);
x = X + cosf(BaseAngle + Rot) * Radius;
y = Y + sinf(BaseAngle + Rot) * Radius;
vertex[3].Position = XMFLOAT3(x, y, 0.0f);
vertex[0].Diffuse = Color;
vertex[1].Diffuse = Color;
vertex[2].Diffuse = Color;
vertex[3].Diffuse = Color;
vertex[0].TexCoord = XMFLOAT2(U, V);
vertex[1].TexCoord = XMFLOAT2(U + UW, V);
vertex[2].TexCoord = XMFLOAT2(U, V + VH);
vertex[3].TexCoord = XMFLOAT2(U + UW, V + VH);
GetDeviceContext()->Unmap(buf, 0);
}