Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/CSprite.cpp
blob: 2814bd0734ef7066566fcff9bfbf962a2d289d31 (plain)
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
#include "CSprite.h"

CSprite::CSprite(void)
{
	mOldX = 0;
	mOldY = 0;
	mFrame = 0;
	mLastUpdate = 0;
	mDrawn = false;
	mParticle = new CParticle(0, 0, 0, 0, 0, 0);
	mStartFrame = 0;
	mEndFrame = 0;
	mIsLooping = false;
}

CSprite::CSprite(CSpriteAnimation *aSpriteAnimation, CSurface *aScreen)
{
	mOldX = 0;
	mOldY = 0;
	mFrame = 0;
	mLastUpdate = 0;
	mDrawn = false;
	mParticle = new CParticle(0, 0, 0, 0, 0, 0);
	mStartFrame = 0;
	mEndFrame = 0;
	mIsLooping = false;

	mSpriteAnimation = aSpriteAnimation;
	mScreen = aScreen;

	if (mSpriteAnimation != NULL)
	{
		// Create the background image.
		mBackground = new CSurface(mSpriteAnimation->getWidth(), mSpriteAnimation->getHeight());

		if (mSpriteAnimation->getNumFrames() > 1)
			mAnimating = true;
		else
			mAnimating = false;
	}
	else
	{
		fprintf(stderr, "Sprite constructor failed!\n");
		fprintf(stderr, "Sprite animation is not built!\n");
	}
}

CSprite::~CSprite(void)
{
	destroy();
}

void CSprite::destroy(void)
{
	if (mBackground != NULL)
	{
		mBackground->destroy();
		mBackground = NULL;
	}

	if (mParticle != NULL)
	{
		delete mParticle;
		mParticle = NULL;
	}
}

// Restore the background.
void CSprite::restoreBackground(void)
{
	// If the sprite has been drawn once before, then we can clear the screen.
	// If we do not do this, the first time the backgrouns is restored and this results in a black rectangle.
	if (mDrawn)
	{
		CSurface::drawImage(mScreen, mBackground, mOldX, mOldY);
	}
}

// Save the background.
void CSprite::saveBackground(void)
{
	CSurface::drawImage(mBackground, mScreen, 0, 0, getIntX()+mOffsetX, getIntY()+mOffsetY, mSpriteAnimation->getWidth(), mSpriteAnimation->getHeight());
	mOldX = getIntX()+mOffsetX;
	mOldY = getIntY()+mOffsetY;
}

// Draw the sprite in the screen.
void CSprite::draw(void)
{
	CSurface::drawImage(mScreen, mSpriteAnimation->getFrame(mFrame)->getImage(), getIntX()+mOffsetX, getIntY()+mOffsetY);

	if (!mDrawn) 
		mDrawn = true;
}

void CSprite::update(void)
{
	if (mAnimating)
	{
		if (mLastUpdate + mSpriteAnimation->getFrame(mFrame)->getFrameTime() < SDL_GetTicks())
		{
			mFrame++;
			if (mFrame > mEndFrame)
			{
				if (mIsLooping)
				{
					mFrame = mStartFrame;
					mAnimating = true;
				}
				else
				{
					mFrame = mEndFrame;
					mAnimating = false;
				}
			}
			mLastUpdate = SDL_GetTicks();
		}
	}

	mParticle->update();

//	fprintf(stdout, "mAnimating = %d.\n", mAnimating);
//	fprintf(stdout, "mFrame = %d.\n", mFrame);
}

void CSprite::render(void)
{
	restoreBackground();
	saveBackground();
	draw();
}

int CSprite::getIntX(void)
{
	return (int) floor(mParticle->getX());
}

int CSprite::getIntY(void)
{
	return (int) floor(mParticle->getY());
}

void CSprite::setXVel(float aXVel)
{
	mParticle->setXVel(aXVel);
}

void CSprite::setYVel(float aYVel)
{
	mParticle->setYVel(aYVel);
}

void CSprite::setAnim(int aStartFrame, int aEndFrame, bool aIsLooping)
{
	if (aStartFrame == aEndFrame)
		mAnimating = false;
	else
		mAnimating = true;

	mIsLooping = aIsLooping;
	mStartFrame = aStartFrame;
	mEndFrame = aEndFrame;

	mFrame = mStartFrame;
	mLastUpdate = SDL_GetTicks();
}

void CSprite::setRenderOffsets(int aOffsetX, int aOffsetY)
{
	mOffsetX = aOffsetX;
	mOffsetY = aOffsetY;
}

void CSprite::setXY(int aX, int aY)
{
	mParticle->setXY(aX, aY);	
}