-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcSprite.cpp
More file actions
363 lines (321 loc) · 8.65 KB
/
cSprite.cpp
File metadata and controls
363 lines (321 loc) · 8.65 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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
=================
cSprite.cpp
- Header file for class definition - IMPLEMENTATION
=================
*/
#include "cSprite.h"
/*
=================
- Data constructor initializes the cSprite to the data passed to
- the constructor from the paramater sPosition.
- Is always called, thus ensures all sprite objects are in a consistent state.
=================
*/
cSprite::cSprite() // Default constructor
{
this->spritePos_2D = { 0, 0, 0, 0 };
this->spriteTexture = NULL;
this->spriteCentre = {0, 0};
this->spriteScale = { 1, 1 };
this->spriteRotationAngle = 0;
this->boundingRect = { 0, 0, 0, 0 };
this->mActive = true;
this->mNoFrames = 0;
this->mCurrentFrame = 0;
this->mRate = 0.0f;
}
cSprite::cSprite(cTexture* theSpriteTexture) // Default constructor
{
this->spriteTexture = theSpriteTexture;
this->setSpriteDimensions(this->spriteTexture->getTWidth(), this->spriteTexture->getTHeight());
this->spritePos_2D = { 0, 0, this->spriteTexture->getTWidth(), this->spriteTexture->getTHeight() };
this->spriteCentre = { this->spritePos_2D.w / 2, this->spritePos_2D.h / 2};
this->spriteScale = { 1, 1 };
this->spriteRotationAngle = 0;
this->boundingRect = { 0, 0, 0, 0 };
this->mActive = true;
this->mNoFrames = 0;
this->mCurrentFrame = 0;
this->mRate = 1.0f / 4.0f;
}
/*
=================
- Destructor
=================
*/
cSprite::~cSprite() // Destructor
{
}
/*
=================
- Return the sprites current position.
=================
*/
SDL_Rect cSprite::getSpritePos() // Return the sprites current position
{
return this->spritePos_2D;
}
/*
=================
- set the position of the sprite.
=================
*/
void cSprite::setSpritePos(SDL_Point sPosition) // set the position of the sprite
{
this->spritePos_2D.x = sPosition.x;
this->spritePos_2D.y = sPosition.y;
}
/*
=================
- Return the sprites current image.
=================
*/
cTexture* cSprite::getTexture() // Return the sprites current image
{
return this->spriteTexture;
}
/*
=================
- set the image of the sprite.
=================
*/
void cSprite::setTexture(cTexture* theSpriteTexture) // set the image of the sprite
{
this->spriteTexture = theSpriteTexture;
this->setSpriteDimensions(spriteTexture->getTWidth(), spriteTexture->getTHeight());
this->spritePos_2D.w = textureWidth;
this->spritePos_2D.h = textureHeight;
this->spriteCentre = { this->spritePos_2D.w / 2, this->spritePos_2D.h / 2 };
}
void cSprite::render(SDL_Renderer* theRenderer, SDL_Rect* theSourceRect, SDL_Rect* theDestRect, FPoint theScaling)
{
this->spriteTexture->renderTexture(theRenderer, this->spriteTexture->getTexture(), theSourceRect, theDestRect, theScaling);
}
void cSprite::render(SDL_Renderer* theRenderer, SDL_Rect* theSourceRect, SDL_Rect* theDestRect, double rotAngle, SDL_Point* spriteCentre, FPoint theScaling)
{
this->spriteTexture->renderTexture(theRenderer, this->spriteTexture->getTexture(), theSourceRect, theDestRect, rotAngle, spriteCentre, theScaling);
}
/*
=================
- Set the sprite dimensions.
=================
*/
void cSprite::setSpriteDimensions(int texWidth, int textHeight)
{
this->textureWidth = texWidth;
this->textureHeight = textHeight;
this->spriteDimensions = { 0, 0, texWidth, textHeight };
this->spritePos_2D = { this->spritePos_2D.x, this->spritePos_2D.y, texWidth, textHeight };
}
/*
=================
- Get the sprite dimensions.
=================
*/
SDL_Rect cSprite::getSpriteDimensions()
{
return this->spriteDimensions;
}
/*
=================
- Return the sprite centre.
=================
*/
SDL_Point cSprite::getSpriteCentre() // Return the sprites current position
{
return this->spriteCentre;
}
/*
=================
- set the the sprite centre.
=================
*/
void cSprite::setSpriteCentre(SDL_Point sCentre) // set the position of the sprite
{
this->spriteCentre.x = sCentre.x;
this->spriteCentre.y = sCentre.y;
}
/*
=================
- Return the sprite scaling.
=================
*/
FPoint cSprite::getSpriteScale() // Return the sprites current scaling
{
return this->spriteScale;
}
/*
=================
- set the the sprite scale.
=================
*/
void cSprite::setSpriteScale(FPoint sScale) // set the sprites current scaling
{
this->spriteScale.X += sScale.X;
this->spriteScale.Y += sScale.Y;
}
/*
=================
- Update sprite scale.
=================
*/
void cSprite::scaleSprite() // set the sprites current scaling
{
// Scale sprite
this->spritePos_2D.w = (int)(this->spriteDimensions.w * this->spriteScale.X);
this->spritePos_2D.h = (int)(this->spriteDimensions.h * this->spriteScale.Y);
// Scale Sprite Centre for rotation.
this->spriteCentre.x = (int)(this->spritePos_2D.w / 2);
this->spriteCentre.y = (int)(this->spritePos_2D.h / 2);
}
/*
=================
- Return the sprite rotation Angle.
=================
*/
float cSprite::getSpriteRotAngle() // Return the sprites current scaling
{
return this->spriteRotationAngle;
}
/*
=================
- set the the sprite scale.
=================
*/
void cSprite::setSpriteRotAngle(float angle) // set the sprites current scaling
{
this->spriteRotationAngle = angle;
}
/*
=================
- Determine the bounding rectangle for the sprite.
=================
*/
void cSprite::setBoundingRect(SDL_Rect pRect)
{
SDL_Point sPos = { this->getSpritePos().x, this->getSpritePos().y };
this->boundingRect = { sPos.x, sPos.y, this->getSpritePos().w, this->getSpritePos().h }; //(pRect, sPos.x - m_Radius, sPos.y - m_Radius, (textureWidth / 2 + sPos.x), (textureHeight / 2 + sPos.y));
}
/*
=================
- Return the bounding rectangle for the sprite.
=================
*/
SDL_Rect cSprite::getBoundingRect() // Return the bounding rectangle for the sprite
{
return cSprite::boundingRect;
}
/*
=================================================================
Sets the translation for the sprite
=================================================================
*/
void cSprite::setSpriteTranslation(SDL_Point spriteTrans)
{
this->spriteTranslation = spriteTrans;
}
/*
=================================================================
Gets the sprite translation
=================================================================
*/
SDL_Point cSprite::getSpriteTranslation()
{
return this->spriteTranslation;
}
/*
=================
- Set the sprite to active.
=================
*/
void cSprite::setActive(bool sActive) // Set the sprite to active.
{
mActive = sActive;
}
/*
=================
- Determine if the sprite is active.
=================
*/
bool cSprite::isActive() // Determine if the sprite is active.
{
return mActive;
}
/*
=================
- Check for collisions.
=================
*/
bool cSprite::collidedWith(SDL_Rect* thisSpriteRect, SDL_Rect* otherSpriteRect)
{
// perform the intersection test
if (SDL_HasIntersection(thisSpriteRect, otherSpriteRect))
return true;
else
return false;
}
bool cSprite::SphereSphereCollision(SDL_Point spritePosition, float spriteRadius)
{
SDL_Point theSpriteLen;
theSpriteLen.x = this->getSpritePos().x - spritePosition.x;
theSpriteLen.y = this->getSpritePos().y - spritePosition.y;
const float distSq = lengthSQRD(theSpriteLen);
const float sumRadius = (this->getSpriteCentre().x + spriteRadius);
if (distSq < sumRadius * sumRadius)
{
return true; // Collision
}
return false; // No Collision
}
float cSprite::lengthSQRD(SDL_Point theLength)
{
return (float)(theLength.x * theLength.x) + (theLength.y * theLength.y);
}
void cSprite::animate(double deltaTime)
{
if ((SDL_GetTicks() / 100) % this->getNoFrames())
{
int frameWidth = this->getSpriteDimensions().w;
this->spritePos_2D.w = frameWidth;
this->setSourceRect( { mCurrentFrame * frameWidth, 0, frameWidth, this->getSpriteDimensions().h });
if (this->getCurrentFrame() >= this->getNoFrames())
{
this->setCurrentFrame(0);
this->setActive(false);
}
this->mCurrentFrame++;
}
}
void cSprite::setNoFrames(int numFrames)
{
this->mNoFrames = numFrames;
}
int cSprite::getNoFrames()
{
return this->mNoFrames;
}
void cSprite::setCurrentFrame(int currentFrame)
{
this->mCurrentFrame = currentFrame;
}
int cSprite::getCurrentFrame()
{
return this->mCurrentFrame;
}
void cSprite::setSourceRect(SDL_Rect sourceRect)
{
this->mSourceRect = sourceRect;
}
SDL_Rect cSprite::getSourceRect()
{
return this->mSourceRect;
}
void cSprite::setRate(float theRate)
{
this->mRate = theRate;
}
float cSprite::getRate()
{
return this->mRate;
}