-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCSprite.cs
More file actions
286 lines (263 loc) · 11 KB
/
CSprite.cs
File metadata and controls
286 lines (263 loc) · 11 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// [Build] check "Unsafe code"
using Direct2D;
using System.Runtime.InteropServices;
using GlobalStructures;
namespace WinUI3_SwapChainPanel_Direct2D
{
internal class CSprite : IDisposable
{
private ID2D1Bitmap m_pBitmap = null;
private D2D1_RECT_U[] m_pRectSource;
private D2D1_RECT_F[] m_pRectDest;
private D2D1_COLOR_F_STRUCT[] m_pColors;
private D2D1_MATRIX_3X2_F_STRUCT[] m_pTransforms;
private ID2D1SpriteBatch m_pSpriteBatch = null;
private uint m_nNbImagesX = 1;
private uint m_nNbImagesY = 1;
private uint m_nNbImages = 1;
public enum HORIZONTALFLIP : int
{
LEFT = 0,
RIGHT
}
public enum BOUNCE : int
{
HORIZONTAL = 0,
VERTICAL,
BOTH,
NONE
}
public int Width = 0;
public int Height = 0;
public long StartTime = 0;
public string Tag = "";
private uint m_nCurrentIndex = 0;
public uint CurrentIndex
{
get => m_nCurrentIndex;
set
{
m_nCurrentIndex = value;
if (m_nCurrentIndex >= m_nNbImages)
m_nCurrentIndex = 0;
}
}
private float m_nStepX = 1;
public float StepX
{
get => m_nStepX;
set => m_nStepX = value;
}
private float m_nStepY = 1;
public float StepY
{
get => m_nStepY;
set => m_nStepY = value;
}
public float X
{
get => m_pRectDest[0].left;
set => m_pRectDest[0].left = value;
}
public float Y
{
get => m_pRectDest[0].top;
set => m_pRectDest[0].top = value;
}
public CSprite(ID2D1DeviceContext3 pDC, ID2D1Bitmap pBitmap, uint nNbImagesX, uint nNbImagesY, uint nNbImages = 0, float nStepX = 1, float nStepY = 1, D2D1_COLOR_F color = null, D2D1_MATRIX_3X2_F matrix = null)
{
HRESULT hr = HRESULT.S_OK;
m_pBitmap = pBitmap;
m_nNbImagesX = nNbImagesX;
m_nNbImagesY = nNbImagesY;
m_nNbImages = (nNbImages == 0) ? m_nNbImagesX * m_nNbImagesY : nNbImages;
hr = pDC.CreateSpriteBatch(out m_pSpriteBatch);
pBitmap.GetSize(out D2D1_SIZE_F bmpSize);
// Only first pRectDest used in the test...
m_pRectSource = new D2D1_RECT_U[m_nNbImagesX * m_nNbImagesY];
m_pRectDest = new D2D1_RECT_F[m_nNbImagesX * m_nNbImagesY];
m_pTransforms = new D2D1_MATRIX_3X2_F_STRUCT[m_nNbImagesX * m_nNbImagesY];
for (uint m = 0; m < m_nNbImagesX * m_nNbImagesY; m++)
{
if (matrix != null)
{
m_pTransforms[m]._11 = matrix._11;
m_pTransforms[m]._12 = matrix._12;
m_pTransforms[m]._21 = matrix._21;
m_pTransforms[m]._22 = matrix._22;
m_pTransforms[m]._31 = matrix._31;
m_pTransforms[m]._32 = matrix._32;
}
}
StepX = nStepX;
StepY = nStepY;
float nWidth = bmpSize.width / m_nNbImagesX;
float nHeight = bmpSize.height / m_nNbImagesY;
Width = (int)nWidth;
Height = (int)nHeight;
int n = 0;
for (uint j = 0; j < m_nNbImagesY; j++)
{
for (uint i = 0; i < m_nNbImagesX; i++)
{
m_pRectSource[n] = new D2D1_RECT_U((uint)(i * bmpSize.width / m_nNbImagesX), (uint)(j * bmpSize.height / m_nNbImagesY), (uint)((i * bmpSize.width / m_nNbImagesX) + (uint)bmpSize.width / m_nNbImagesX), (uint)((j * bmpSize.height / m_nNbImagesY) + (uint)bmpSize.height / m_nNbImagesY));
m_pRectDest[n] = new D2D1_RECT_F((float)(i * nWidth), (float)(j * nHeight), (float)((i * nWidth) + (float)nWidth), (uint)((j * nHeight) + (float)nHeight));
n++;
}
}
m_pColors = new D2D1_COLOR_F_STRUCT[m_nNbImagesX * m_nNbImagesY];
for (uint c = 0; c < m_nNbImagesX * m_nNbImagesY; c++)
{
if (color != null)
{
m_pColors[c].a = color.a;
m_pColors[c].r = color.r;
m_pColors[c].g = color.g;
m_pColors[c].b = color.b;
}
}
hr = m_pSpriteBatch.AddSprites(m_nNbImagesX * m_nNbImagesY, m_pRectDest, m_pRectSource, color == null ? null : m_pColors, matrix == null ? null : m_pTransforms, 0, (uint)Marshal.SizeOf(typeof(D2D1_RECT_U)), 0, (uint)Marshal.SizeOf(typeof(D2D1_MATRIX_3X2_F)));
}
public void Draw(ID2D1DeviceContext3 pDC, uint nSpriteIndex, uint nSpriteCount, bool bIncrement)
{
pDC.DrawSpriteBatch(m_pSpriteBatch, nSpriteIndex, nSpriteCount, m_pBitmap);
}
public void Move(D2D1_SIZE_F nClientSize, ID2D1DeviceContext3 pDC, HORIZONTALFLIP nHorizontalFlip, BOUNCE nBounce)
{
HRESULT hr = HRESULT.S_OK;
D2D1_SIZE_F size;
if (!nClientSize.Equals(default(D2D1_SIZE_F)))
{
size = nClientSize;
}
else
{
pDC.GetSize(out size);
}
m_pBitmap.GetSize(out D2D1_SIZE_F bmpSize);
float nWidth = bmpSize.width / m_nNbImagesX;
float nHeight = bmpSize.height / m_nNbImagesY;
if (m_pTransforms[0]._11 != 0)
{
//nWidth *= 1/m_pTransforms[0]._11;
size.width *= 1 / m_pTransforms[0]._11;
}
if (m_pTransforms[0]._22 != 0)
{
//nHeight *= 1/m_pTransforms[0]._22;
size.height *= 1 / m_pTransforms[0]._22;
}
m_pRectDest[0].right = m_pRectDest[0].left + nWidth;
m_pRectDest[0].bottom = m_pRectDest[0].top + nHeight;
// Tests to bounce the sprite
if (nBounce == BOUNCE.BOTH)
{
if (m_pRectDest[0].left >= size.width - nWidth)
{
m_nStepX = -Math.Abs(m_nStepX);
m_pRectDest[0].left = size.width - nWidth;
}
if (m_pRectDest[0].top >= size.height - nHeight)
{
m_nStepY = -Math.Abs(m_nStepY);
m_pRectDest[0].top = size.height - nHeight;
}
if (m_pRectDest[0].left <= 0)
{
m_nStepX = Math.Abs(m_nStepX);
m_pRectDest[0].left = 0;
}
if (m_pRectDest[0].top <= 0)
{
m_nStepY = Math.Abs(m_nStepY);
m_pRectDest[0].top = 0;
}
}
else if (nBounce == BOUNCE.HORIZONTAL)
{
if (m_pRectDest[0].left >= size.width - nWidth)
{
m_nStepX = -Math.Abs(m_nStepX);
m_pRectDest[0].left = size.width - nWidth;
}
if (m_pRectDest[0].left <= 0)
{
m_nStepX = Math.Abs(m_nStepX);
m_pRectDest[0].left = 0;
}
if (m_pRectDest[0].top >= size.height && m_nStepY >= 0)
{
m_pRectDest[0].top = 0 - bmpSize.height / m_nNbImagesY;
m_pRectDest[0].bottom = m_pRectDest[0].top + bmpSize.height / m_nNbImagesY;
}
if (m_pRectDest[0].bottom <= 0 && m_nStepY < 0)
{
m_pRectDest[0].top = size.height;
m_pRectDest[0].bottom = m_pRectDest[0].top + bmpSize.height / m_nNbImagesY;
}
}
else if (nBounce == BOUNCE.VERTICAL)
{
if (m_pRectDest[0].top >= size.height - nHeight)
{
m_nStepY = -Math.Abs(m_nStepY);
m_pRectDest[0].top = size.height - nHeight;
}
if (m_pRectDest[0].top <= 0)
{
m_nStepY = Math.Abs(m_nStepY);
m_pRectDest[0].top = 0;
}
if (m_pRectDest[0].left >= size.width && m_nStepX >= 0)
{
m_pRectDest[0].left = 0 - bmpSize.width / m_nNbImagesX;
m_pRectDest[0].right = m_pRectDest[0].left + bmpSize.width / m_nNbImagesX;
}
if (m_pRectDest[0].right <= 0 && m_nStepX < 0)
{
m_pRectDest[0].left = size.width;
m_pRectDest[0].right = m_pRectDest[0].left + bmpSize.width / m_nNbImagesX;
}
}
if (nHorizontalFlip == HORIZONTALFLIP.LEFT || nHorizontalFlip == HORIZONTALFLIP.RIGHT)
{
if (m_nStepX >= 0 && nHorizontalFlip == HORIZONTALFLIP.RIGHT || m_nStepX < 0 && nHorizontalFlip == HORIZONTALFLIP.LEFT)
{
int n = 0;
for (uint j = 0; j < m_nNbImagesY; j++)
{
for (uint i = 0; i < m_nNbImagesX; i++)
{
m_pRectSource[n] = new D2D1_RECT_U((uint)((i * bmpSize.width / m_nNbImagesX) + (uint)bmpSize.width / m_nNbImagesX), (uint)(j * bmpSize.height / m_nNbImagesY), (uint)(i * bmpSize.width / m_nNbImagesX), (uint)((j * bmpSize.height / m_nNbImagesY) + (uint)bmpSize.height / m_nNbImagesY));
n++;
}
}
}
else
{
int n = 0;
for (uint j = 0; j < m_nNbImagesY; j++)
{
for (uint i = 0; i < m_nNbImagesX; i++)
{
m_pRectSource[n] = new D2D1_RECT_U((uint)(i * bmpSize.width / m_nNbImagesX), (uint)(j * bmpSize.height / m_nNbImagesY), (uint)((i * bmpSize.width / m_nNbImagesX) + (uint)bmpSize.width / m_nNbImagesX), (uint)((j * bmpSize.height / m_nNbImagesY) + (uint)bmpSize.height / m_nNbImagesY));
n++;
}
}
}
}
hr = m_pSpriteBatch.SetSprites(0, m_nNbImagesX * m_nNbImagesY, m_pRectDest, m_pRectSource, null, null, 0, (uint)Marshal.SizeOf(typeof(D2D1_RECT_U)), 0, (uint)Marshal.SizeOf(typeof(D2D1_MATRIX_3X2_F)));
}
public void Dispose()
{
m_pSpriteBatch.Clear();
Marshal.ReleaseComObject(m_pSpriteBatch);
m_pSpriteBatch = null;
}
}
}