PageRenderTime 53ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/source/CSprite.cpp

https://bitbucket.org/digaomatias/pucgalaga
C++ | 245 lines | 171 code | 38 blank | 36 comment | 20 complexity | 4dd2db0cb184350ebf86603afcf7c7ea MD5 | raw file
Possible License(s): LGPL-2.1
  1. /*
  2. * CSprite.cpp
  3. * Sprite class
  4. *
  5. * Created by Marcelo Cohen on 04/11.
  6. * Copyright 2011 PUCRS. All rights reserved.
  7. *
  8. */
  9. #include "CSprite.h"
  10. #include <iostream>
  11. #include <string>
  12. #include <cmath>
  13. using namespace std;
  14. // Construtor
  15. CSprite::CSprite() : CMultiImage()
  16. {
  17. firstFrame = 0;
  18. lastFrame = 0;
  19. curFrameD = 0.0;
  20. curframe = 0;
  21. curframe = 0;
  22. framedelay = 10;
  23. }
  24. bool CSprite::loadSprite(char nomeArq[], int w, int h, int hSpace, int vSpace, int xIni, int yIni,
  25. int column, int row, int total)
  26. {
  27. if(!loadMultiImage(nomeArq,w,h,hSpace,vSpace,xIni,yIni,column,row,total))
  28. return false;
  29. // Init animation vars
  30. xspeed = 0;
  31. yspeed = 0;
  32. curframe = 0;
  33. curFrameD = 0;
  34. firstFrame = 0;
  35. lastFrame = total-1;
  36. return true;
  37. }
  38. bool CSprite::loadSpriteSparrowXML(char xmlFile[])
  39. {
  40. cout << "CSprite::loadSpriteSparrowXML " << xmlFile << endl;
  41. TiXmlDocument doc(xmlFile);
  42. if ( ! doc.LoadFile() ) {
  43. return false;
  44. }
  45. //TiXmlElement* elem = doc.RootElement();
  46. doc.Accept(this);
  47. xOffset = spriteW/2;
  48. yOffset = spriteH/2;
  49. width = spriteW;
  50. height = spriteH;
  51. totalFrames = uvs.size();
  52. cout << "CSprite::loadSpriteSparrowXML total frames = " << totalFrames << endl;
  53. // Init animation vars
  54. xspeed = 0;
  55. yspeed = 0;
  56. curframe = 0;
  57. curFrameD = 0;
  58. firstFrame = 0;
  59. lastFrame = totalFrames-1;
  60. mirror = false;
  61. return true;
  62. }
  63. CSprite::~CSprite()
  64. {
  65. //dtor
  66. }
  67. // Especifica quantos pixels o sprite ira se mover em x.
  68. void CSprite::setXspeed(double xspeed)
  69. {
  70. this->xspeed = xspeed;
  71. }
  72. // Especifica quantos pixels a sprite ira se mover em y.
  73. void CSprite::setYspeed(double yspeed)
  74. {
  75. this->yspeed = yspeed;
  76. }
  77. // Sets the current frame
  78. void CSprite::setCurrentFrame(int c)
  79. {
  80. if ( c>=0 && c<totalFrames )
  81. curframe = c;
  82. else
  83. curframe = 0;
  84. curFrameD = curframe;
  85. }
  86. /** @brief setFrameRange
  87. *
  88. * @todo: document this function
  89. */
  90. bool CSprite::setFrameRange(int first, int last)
  91. {
  92. if(first > last || first < 0 || last >= totalFrames)
  93. return false;
  94. firstFrame = first;
  95. lastFrame = last;
  96. return true;
  97. }
  98. // Advance to next frame
  99. void CSprite::frameForward()
  100. {
  101. curframe++;
  102. if (curframe > lastFrame)
  103. curframe = firstFrame;
  104. }
  105. // Go back to previous frame
  106. void CSprite::frameBack()
  107. {
  108. curframe--;
  109. if (curframe < firstFrame)
  110. curframe = lastFrame;
  111. }
  112. // Recebe por parametro o valor que sera usado para especificar o atributo
  113. // framedelay, responsavel por diminuir ou aumentar a taxa de animacao.
  114. void CSprite::setAnimRate(int fdelay)
  115. {
  116. if (fdelay >= 0)
  117. framedelay = fdelay;
  118. else
  119. framedelay = 0;
  120. // Reset framecount so next draw will work as it should be
  121. framecount = 0;
  122. }
  123. // Metodo responsavel por fazer as atualizacoes necessarias para a correta
  124. // animacao do sprite.
  125. void CSprite::update(double updateInterval)
  126. {
  127. // Move sprite according to its speed and the amount of time that has passed
  128. x += xspeed/1000*updateInterval;
  129. y += yspeed/1000*updateInterval;
  130. curFrameD += (double)framedelay/1000*updateInterval;
  131. curframe = (int) curFrameD;
  132. if(curframe > lastFrame) {
  133. curFrameD = firstFrame;
  134. curframe = firstFrame;
  135. }
  136. }
  137. // Draw the sprite
  138. void CSprite::draw()
  139. {
  140. drawFrame(curframe);
  141. }
  142. // Check bounding box collision between this and other sprite
  143. bool CSprite::bboxCollision(CSprite* other)
  144. {
  145. float width1 = this->width/2 * this->scale;
  146. float width2 = other->width/2 * other->scale;
  147. float height1 = this->height/2 * this->scale;
  148. float height2 = other->height/2 * other->scale;
  149. float x0 = this->x - width1;
  150. float y0 = this->y - height1;
  151. float x1 = this->x + width1;
  152. float y1 = this->y + height1;
  153. float x2 = other->x - width2;
  154. float y2 = other->y - height2;
  155. float x3 = other->x + width2;
  156. float y3 = other->y + height2;
  157. return !(x1<x2 || x3<x0 || y1<y2 || y3<y0);
  158. //return !(x1<other->x || x3<this->x || y1<other->y || y3<this->y);
  159. }
  160. // Check circle collision between this and other sprite
  161. bool CSprite::circleCollision(CSprite* other)
  162. {
  163. int radius1 = max(this->width, this->height)/2;
  164. int radius2 = max(other->width, other->height)/2;
  165. radius1 *= this->scale;
  166. radius2 *= other->scale;
  167. float dist = sqrt(pow(this->x-other->x,2)+pow(this->y-other->y,2));
  168. //cout << "Radius: " << radius1 << " and " << radius2 << endl;
  169. //cout << "distance: " << dist << endl;
  170. return (dist < radius1 + radius2);
  171. }
  172. // TiXml visitor implementation: load texture atlas in Sparrow format (http://www.sparrow-framework.org/)
  173. bool CSprite::VisitEnter (const TiXmlElement &elem, const TiXmlAttribute *attrib)
  174. {
  175. cout << "CSprite::VisitEnter " << elem.Value() << "*"<< endl;
  176. if (elem.Value() == string("SubTexture")) {
  177. int x1, y1, h, w;
  178. elem.Attribute("x", &x1);
  179. elem.Attribute("y", &y1);
  180. elem.Attribute("height", &h);
  181. elem.Attribute("width", &w);
  182. spriteW = w;
  183. spriteH = h;
  184. cout << "Texture: " << x1 << " " << y1 << " " << w-1 << " " << h-1 << endl;
  185. float u1 = (float)x1/width;
  186. float v1 = (float)y1/height;
  187. float u2 = (float)(x1+w)/width;
  188. float v2 = (float)(y1+h)/height;
  189. TexRect rect = { u1, v1, u2, v2 };
  190. cout << "Rect: " << rect.u1 << "," << rect.v1
  191. << " - " << rect.u2 << "," << rect.v2 << endl;
  192. uvs.push_back(rect);
  193. //TODO: get spacing and margin
  194. }
  195. else if (elem.Value() == string("TextureAtlas")) { //string("TextureAtlas") == elem.Value()) {
  196. string attrib = elem.Attribute("imagePath");
  197. string prefix = "data/img/";
  198. prefix.append(attrib);// = "data/img/"+attrib;
  199. cout << "TextureAtlas: " << prefix << endl;
  200. bool ok = loadImage((char *) prefix.c_str());
  201. if(!ok)
  202. {
  203. cout << "ERROR LOADING SPRITE IMG: " << prefix.c_str() << endl;
  204. }
  205. }
  206. return true;
  207. }