-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSFXImage.cpp
345 lines (251 loc) · 7.98 KB
/
SFXImage.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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// SFXImage.cpp
//
// Paints a simple image
#include "PreComp.h"
#define RANDOM_START_FRAME_ATTRIB CONSTLIT("randomStartFrame")
#define ROTATE_IMAGE_ATTRIB CONSTLIT("rotateImage")
#define VARIANTS_ATTRIB CONSTLIT("imageVariants")
#define LIFETIME_ATTRIB CONSTLIT("lifetime")
#define ROTATION_COUNT_ATTRIB CONSTLIT("rotationCount")
const int COMPUTE_LIFETIME_CONSTANT = -100;
const int ALWAYS_LOOP_CONSTANT = -101;
class CImagePainter : public IEffectPainter
{
public:
CImagePainter (CImageEffectCreator *pCreator);
// IEffectPainter virtuals
virtual CEffectCreator *GetCreator (void) { return m_pCreator; }
virtual bool GetParticlePaintDesc (SParticlePaintDesc *retDesc);
virtual void GetRect (RECT *retRect) const;
virtual int GetVariants (void) const;
virtual void Paint (CG32bitImage &Dest, int x, int y, SViewportPaintCtx &Ctx);
virtual bool PointInImage (int x, int y, int iTick, int iVariant = 0, int iRotation = 0) const;
protected:
virtual void OnReadFromStream (SLoadCtx &Ctx);
virtual void OnWriteToStream (IWriteStream *pStream);
private:
CImageEffectCreator *m_pCreator;
CCompositeImageSelector m_Sel;
};
IEffectPainter *CImageEffectCreator::OnCreatePainter (CCreatePainterCtx &Ctx)
// CreatePainter
//
// Returns a painter
{
if (m_Image.IsConstant())
return this;
else
return new CImagePainter(this);
}
bool CImageEffectCreator::GetParticlePaintDesc (SParticlePaintDesc *retDesc)
// GetParticlePaintDesc
//
// Returns particle painting descriptor for optimized painting
{
retDesc->iStyle = paintImage;
retDesc->pImage = &m_Image.GetImage(CCompositeImageSelector());
retDesc->iVariants = m_iVariants;
retDesc->bDirectional = m_bDirectional;
retDesc->bRandomStartFrame = m_bRandomStartFrame;
return true;
}
void CImageEffectCreator::GetRect (RECT *retRect) const
// GetRect
//
// Returns the image rect
{
CObjectImageArray &Image = m_Image.GetImage(CCompositeImageSelector());
*retRect = Image.GetImageRect();
}
ALERROR CImageEffectCreator::OnEffectCreateFromXML (SDesignLoadCtx &Ctx, CXMLElement *pDesc, const CString &sUNID)
// OnEffectCreateFromXML
//
// Initialize type-specific data
{
ALERROR error;
if (error = m_Image.InitFromXML(Ctx, pDesc))
return error;
if (!pDesc->FindAttributeInteger(LIFETIME_ATTRIB, &m_iLifetime))
m_iLifetime = (Ctx.bLoopImages ? ALWAYS_LOOP_CONSTANT : COMPUTE_LIFETIME_CONSTANT);
m_bRandomStartFrame = pDesc->GetAttributeBool(RANDOM_START_FRAME_ATTRIB);
// Variants & Rotation
m_bRotateImage = pDesc->GetAttributeBool(ROTATE_IMAGE_ATTRIB);
if (m_bRotateImage)
{
m_iVariants = 1;
m_bDirectional = false;
}
else
{
m_iVariants = pDesc->GetAttributeInteger(ROTATION_COUNT_ATTRIB);
m_bDirectional = (m_iVariants > 1);
if (m_iVariants == 0)
m_iVariants = pDesc->GetAttributeInteger(VARIANTS_ATTRIB);
if (m_iVariants <= 0)
m_iVariants = 1;
}
return NOERROR;
}
ALERROR CImageEffectCreator::OnEffectBindDesign (SDesignLoadCtx &Ctx)
// OnEffectBindDesign
//
// Resolve loading
{
ALERROR error;
if (error = m_Image.OnDesignLoadComplete(Ctx))
return error;
if (m_iLifetime == COMPUTE_LIFETIME_CONSTANT)
m_iLifetime = m_Image.GetMaxLifetime();
else if (m_iLifetime == ALWAYS_LOOP_CONSTANT)
m_iLifetime = -1;
return NOERROR;
}
void CImageEffectCreator::Paint (CG32bitImage &Dest, int x, int y, SViewportPaintCtx &Ctx)
// Paint
//
// Paint the effect
{
// Calculate the tick.
int iTick = Ctx.iTick;
if (m_bRandomStartFrame)
iTick += Ctx.iDestiny;
// If this image has a rotation count, then we get the rotation from the
// context block.
if (m_Image.IsRotatable() && m_bDirectional)
{
CCompositeImageModifiers Modifiers;
Modifiers.SetRotation(Ctx.iRotation);
CObjectImageArray &Image = m_Image.GetImage(CCompositeImageSelector(), Modifiers);
Image.PaintImage(Dest, x, y, iTick, 0);
}
// Otherwise, if we've been asked to rotate the image procedurally, do that.
else if (m_bRotateImage)
{
CObjectImageArray &Image = m_Image.GetImage(CCompositeImageSelector());
Image.PaintRotatedImage(Dest, x, y, iTick, Ctx.iRotation);
}
// Otherwise we assume we have the entire image
else
{
CObjectImageArray &Image = m_Image.GetImage(CCompositeImageSelector());
int iFrame = (m_bDirectional ? Angle2Direction(Ctx.iRotation, m_iVariants) : (Ctx.iVariant % m_iVariants));
Image.PaintImage(Dest, x, y, iTick, iFrame);
}
}
void CImageEffectCreator::PaintComposite (CG32bitImage &Dest, int x, int y, SViewportPaintCtx &Ctx)
// Paint
//
// Paint the effect
{
CObjectImageArray &Image = m_Image.GetImage(CCompositeImageSelector());
int iTick = Ctx.iTick;
if (m_bRandomStartFrame)
iTick += Ctx.iDestiny;
if (m_bRotateImage)
Image.PaintRotatedImage(Dest, x, y, iTick, Ctx.iRotation, true);
else
{
int iFrame = (m_bDirectional ? Angle2Direction(Ctx.iRotation, m_iVariants) : (Ctx.iVariant % m_iVariants));
Image.PaintImage(Dest, x, y, iTick, iFrame, true);
}
}
bool CImageEffectCreator::PointInImage (int x, int y, int iTick, int iVariant, int iRotation) const
// PointInImage
//
// Returns TRUE if the given point is in the image
{
CObjectImageArray &Image = m_Image.GetImage(CCompositeImageSelector());
return Image.PointInImage(x, y, iTick, (iVariant % m_iVariants));
}
void CImageEffectCreator::SetVariants (int iVariants)
// SetVariants
//
// Sets the number of variants
// This function is only called because directionally used to be set at the
// object level. We only honor this if we are not already directional
{
if (!m_bDirectional)
{
m_bDirectional = true;
m_iVariants = iVariants;
}
}
// CImagePainter --------------------------------------------------------------
CImagePainter::CImagePainter (CImageEffectCreator *pCreator) : m_pCreator(pCreator)
// CImagePainter constructor
{
SSelectorInitCtx InitCtx;
m_pCreator->GetImage().InitSelector(InitCtx, &m_Sel);
}
bool CImagePainter::GetParticlePaintDesc (SParticlePaintDesc *retDesc)
// GetParticlePaintDesc
//
// Returns the particle paint descriptor
{
CObjectImageArray &Image = m_pCreator->GetImage().GetImage(m_Sel);
retDesc->iStyle = paintImage;
retDesc->pImage = &Image;
retDesc->iVariants = m_pCreator->GetVariants();
retDesc->bDirectional = m_pCreator->IsDirectional();
retDesc->bRandomStartFrame = m_pCreator->HasRandomStartFrame();
return true;
}
void CImagePainter::GetRect (RECT *retRect) const
// GetRect
//
// Returns the rect of the image
{
CObjectImageArray &Image = m_pCreator->GetImage().GetImage(m_Sel);
*retRect = Image.GetImageRect();
}
int CImagePainter::GetVariants (void) const
// GetVariants
//
// Returns the number of variants
{
return m_pCreator->GetVariants();
}
void CImagePainter::OnReadFromStream (SLoadCtx &Ctx)
// OnReadFromStream
//
// Load from stream
{
// The constructor initializes this to a default value; we need to clear
// it out because ReadFromStream ASSERTS that it is empty.
m_Sel.DeleteAll();
m_Sel.ReadFromStream(Ctx);
}
void CImagePainter::OnWriteToStream (IWriteStream *pStream)
// OnWriteToStream
//
// Write to stream
{
m_Sel.WriteToStream(pStream);
}
void CImagePainter::Paint (CG32bitImage &Dest, int x, int y, SViewportPaintCtx &Ctx)
// Paint
//
// Paint image
{
CObjectImageArray &Image = m_pCreator->GetImage().GetImage(m_Sel);
int iTick = Ctx.iTick;
if (m_pCreator->HasRandomStartFrame())
iTick += Ctx.iDestiny;
if (m_pCreator->ImageRotationNeeded())
Image.PaintRotatedImage(Dest, x, y, iTick, Ctx.iRotation);
else
{
int iVariants = m_pCreator->GetVariants();
int iFrame = (m_pCreator->IsDirectional() ? Angle2Direction(Ctx.iRotation, iVariants) : (Ctx.iVariant % iVariants));
Image.PaintImage(Dest, x, y, iTick, iFrame);
}
}
bool CImagePainter::PointInImage (int x, int y, int iTick, int iVariant, int iRotation) const
// PointInImage
//
// Returns TRUE if point is in the image
{
CObjectImageArray &Image = m_pCreator->GetImage().GetImage(m_Sel);
int iVariants = m_pCreator->GetVariants();
return Image.PointInImage(x, y, iTick, (iVariant % iVariants));
}