Guest

Nicolas Bertoa

By: a guest on Sep 17th, 2009  |  syntax: C++  |  size: 2.01 KB  |  hits: 63  |  expires: Never
download  |  raw  |  embed  |  report abuse
This paste has a previous version, view the difference. Copied
  1. /**
  2. * Author: Nicolas Bertoa
  3. *
  4. * File: bullet.cpp
  5. */
  6.  
  7. #include "bullet.h"
  8. #include <iostream>
  9.  
  10.  
  11. //
  12. // Class cBullet
  13. //
  14.  
  15. /**
  16. * FUNCTION: cBullet(SDL_Surface* img, int power, int speed)
  17. *
  18. * Constructor
  19. *
  20. * PARAMETERS:
  21. *
  22. *  SDL_Surface* img: Bullet’s image.
  23. *
  24. *  int power: bullet’s power
  25. *
  26. *  int speed: bullet’s speed
  27. *
  28. * RETURN:
  29. *
  30. *  none
  31. */
  32. cBullet::cBullet(SDL_Surface* img, int power, int speed) : cImage(img)
  33. {
  34.  
  35.         if (power < 0)
  36.                 mPower = 0;
  37.         else
  38.                 mPower = power;
  39.  
  40.         if (speed < 0)
  41.                 mSpeed = 0;
  42.         else
  43.                 mSpeed = speed;
  44.  
  45.         active(true);
  46.  
  47.         mSound = 0;
  48. }
  49.  
  50.  
  51. /**
  52. * FUNCTION: cBullet(SDL_Surface* img, Uint32 colorkey, int power, int speed)
  53. *
  54. * Destructor
  55. *
  56. * PARAMETERS:
  57. *
  58. *  none
  59. *
  60. * RETURN:
  61. *
  62. *  none
  63. */
  64. cBullet::~cBullet() {}
  65.  
  66.  
  67. /**
  68. * FUNCTION: void draw(SDL_Surface* screen)
  69. *
  70. * Draw the bullet at the screen
  71. *
  72. * PARAMETERS:
  73. *
  74. *  SDL_Surface* screen: Window’s image
  75. *
  76. * RETURN:
  77. *
  78. *  none
  79. */
  80.  
  81. void cBullet::draw(SDL_Surface* screen)
  82. {
  83.  
  84.     // Rectangle where we will draw the bullet
  85.     SDL_Rect screenRect;
  86.     screenRect.x = getCoordX();
  87.     screenRect.y = getCoordY();
  88.     screenRect.w = getWidth();
  89.     screenRect.h = getHeight();
  90.  
  91.  
  92.     // If the bullet goes out the screen, we deactivate it
  93.     if (screenRect.x < 0 - getWidth())
  94.         active(false);
  95.  
  96.     if (screenRect.x >= screen->w)
  97.         active(false);
  98.  
  99.     if (screenRect.y < 0 - getHeight())
  100.         active(false);
  101.  
  102.     if (screenRect.y >= screen->h)
  103.         active(false);
  104.  
  105.     cImage::draw(screen);
  106.  
  107. }
  108.  
  109.  
  110. /**
  111. * FUNCTION: void play(int channel, int times)
  112. *
  113. * Play the explosion’s sound
  114. *
  115. * PARAMETERS:
  116. *
  117. *  int channel: Sound reproduction channel. 0 to 99
  118. *
  119. *  int times: Reproduction times. Pass 0 for only 1 reproduction and
  120. *  pass -1 for an infinite loop reproduction.
  121. *
  122. * RETURN:
  123. *
  124. *  none
  125. */
  126. void cBullet::play(int channel, int times)
  127. {
  128.  
  129.     if (mSound == 0)
  130.         return;
  131.  
  132.     if (times >= -1)
  133.         Mix_PlayChannel(channel, mSound, times);
  134.  
  135. }