Jeffery Hurley
/
Asteroids
Simple starter skeleton for asteroids video game.
Sprite.cpp@5:454ff3197a74, 2021-04-01 (annotated)
- Committer:
- jhurley31
- Date:
- Thu Apr 01 20:09:47 2021 +0000
- Revision:
- 5:454ff3197a74
- Parent:
- 3:98aa3db6a48f
Updating OS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jhurley31 | 3:98aa3db6a48f | 1 | #include <math.h> |
jhurley31 | 2:30020ddfccf6 | 2 | #include "Sprite.h" |
jhurley31 | 3:98aa3db6a48f | 3 | #include "uLCD_4DGL.h" |
jhurley31 | 3:98aa3db6a48f | 4 | #include "graphic.h" |
jhurley31 | 3:98aa3db6a48f | 5 | |
jhurley31 | 2:30020ddfccf6 | 6 | |
jhurley31 | 3:98aa3db6a48f | 7 | extern uLCD_4DGL guLCD; |
jhurley31 | 3:98aa3db6a48f | 8 | extern double gTimeStep; |
jhurley31 | 3:98aa3db6a48f | 9 | extern double gOriginX; |
jhurley31 | 3:98aa3db6a48f | 10 | extern double gOriginY; |
jhurley31 | 3:98aa3db6a48f | 11 | |
jhurley31 | 2:30020ddfccf6 | 12 | Sprite::Sprite(): |
jhurley31 | 3:98aa3db6a48f | 13 | m_xCenter(gOriginX), |
jhurley31 | 3:98aa3db6a48f | 14 | m_yCenter(gOriginY), |
jhurley31 | 3:98aa3db6a48f | 15 | m_DirX(0.0), |
jhurley31 | 3:98aa3db6a48f | 16 | m_DirY(0.0), |
jhurley31 | 3:98aa3db6a48f | 17 | m_Velocity(0.0), |
jhurley31 | 3:98aa3db6a48f | 18 | m_color(WHITE), |
jhurley31 | 3:98aa3db6a48f | 19 | m_RotationVelocity(0.0), |
jhurley31 | 3:98aa3db6a48f | 20 | m_RotationDirection(NONE), |
jhurley31 | 3:98aa3db6a48f | 21 | m_numPoints(0), |
jhurley31 | 3:98aa3db6a48f | 22 | m_bIsValid(false) |
jhurley31 | 3:98aa3db6a48f | 23 | { |
jhurley31 | 3:98aa3db6a48f | 24 | |
jhurley31 | 3:98aa3db6a48f | 25 | } |
jhurley31 | 3:98aa3db6a48f | 26 | ///////////////////////////////////////////////////////////////////////// |
jhurley31 | 3:98aa3db6a48f | 27 | ///////////////////////////////////////////////////////////////////////// |
jhurley31 | 3:98aa3db6a48f | 28 | // move |
jhurley31 | 3:98aa3db6a48f | 29 | // Erase Sprite from current location |
jhurley31 | 3:98aa3db6a48f | 30 | // Move the sprite to the new location |
jhurley31 | 3:98aa3db6a48f | 31 | // Draw sprite at new location |
jhurley31 | 3:98aa3db6a48f | 32 | void Sprite::move() |
jhurley31 | 2:30020ddfccf6 | 33 | { |
jhurley31 | 3:98aa3db6a48f | 34 | |
jhurley31 | 3:98aa3db6a48f | 35 | // Erase object |
jhurley31 | 3:98aa3db6a48f | 36 | draw(true); |
jhurley31 | 3:98aa3db6a48f | 37 | |
jhurley31 | 3:98aa3db6a48f | 38 | // Translate to the origin all points around the origin using the center points |
jhurley31 | 3:98aa3db6a48f | 39 | for (int ii = 0 ; ii < m_numPoints ; ++ii) |
jhurley31 | 3:98aa3db6a48f | 40 | { |
jhurley31 | 3:98aa3db6a48f | 41 | m_xPoints[ii] -= m_xCenter; |
jhurley31 | 3:98aa3db6a48f | 42 | m_yPoints[ii] -= m_yCenter; |
jhurley31 | 3:98aa3db6a48f | 43 | } |
jhurley31 | 3:98aa3db6a48f | 44 | |
jhurley31 | 3:98aa3db6a48f | 45 | // Rotate around the origin |
jhurley31 | 3:98aa3db6a48f | 46 | float tmpX, tmpY; |
jhurley31 | 3:98aa3db6a48f | 47 | for (int ii = 0 ; ii < m_numPoints ; ++ii) |
jhurley31 | 3:98aa3db6a48f | 48 | { |
jhurley31 | 3:98aa3db6a48f | 49 | tmpX = m_xPoints[ii]*std::cos(m_RotationVelocity*PI/180.0) - m_yPoints[ii]*std::sin(m_RotationVelocity*PI/180.0); |
jhurley31 | 3:98aa3db6a48f | 50 | tmpY = m_yPoints[ii]*std::cos(m_RotationVelocity*PI/180.0) + m_xPoints[ii]*std::sin(m_RotationVelocity*PI/180.0); |
jhurley31 | 3:98aa3db6a48f | 51 | |
jhurley31 | 3:98aa3db6a48f | 52 | m_xPoints[ii] = tmpX; |
jhurley31 | 3:98aa3db6a48f | 53 | m_yPoints[ii] = tmpY; |
jhurley31 | 3:98aa3db6a48f | 54 | } |
jhurley31 | 3:98aa3db6a48f | 55 | // Translate back to original location |
jhurley31 | 3:98aa3db6a48f | 56 | for (int ii = 0 ; ii < m_numPoints ; ++ii) |
jhurley31 | 3:98aa3db6a48f | 57 | { |
jhurley31 | 3:98aa3db6a48f | 58 | m_xPoints[ii] += m_xCenter; |
jhurley31 | 3:98aa3db6a48f | 59 | m_yPoints[ii] += m_yCenter; |
jhurley31 | 3:98aa3db6a48f | 60 | } |
jhurley31 | 3:98aa3db6a48f | 61 | // Move to new location |
jhurley31 | 3:98aa3db6a48f | 62 | for (int ii = 0 ; ii < (m_numPoints-1) ; ++ii) |
jhurley31 | 3:98aa3db6a48f | 63 | { |
jhurley31 | 3:98aa3db6a48f | 64 | m_xPoints[ii] += m_DirX*m_Velocity*(gTimeStep); |
jhurley31 | 3:98aa3db6a48f | 65 | m_yPoints[ii] += m_DirY*m_Velocity*(gTimeStep); |
jhurley31 | 3:98aa3db6a48f | 66 | } |
jhurley31 | 3:98aa3db6a48f | 67 | m_xCenter += m_DirX*m_Velocity*(gTimeStep); |
jhurley31 | 3:98aa3db6a48f | 68 | m_yCenter += m_DirX*m_Velocity*(gTimeStep); |
jhurley31 | 3:98aa3db6a48f | 69 | // Draw at the new location |
jhurley31 | 3:98aa3db6a48f | 70 | draw(false); |
jhurley31 | 3:98aa3db6a48f | 71 | |
jhurley31 | 3:98aa3db6a48f | 72 | } |
jhurley31 | 3:98aa3db6a48f | 73 | ///////////////////////////////////////////////////////////////////////// |
jhurley31 | 3:98aa3db6a48f | 74 | ///////////////////////////////////////////////////////////////////////// |
jhurley31 | 3:98aa3db6a48f | 75 | // draw sprite at new location |
jhurley31 | 3:98aa3db6a48f | 76 | void Sprite::draw(bool bErase) |
jhurley31 | 3:98aa3db6a48f | 77 | { |
jhurley31 | 3:98aa3db6a48f | 78 | int currentColor = BLACK; |
jhurley31 | 3:98aa3db6a48f | 79 | if (!bErase) |
jhurley31 | 3:98aa3db6a48f | 80 | { |
jhurley31 | 3:98aa3db6a48f | 81 | currentColor = m_color; |
jhurley31 | 3:98aa3db6a48f | 82 | } |
jhurley31 | 2:30020ddfccf6 | 83 | |
jhurley31 | 3:98aa3db6a48f | 84 | if (m_numPoints < 2) |
jhurley31 | 3:98aa3db6a48f | 85 | { |
jhurley31 | 3:98aa3db6a48f | 86 | return; |
jhurley31 | 3:98aa3db6a48f | 87 | } |
jhurley31 | 3:98aa3db6a48f | 88 | else if (m_numPoints == 2) |
jhurley31 | 3:98aa3db6a48f | 89 | { |
jhurley31 | 3:98aa3db6a48f | 90 | guLCD.line((int)m_xPoints[m_numPoints-1], (int)m_yPoints[m_numPoints-1] , (int)m_xPoints[0], (int)m_yPoints[0], currentColor); |
jhurley31 | 3:98aa3db6a48f | 91 | } |
jhurley31 | 3:98aa3db6a48f | 92 | else |
jhurley31 | 3:98aa3db6a48f | 93 | { |
jhurley31 | 3:98aa3db6a48f | 94 | for (int ii = 0 ; ii < (m_numPoints-1) ; ++ii) |
jhurley31 | 3:98aa3db6a48f | 95 | { |
jhurley31 | 3:98aa3db6a48f | 96 | guLCD.line((int)m_xPoints[ii], (int)m_yPoints[ii] , (int)m_xPoints[ii+1], (int)m_yPoints[ii+1], currentColor); |
jhurley31 | 3:98aa3db6a48f | 97 | } |
jhurley31 | 3:98aa3db6a48f | 98 | // go from last point to first point |
jhurley31 | 3:98aa3db6a48f | 99 | guLCD.line((int)m_xPoints[m_numPoints-1], (int)m_yPoints[m_numPoints-1] , (int)m_xPoints[0], (int)m_yPoints[0], currentColor); |
jhurley31 | 3:98aa3db6a48f | 100 | } |
jhurley31 | 3:98aa3db6a48f | 101 | |
jhurley31 | 3:98aa3db6a48f | 102 | } |
jhurley31 | 3:98aa3db6a48f | 103 | ///////////////////////////////////////////////////////////////////////// |
jhurley31 | 3:98aa3db6a48f | 104 | ///////////////////////////////////////////////////////////////////////// |
jhurley31 | 3:98aa3db6a48f | 105 | // addPoint |
jhurley31 | 3:98aa3db6a48f | 106 | // Adds a point to the vector of points |
jhurley31 | 3:98aa3db6a48f | 107 | void Sprite::addPoint(int x, int y) |
jhurley31 | 2:30020ddfccf6 | 108 | { |
jhurley31 | 3:98aa3db6a48f | 109 | m_xPoints[m_numPoints] = x; |
jhurley31 | 3:98aa3db6a48f | 110 | m_yPoints[m_numPoints] = y; |
jhurley31 | 3:98aa3db6a48f | 111 | m_numPoints++; |
jhurley31 | 3:98aa3db6a48f | 112 | if (m_numPoints == NUM_POINTS) |
jhurley31 | 3:98aa3db6a48f | 113 | { |
jhurley31 | 3:98aa3db6a48f | 114 | m_numPoints = 0; |
jhurley31 | 3:98aa3db6a48f | 115 | } |
jhurley31 | 3:98aa3db6a48f | 116 | } |
jhurley31 | 3:98aa3db6a48f | 117 |