| 1 |
#include "oodle.h" |
| 2 |
|
| 3 |
CSprite::CSprite(CVector v, CVector p, const string &fileName, const string &animName, const CRect& b, bool alpha, bool nopath) |
| 4 |
: CParticle(v, p, b), currentAnimation(NULL), isClone(false), effect(NULL), scale(1), rot(0) |
| 5 |
{ |
| 6 |
// try |
| 7 |
// { |
| 8 |
currentAnimation = loadAnimation(fileName, animName, alpha, nopath); |
| 9 |
// } |
| 10 |
} |
| 11 |
|
| 12 |
CSprite::CSprite(CVector v, CVector p, const string &fileName, const string &animName, const CRect& b, |
| 13 |
int width, int height, int padding, int speed, bool alpha, bool nopath) |
| 14 |
: CParticle(v, p, b), currentAnimation(NULL), isClone(false), effect(NULL), scale(1), rot(0) |
| 15 |
{ |
| 16 |
// try |
| 17 |
// { |
| 18 |
currentAnimation = loadAnimation(fileName, animName, width, height, padding, speed, alpha, nopath); |
| 19 |
// } |
| 20 |
} |
| 21 |
|
| 22 |
CSprite::CSprite(const CSprite &surf) |
| 23 |
: CParticle(surf), |
| 24 |
isClone(surf.isClone), effect(NULL), scale(surf.scale), rot(surf.rot) |
| 25 |
{ |
| 26 |
// copy over all the CAnimation objects (need to make copies, not just copy pointers) |
| 27 |
for (map<string,CAnimation*>::const_iterator i = surf.animations.begin(); i != surf.animations.end(); ++i) |
| 28 |
{ |
| 29 |
CAnimation* anim = new CAnimation( *i->second ); |
| 30 |
animations[i->first] = anim; |
| 31 |
if (surf.currentAnimation == i->second) // if this was the current animation of surf, then make the current animation of this point to the newly allocated object |
| 32 |
{ |
| 33 |
currentAnimation = anim; |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
CSprite::CSprite(const CSprite& sprite, bool clone) |
| 39 |
: CParticle(sprite), |
| 40 |
isClone(clone), effect(NULL), scale(sprite.scale), rot(sprite.rot) |
| 41 |
{ |
| 42 |
// LOG("Creating clone of CSprite {" << this << "}", 5, LOG_INFO); |
| 43 |
|
| 44 |
for (map<string,CAnimation*>::const_iterator i = sprite.animations.begin(); i != sprite.animations.end(); ++i) |
| 45 |
{ |
| 46 |
animations[i->first] = i->second->getClone(); // construct new map of clones of animations |
| 47 |
} |
| 48 |
|
| 49 |
currentAnimation = animations.begin()->second; |
| 50 |
} |
| 51 |
|
| 52 |
CSprite::CSprite(const CSprite& sprite, bool clone, CVector vel, CVector pos) |
| 53 |
: CParticle(sprite), isClone(clone), effect(NULL), scale(sprite.scale), rot(sprite.rot) |
| 54 |
{ |
| 55 |
// LOG("Creating clone of CSprite {" << this << "}", 5, LOG_INFO); |
| 56 |
|
| 57 |
for (map<string,CAnimation*>::const_iterator i = sprite.animations.begin(); i != sprite.animations.end(); ++i) |
| 58 |
{ |
| 59 |
animations[i->first] = i->second->getClone(); // construct new map of clones of animations |
| 60 |
} |
| 61 |
setPos(pos); |
| 62 |
setVelocity(vel); |
| 63 |
timer.reset(); |
| 64 |
currentAnimation = sprite.currentAnimation; |
| 65 |
} |
| 66 |
|
| 67 |
CSprite::CSprite(CVector v, CVector pos, CSurface* s, const string &animName) |
| 68 |
: CParticle(v, pos), effect(NULL), scale(1), isClone(false), rot(0) |
| 69 |
{ |
| 70 |
currentAnimation = addAnimation(animName, new CAnimation(s)); |
| 71 |
} |
| 72 |
|
| 73 |
CSprite::CSprite(CVector v, CVector p) |
| 74 |
: CParticle(v, p, CRect(0,0,0,0)), currentAnimation(NULL), isClone(false), effect(NULL), scale(1), rot(0) |
| 75 |
{ |
| 76 |
} |
| 77 |
|
| 78 |
CSprite::~CSprite() |
| 79 |
{ |
| 80 |
if (!isClone) |
| 81 |
{ // not a clone so free up memory |
| 82 |
// LOG("deleting non-clone sprite {" << this << "}", 5, LOG_INFO); |
| 83 |
|
| 84 |
for (map<string,CAnimation*>::iterator i = animations.begin(); i != animations.end(); ++i) |
| 85 |
{ |
| 86 |
delete i->second; |
| 87 |
} |
| 88 |
animations.clear(); |
| 89 |
} |
| 90 |
else |
| 91 |
{ // clone, so just do nothing |
| 92 |
|
| 93 |
} |
| 94 |
delete effect; // even is this is NULL, we can call delete on it |
| 95 |
} |
| 96 |
|
| 97 |
const CSprite& CSprite::operator=(const CSprite &s) |
| 98 |
{ |
| 99 |
if (&s != this) // don't allow assignment to self |
| 100 |
{ |
| 101 |
// free old memory |
| 102 |
for (map<string,CAnimation*>::iterator i = animations.begin(); i != animations.end(); ++i) |
| 103 |
{ |
| 104 |
delete i->second; |
| 105 |
} |
| 106 |
animations.clear(); |
| 107 |
|
| 108 |
// copy over all the CAnimation objects (need to make copies, not just copy pointers) |
| 109 |
for (map<string,CAnimation*>::const_iterator i = s.animations.begin(); i != s.animations.end(); ++i) |
| 110 |
{ |
| 111 |
CAnimation* anim = new CAnimation( *i->second ); |
| 112 |
animations[i->first] = anim; |
| 113 |
if (s.currentAnimation == i->second) // if this was the current animation of surf, then make the current animation of this point to the newly allocated object |
| 114 |
{ |
| 115 |
currentAnimation = anim; |
| 116 |
} |
| 117 |
} |
| 118 |
velocity = s.velocity; |
| 119 |
pos = s.pos; |
| 120 |
box = s.box; |
| 121 |
moving = s.moving; |
| 122 |
isClone = s.isClone; |
| 123 |
effect = NULL; |
| 124 |
timer = s.timer; |
| 125 |
rot = s.rot; |
| 126 |
scale = s.scale; |
| 127 |
} |
| 128 |
return *this; |
| 129 |
} |
| 130 |
|
| 131 |
void CSprite::setAnimation(const string &name) |
| 132 |
{ |
| 133 |
// LOG("Setting animation of {" << this << "} to '" << name << "'", 5, LOG_INFO); |
| 134 |
if (animations.find(name) == animations.end()) |
| 135 |
{ |
| 136 |
_THROWEX(ex_illegalArguments, "Animation '" << name << "' not found", "CSprite", "setAnimation", "name = " << name); |
| 137 |
//return; |
| 138 |
} |
| 139 |
currentAnimation = animations[name]; |
| 140 |
currentAnimation->reset(); |
| 141 |
} |
| 142 |
|
| 143 |
void CSprite::setAlpha(Uint32 flags, Uint8 alpha) |
| 144 |
{ |
| 145 |
if (currentAnimation) |
| 146 |
{ |
| 147 |
currentAnimation->setAlpha(flags, alpha); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
void CSprite::update() |
| 152 |
{ |
| 153 |
/*for (map<string,CAnimation*>::iterator i = animations.begin(); i != animations.end(); ++i) |
| 154 |
{ |
| 155 |
i->second->update(); |
| 156 |
} */ |
| 157 |
/* if (dynamic_cast<CBlock*> (this) != NULL && (dynamic_cast<CBlock*> (this))->getBoardY() < 11) |
| 158 |
cout << "update {" << this << "} [" << velocity << "]" << endl;*/ |
| 159 |
if (currentAnimation) |
| 160 |
{ |
| 161 |
currentAnimation->update(); |
| 162 |
if (currentAnimation->isDone()) |
| 163 |
{ |
| 164 |
try |
| 165 |
{ |
| 166 |
setAnimation("static"); |
| 167 |
} |
| 168 |
catch (ex_illegalArguments e) |
| 169 |
{ |
| 170 |
LOG("failed to set animation of {" << this << "} to static", 5, LOG_INFO); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
// Let the CParticle code update position and velocity |
| 175 |
CParticle::update(); |
| 176 |
|
| 177 |
if (box != CRect(0,0,0,0)) |
| 178 |
{ |
| 179 |
if (pos.getX() + getCurrentFrame()->getWidth() > box.getWidth()+box.getX()) |
| 180 |
{ |
| 181 |
pos.setX( box.getWidth()+box.getX() - getCurrentFrame()->getWidth()); |
| 182 |
} |
| 183 |
|
| 184 |
if (pos.getY() + getCurrentFrame()->getHeight() > box.getY() + box.getHeight()) |
| 185 |
{ |
| 186 |
pos.setY( box.getHeight()+box.getY() - getCurrentFrame()->getHeight()); |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
CAnimation* CSprite::loadAnimation(const string &fileName, const string &name, bool alpha, bool nopath) |
| 193 |
{ |
| 194 |
CAnimation* tmp = NULL; |
| 195 |
try |
| 196 |
{ |
| 197 |
tmp = new CAnimation(fileName, alpha, nopath); |
| 198 |
addAnimation(name, tmp); |
| 199 |
} |
| 200 |
catch (ex_illegalArguments e) |
| 201 |
{ |
| 202 |
_ADDTRACEINFO(e, "CSprite", "loadAnimation", "fileName = '" << fileName << "', name = '" << name << "'"); |
| 203 |
delete tmp; |
| 204 |
throw e; |
| 205 |
} |
| 206 |
return tmp; |
| 207 |
} |
| 208 |
|
| 209 |
CAnimation* CSprite::loadAnimation(const string &fileName, const string &name, int width, int height, int padding, int speed, bool alpha, bool nopath) |
| 210 |
{ |
| 211 |
CAnimation* tmp = NULL; |
| 212 |
try |
| 213 |
{ |
| 214 |
tmp = new CAnimation(fileName, width, height, padding, speed, alpha, nopath); |
| 215 |
addAnimation(name, tmp); |
| 216 |
} |
| 217 |
catch (ex_illegalArguments e) |
| 218 |
{ |
| 219 |
_ADDTRACEINFO(e, "CSprite", "loadAnimation", "fileName = '" << fileName << "', name = '" << name << "'" << |
| 220 |
", width = " << width << ", height = " << height << |
| 221 |
", padding = " << padding << ", speed = " << speed); |
| 222 |
delete tmp; |
| 223 |
throw e; |
| 224 |
} |
| 225 |
return tmp; |
| 226 |
} |
| 227 |
|
| 228 |
void CSprite::draw(CSurface &surface) |
| 229 |
{ |
| 230 |
if (CSurface *s = getCurrentFrame()) |
| 231 |
{ |
| 232 |
for (CSequencedEvent::Iter i = events.begin(); i != events.end(); ++i) |
| 233 |
{ |
| 234 |
if ((*i)->hasImage()) |
| 235 |
{ |
| 236 |
s = (*i)->getImage(); |
| 237 |
} |
| 238 |
else if ((*i)->hasDraw()) |
| 239 |
{ |
| 240 |
(*i)->draw(surface, (int)pos.getX(), (int)pos.getY()); |
| 241 |
s = NULL; |
| 242 |
} |
| 243 |
if ( !(*i)->isConcurrent() ) |
| 244 |
{ |
| 245 |
break; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
if (s) |
| 250 |
{ |
| 251 |
if (scale != 1 && rot == 0) |
| 252 |
{ |
| 253 |
surface.blitScale(*s, CRect(pos.getX(), pos.getY(),0,0), scale ); |
| 254 |
} |
| 255 |
else if (rot != 0) |
| 256 |
{ |
| 257 |
surface.blitRotScale(*s, CRect(pos.getX(), pos.getY(), 0,0), scale, rot); |
| 258 |
} |
| 259 |
else |
| 260 |
{ |
| 261 |
surface.blit(*s, (int)pos.getX(), (int)pos.getY()); |
| 262 |
} |
| 263 |
//CGfxPrimitives::drawCircle(pos.getX() + getWidth()/2, pos.getY() + getHeight()/2, 5, CColor(255), surface); |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
CAnimation* CSprite::addAnimation(const string &name, CAnimation* a) |
| 269 |
{ |
| 270 |
if (!a) |
| 271 |
{ |
| 272 |
_THROWEX(ex_illegalArguments, "Attempted to add null pointer as animation", "CSprite", "addAnimation", "name = " << name << ", a = " << a); |
| 273 |
} |
| 274 |
if (isClone) |
| 275 |
{ |
| 276 |
_THROWEX(ex_illegalState, "Attempted to add animation to clone of sprite", "CSprite", "addAnimation", "name = " << name << ", a = " << a); |
| 277 |
} |
| 278 |
|
| 279 |
if( animations.find(name) == animations.end() ) |
| 280 |
{ // make sure key doesn't already exist |
| 281 |
return animations[name] = a; |
| 282 |
} |
| 283 |
else |
| 284 |
{ |
| 285 |
// delete existing animation |
| 286 |
delete animations[name]; |
| 287 |
return animations[name] = a; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
void CSprite::appendEffect(int duration, CEffect::Effects effect) |
| 292 |
{ |
| 293 |
switch (effect) |
| 294 |
{ |
| 295 |
case CEffect::BLACK: |
| 296 |
{ |
| 297 |
//delete CSprite::effect; |
| 298 |
appendEvent( new CBlackEffect(duration, false, *getCurrentFrame()) ); |
| 299 |
break; |
| 300 |
} |
| 301 |
default: |
| 302 |
{ |
| 303 |
// throw exception |
| 304 |
break; |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
CSurface* CSprite::getCurrentFrame(bool orEffect) |
| 310 |
{ |
| 311 |
if (orEffect) |
| 312 |
{ |
| 313 |
for (CSequencedEvent::Iter i = events.begin(); i != events.end(); ++i) |
| 314 |
{ |
| 315 |
if ((*i)->hasImage()) |
| 316 |
{ |
| 317 |
return (*i)->getImage(); |
| 318 |
} |
| 319 |
if (!(*i)->isConcurrent()) |
| 320 |
break; |
| 321 |
} |
| 322 |
return (currentAnimation ? currentAnimation->getCurrentFrame() : NULL); |
| 323 |
} |
| 324 |
else |
| 325 |
return (currentAnimation ? currentAnimation->getCurrentFrame() : NULL); |
| 326 |
} |
| 327 |
|
| 328 |
CSprite* CSprite::getClone() |
| 329 |
{ |
| 330 |
CSprite* clone = new CSprite(*this, true); |
| 331 |
return clone; |
| 332 |
} |
| 333 |
|
| 334 |
CSprite* CSprite::getClone(CVector vel, CVector pos) |
| 335 |
{ |
| 336 |
CSprite* clone = new CSprite(*this, true, vel, pos); |
| 337 |
return clone; |
| 338 |
} |
| 339 |
|
| 340 |
CParticle::BoundaryInfo CSprite::inBoundaryHoriz(double x) |
| 341 |
{ |
| 342 |
if (box != CRect(0,0,0,0)) |
| 343 |
{ |
| 344 |
if (! box.checkLeft(x)) |
| 345 |
{ |
| 346 |
return OUTSIDE_LOW; |
| 347 |
} |
| 348 |
else if (! box.checkRight(x + getWidth())) |
| 349 |
{ |
| 350 |
return OUTSIDE_HIGH; |
| 351 |
} |
| 352 |
else |
| 353 |
{ |
| 354 |
return INSIDE; |
| 355 |
} |
| 356 |
} |
| 357 |
else |
| 358 |
{ |
| 359 |
return INSIDE; |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
CParticle::BoundaryInfo CSprite::inBoundaryVert(double y) |
| 364 |
{ |
| 365 |
if (box != CRect(0,0,0,0)) |
| 366 |
{ |
| 367 |
if (! box.checkBottom(y + getHeight())) |
| 368 |
{ |
| 369 |
return OUTSIDE_HIGH; |
| 370 |
} |
| 371 |
else if (! box.checkTop(y)) |
| 372 |
{ |
| 373 |
return OUTSIDE_LOW; |
| 374 |
} |
| 375 |
else |
| 376 |
{ |
| 377 |
return INSIDE; |
| 378 |
} |
| 379 |
} |
| 380 |
else |
| 381 |
{ |
| 382 |
return INSIDE; |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
void CSprite::setScale(double factor) |
| 387 |
{ |
| 388 |
if (scale >= 0 && scale < 10) |
| 389 |
scale = factor; |
| 390 |
} |
| 391 |
|
| 392 |
void CSprite::resetScale() |
| 393 |
{ |
| 394 |
scale = 1; |
| 395 |
} |
| 396 |
|
| 397 |
void CSprite::resetRot() |
| 398 |
{ |
| 399 |
rot = 0; |
| 400 |
} |
| 401 |
|
| 402 |
void CSprite::setRot(double rad) |
| 403 |
{ |
| 404 |
int w = getWidth(); |
| 405 |
int h = getHeight(); |
| 406 |
double oldRad = rot; |
| 407 |
|
| 408 |
if (! (rad >= 0 && rad <= 2*PI)) |
| 409 |
{ |
| 410 |
if (rad < 0) |
| 411 |
{ |
| 412 |
while( rad < 0) |
| 413 |
{ |
| 414 |
rad += 2*PI; |
| 415 |
} |
| 416 |
} |
| 417 |
else // rad > 2PI |
| 418 |
{ |
| 419 |
while (rad > 2*PI) |
| 420 |
{ |
| 421 |
rad -= 2*PI; |
| 422 |
} |
| 423 |
} |
| 424 |
rot=rad; |
| 425 |
} |
| 426 |
rot = rad; |
| 427 |
// cout << rot/PI << "PI rad" << endl; |
| 428 |
//double dR = rot - oldRad; |
| 429 |
// double angle = rot; |
| 430 |
|
| 431 |
/* if( angle > PI/2 ) |
| 432 |
{ |
| 433 |
while (angle > PI/2) |
| 434 |
{ |
| 435 |
angle -= PI/2; |
| 436 |
} |
| 437 |
}*/ |
| 438 |
/*cout << " " << angle/PI << "PI rad" << endl; |
| 439 |
//dR = dR - PI/2; |
| 440 |
double c = sqrt((double)w*w + (double)h*h); |
| 441 |
double at = acos((double)w/c); |
| 442 |
double comp = (PI/2)-angle; |
| 443 |
|
| 444 |
double w2 = sin( at + comp) * c; |
| 445 |
double h2 = sin( at + angle ) * c; |
| 446 |
|
| 447 |
cout << " " << (int)w2 << " " << (int)h2 << endl; |
| 448 |
|
| 449 |
setPos(50-(w2-2)/2, 50- (h2 - h)/2.);*/ |
| 450 |
} |