source: GTP/trunk/App/Games/Jungle_Rumble/src/Sprite.cpp @ 1378

Revision 1378, 6.5 KB checked in by giegl, 18 years ago (diff)

GTPD - Jungle Rumble - integrate into GTP SVN structure

Line 
1#include "dxstdafx.h"
2#include ".\sprite.h"
3#include "Scene.h"
4#include "GameManager.h"
5#include "ResourceManager.h"
6
7Sprite::Sprite(void) : Node()
8{
9        this->firstTime = true;
10        this->lookAtCamera = true;
11        this->rightVec.setXYZ(1, 0, 0);
12        this->upVec.setXYZ(0, 1, 0);
13        this->activeFrame = 0;
14        this->nbFrames = 1;
15        this->animationDuration = 0;
16        this->maxDuration = 0;
17        this->nbRows = 1;
18        this->nbColumns = 1;
19        this->invRows = 1;
20        this->invColumns = 1;
21        this->loopAnimation = true;
22        this->invFps = 1;
23        this->fps = 1;
24        this->sphereSizeFactor = 1;
25        for(int i=0;i<3;i++) {
26                for(int j=0;j<7;j++) {
27                        this->keyFrame[i][j] = 1;
28                        currentValues[j] = 1;
29                }
30        }
31        this->halfWidth = 1;
32        this->halfHeight = 1;
33        this->keyFrame[Sprite::KEY_START][KF_TIME] = 0;
34        this->keyFrame[Sprite::KEY_MIDDLE][KF_TIME] = 0.5;
35        this->firstRendering = true;
36        this->nodeType |= Scene::NODE_SPRITE;
37        this->animatedSprite = false;
38        this->textureName = "";
39        this->alpha = 1;
40        this->firstRendering = true;
41        this->texture = NULL;
42
43}
44
45Sprite::~Sprite(void)
46{
47}
48
49void Sprite::setDimension(float _width, float _height)
50{
51        this->halfWidth = _width/2;
52        this->halfHeight = _height/2;
53
54        for(int i=0;i<3;i++) {
55                this->keyFrame[i][Sprite::KF_HWIDTH] = this->halfWidth;
56                this->keyFrame[i][Sprite::KF_HHEIGHT] = this->halfHeight;
57        }
58
59        this->minAABBox.x = -this->halfWidth;
60        this->maxAABBox.x = this->halfWidth;
61
62        this->minAABBox.y = -this->halfWidth;
63        this->maxAABBox.y = this->halfWidth;
64
65        this->minAABBox.z = -0.5;
66        this->maxAABBox.z = 0.5;
67}
68
69void Sprite::update(float dt)
70{
71        Node::update(dt);
72       
73        if(!this->firstRendering) {
74                this->calcCurrentValues(this->maxAge - this->timeToLive);
75               
76                if(this->animatedSprite) {
77                        //Calculate activeFrame
78                        this->animationDuration+=dt;
79                        if(this->loopAnimation) {
80                                if(this->animationDuration>this->maxDuration)
81                                        this->animationDuration-=this->maxDuration;
82                        } else {
83                                this->animationDuration = this->maxDuration;
84                        }
85
86                        this->activeFrame = (int) (this->animationDuration*this->fps);
87                } else {
88                        this->maxDuration = (float) this->nbFrames*this->invFps;
89                        this->maxDuration++;
90                }
91        }
92}
93
94void Sprite::generatePhysicMesh(int type)
95{
96        sphereDesc.radius = (this->halfHeight>this->halfWidth) ? this->halfHeight*this->sphereSizeFactor : this->halfWidth*this->sphereSizeFactor;
97        this->pActorDesc.shapes.pushBack(&sphereDesc);
98}
99
100Node* Sprite::clone()
101{
102        Sprite* obj = (Sprite*) this->myScene->createNode(Scene::NODE_SPRITE,*this->father, true, false);
103        this->cloneChildren(obj);
104       
105        obj->lookAtCamera = this->lookAtCamera;
106        obj->halfWidth = this->halfWidth;
107        obj->halfHeight = this->halfHeight;
108        obj->rightVec = this->rightVec;
109        obj->upVec = this->upVec;
110        obj->setAlpha(this->alpha);
111        obj->setRowColumnCount(this->nbRows, this->nbColumns);
112        obj->setFPS(this->fps);
113        obj->setFrameCount(this->nbFrames);
114        obj->setTexture(this->textureName);
115        obj->setDimension(2*this->halfWidth, 2*this->halfHeight);
116        obj->setTimeToLive(this->timeToLive);
117        obj->userData = this->userData;
118        for(int i=0;i<3;i++) {
119                for(int j=0;j<7;j++) {
120                        obj->keyFrame[i][j] = this->keyFrame[i][j];
121                }
122        }
123        obj->animatedSprite = this->animatedSprite;
124
125        obj->pActorDesc = this->pActorDesc;
126        obj->pBodyDesc = this->pBodyDesc;
127        obj->pActorDesc.body = &obj->pBodyDesc;
128       
129        obj->pActorDesc.shapes.clear();
130        obj->sphereDesc.radius = this->sphereDesc.radius;
131        obj->pActorDesc.shapes.pushBack(&obj->sphereDesc);
132        return obj;
133}
134
135void Sprite::setLookAtCamera(bool _lookAtCamera)
136{
137        this->lookAtCamera = _lookAtCamera;
138}
139
140void Sprite::calcPhysicWorldMatrix(D3DXMATRIX &pMatWorld)
141{
142        if(this->lookAtCamera) {
143                if(this->pActor!=NULL) {
144                        NxVec3 vec = this->pActor->getGlobalPosition();
145                        D3DXMatrixTranslation(&this->myWorldMatrix, vec.x, vec.y, vec.z);
146                }
147        } else {
148                Node::calcPhysicWorldMatrix(pMatWorld);
149        }
150}
151
152void Sprite::setTexture(std::string _textureName)
153{
154        this->textureName = _textureName;
155        this->texture = this->myScene->manager->resManager.loadTexture(this->textureName);
156}
157
158void Sprite::setAlpha(float _alpha)
159{
160        this->alpha = _alpha;
161        if(this->hasRenderer() && this->alpha!=1) {
162                SimpleMeshRenderer* r = (SimpleMeshRenderer*) this->myRenderer.get();
163                r->enableAlphaBlending(true);
164        }
165}
166
167float Sprite::getMinDim() {
168        return min(this->halfWidth, this->halfHeight);
169}
170
171void Sprite::setFPS(int _fps)
172{
173        this->animatedSprite = true;
174        this->invFps = (float) 1.0f/_fps;
175        this->fps = _fps;
176}
177
178void Sprite::setFrameCount(int _frameCount)
179{
180        this->animatedSprite = true;
181        this->nbFrames = _frameCount;
182}
183
184void Sprite::setRowColumnCount(int _nbRows, int _nbColumns)
185{
186        this->animatedSprite = true;
187        this->nbRows = _nbRows;
188        this->nbColumns = _nbColumns;
189        this->invRows = (float) 1/this->nbRows;
190        this->invColumns = (float) 1/this->nbColumns;
191}
192
193void Sprite::setLoopAnimation(bool _loopAnimation)
194{
195        this->animatedSprite = true;
196        this->loopAnimation = _loopAnimation;
197}
198
199void Sprite::setSphereSizeFactor(float _sphereSizeFactor)
200{
201        this->sphereSizeFactor = _sphereSizeFactor;
202}
203
204void Sprite::setKeyFrame(int _keyFrame, float alpha, float red, float green, float blue, float width, float height, float time)
205{
206        this->keyFrame[_keyFrame][Sprite::KF_ALPHA]     = alpha;
207        this->keyFrame[_keyFrame][Sprite::KF_RED] = red;
208        this->keyFrame[_keyFrame][Sprite::KF_GREEN] = green;
209        this->keyFrame[_keyFrame][Sprite::KF_BLUE] = blue;
210        this->keyFrame[_keyFrame][Sprite::KF_HWIDTH] = width/2;
211        this->keyFrame[_keyFrame][Sprite::KF_HHEIGHT] = height/2;
212        this->keyFrame[_keyFrame][Sprite::KF_TIME] = time;
213}
214
215void Sprite::calcCurrentValues(float age) {
216        int sid;
217        int eid;
218        float fadeFactor = age/this->maxAge;
219        if(fadeFactor < this->keyFrame[Sprite::KEY_MIDDLE][KF_TIME]) {
220                //interpolate between start - middle
221                sid = KEY_START;
222                eid = KEY_MIDDLE;
223                fadeFactor = fadeFactor/this->keyFrame[Sprite::KEY_MIDDLE][KF_TIME];
224        } else {
225                //interpolate between middle - end
226                sid = KEY_MIDDLE;
227                eid = KEY_END;
228                fadeFactor = (fadeFactor-this->keyFrame[Sprite::KEY_MIDDLE][KF_TIME])/(1-this->keyFrame[Sprite::KEY_MIDDLE][KF_TIME]);
229        }
230        fadeFactor = (fadeFactor>1) ? 1 : fadeFactor;
231        for(int i=0;i<7;i++) {
232                this->currentValues[i] = (1-fadeFactor)*this->keyFrame[sid][i] + fadeFactor*this->keyFrame[eid][i];
233        }
234        this->currentValues[Sprite::KF_ALPHA] = max(0.0f, this->currentValues[Sprite::KF_ALPHA]);
235        this->halfWidth = this->currentValues[Sprite::KF_HWIDTH];
236        this->halfHeight = this->currentValues[Sprite::KF_HHEIGHT];
237}
Note: See TracBrowser for help on using the repository browser.