/pingus-0.7.6/src/pingus/particles/smoke_particle_holder.cpp

# · C++ · 91 lines · 57 code · 14 blank · 20 comment · 10 complexity · d9c5ab6bb9f3b65be4d2125a77a02dc6 MD5 · raw file

  1. // Pingus - A free Lemmings clone
  2. // Copyright (C) 1999 Ingo Ruhnke <grumbel@gmx.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include "pingus/particles/smoke_particle_holder.hpp"
  17. #include "engine/display/scene_context.hpp"
  18. namespace Particles {
  19. SmokeParticleHolder::SmokeParticle::SmokeParticle (float x, float y, float vel_x, float vel_y) :
  20. time(),
  21. livetime(),
  22. use_surf2(),
  23. pos(Vector3f(x,y)),
  24. velocity(Vector3f(vel_x, vel_y))
  25. {
  26. time = livetime = 25 + (rand() % 10);
  27. use_surf2 = rand() % 2;
  28. }
  29. SmokeParticleHolder::SmokeParticleHolder ()
  30. : surf1("particles/smoke"),
  31. surf2("particles/smoke2"),
  32. particles()
  33. {
  34. }
  35. void
  36. SmokeParticleHolder::add_particle (float x, float y, float vel_x, float vel_y)
  37. {
  38. // search for dead entry to replace
  39. for (std::vector<SmokeParticle>::iterator it=particles.begin(); it != particles.end(); ++it)
  40. if (!it->livetime)
  41. {
  42. *it = SmokeParticle(x, y, vel_x, vel_y);
  43. return;
  44. }
  45. // create new entry
  46. particles.push_back(SmokeParticle(x, y, vel_x, vel_y));
  47. }
  48. void
  49. SmokeParticleHolder::update ()
  50. {
  51. // update all contained particles
  52. for (std::vector<SmokeParticle>::iterator it=particles.begin(); it != particles.end(); ++it)
  53. {
  54. // skip dead particles
  55. if (!it->livetime)
  56. continue;
  57. it->pos.x += it->velocity.x;
  58. it->pos.y += it->velocity.y;
  59. it->pos.z = get_z_pos();
  60. --it->livetime;
  61. }
  62. }
  63. void
  64. SmokeParticleHolder::draw (SceneContext& gc)
  65. {
  66. for (std::vector<SmokeParticle>::iterator it=particles.begin(); it != particles.end(); ++it)
  67. {
  68. if (!it->livetime)
  69. continue;
  70. if (!it->use_surf2)
  71. gc.color().draw(surf1, it->pos);
  72. else
  73. gc.color().draw(surf2, it->pos);
  74. }
  75. }
  76. } // namespace Particles
  77. /* EOF */