The Game Engine  1
Sprite.cpp
Go to the documentation of this file.
1 #include "Sprite.h"
2 #include "Game.h"
3 #include <GL/gl.h>
4 #include <iostream>
5 #include "Collision.h"
6 using std::cout;
7 using std::endl;
8 
9 Sprite::Sprite( std::string name, Actor actor, bool menuSprite) :
10 WorldObject(name),
11  mDrawn(0),
12  mActor(actor)
13 {
14  //add to current level
15  Game* g = Game::game();
16  if(!menuSprite)
17  g->addObject(this);
18 
20  {
21  if (mActor.Animations[mActor.CurrentAnimation]->NumFrames > 1) mAnimating = 1;
22  }
23 }
24 
26 {
27  //Frame so we don't have to find it so often
28  SpriteFrame* frame = getAnimation();
29  if(mVisible == true) {
30  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
31  glColor4f(1, 1, 1, mTransparency);
32  glLoadIdentity();
33  //cam pos
34  Point2D movePos=mPos-Game::game()->mCamera.pos;
35  glTranslated(movePos.x, movePos.y, 0);//move to position
36  glTranslated(frame->animationPeg.x, frame->animationPeg.y, 0);//move back to where it was \todo fox for scaling
37  glRotatef(mAngle,0.0f,0.0f,1.0f);//rotate shape
38  glScaled(mScale, mScale, mScale);
39  glTranslated(-frame->animationPeg.x, -frame->animationPeg.y, 0);//place animation peg on origin
40  glBindTexture(GL_TEXTURE_2D, frame->tex.image);
41  glBegin( GL_QUADS );
42  // Top-left vertex (corner)
43  glTexCoord2f( 0, 0 );
44  glVertex3d( 0, 0, 0 );
45 
46  // Bottom-left vertex (corner)
47  glTexCoord2f( 1, 0 );
48  glVertex3d( frame->width, 0, 0 );
49 
50  // Bottom-right vertex (corner)
51  glTexCoord2f( 1, 1 );
52  glVertex3d( frame->width, frame->height, 0 );
53 
54  // Top-right vertex (corner)
55  glTexCoord2f( 0, 1 );
56  glVertex3d( 0, frame->height, 0 );
57  glEnd();
58  glLoadIdentity();
59  glFinish();
60  }
61 }
62 
64 {
65  if(mUnused) return;
67  SpriteFrame* frame = getAnimation();
68  if(mAnimating == 1) {
69  if(mLastUpdate+frame->pause*mSpeed<SDL_GetTicks()) {
70  //obtain current peg
71  Point2D ppos = frame->animationPeg;
72  mActor.Frame++;
74  mActor.Frame=0;
75  //update frame so we don't need to worry
76  frame = getAnimation();
77  //obtain next peg
78  Point2D npos = frame->animationPeg;
79  //move current position to difference of two
80  mPos.add(ppos - npos);
81  mLastUpdate = SDL_GetTicks();
82  }
83  }
84 }
85 
87  Game::game()->CurrentSprite = this;
89  Game::game()->CurrentSprite = NULL;
90 }
91 
92 vector<CollisionObject*>& Sprite::getCollisionData() {
93  return getAnimation()->collisionData;
94 }
95 
96 
98  //get the frame for readability
99  SpriteFrame* frame = getAnimation();
100  //center the location
101  Point2D pt = mPos + frame->animationPeg;
103 }
104 
105 vector<Sprite*> Sprite::collisionWithSprites(string name, int count) {
106  vector<Sprite*> retvals = vector<Sprite*>();
107  //get the frame for readability
108  SpriteFrame* frame = getAnimation();
109  //get the first sprite with this name
110  vector<Sprite*> ss = Game::game()->getCurrentLevel()->findSprites(name,count);
111  if(ss.size()==0)
112  return retvals;
114  for(unsigned int i=0; i < frame->collisionData.size(); i++){
115  foritr(s, ss)
116  {
117  auto sprite = *s;
118  //if there is a collision
119  if(col->collided(this, sprite)){
120  retvals.push_back(sprite);
121  }
122  else{
123  auto val = frame->collisionData[i]->checkCollisions(sprite->getCollisionData(), sprite->mPos + sprite->getAnimation()->animationPeg, mPos + frame->animationPeg);
125  if(val){
126  //col->markCollision(this, frame->collisionData[i], sprite, );
127  retvals.push_back(sprite);
128  }
129  }
130  }
131  }
132 
133  return retvals;//if there aren't collisions
134 }
135 
137  auto anim = getAnimation();
138  return Point2D(mPos.x+anim->width/2.0, mPos.y+anim->height);
139 }
140 
142  auto anim = getAnimation();
143  return Point2D(mPos.x+anim->width/2.0, mPos.y+anim->height/2.0);
144 }