Store user data in $HOME
[kball.git] / src / partmang.cpp
blobe1b5ca5d941b490efdbe443dde47643c6da51227
1 // -----------------------------------------------
2 // partmang.cpp
3 // -----------------------------------------------
4 // Particle manager, to control particles in the game
5 // Also, implementation of base particle
6 // -----------------------------------------------
7 // Developed By Kronoman - Copyright (c) 2004
8 // In loving memory of my father
9 // -----------------------------------------------
11 #include "partmang.h"
12 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
13 // The base particle, a dot
14 // This particle must be used as base
15 // for making other types of particles, using
16 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
17 CBaseParticle::CBaseParticle()
19 x = 0.0;
20 y = 0.0;
21 dx = 0.0;
22 dy = 0.0;
23 color = 0;
24 life = 0;
27 CBaseParticle::CBaseParticle(float x1, float y1, float dx1, float dy1, int color1, int life1)
29 x = x1;
30 y = y1;
31 dx = dx1;
32 dy = dy1;
33 color = color1;
34 life = life1;
37 CBaseParticle::~CBaseParticle()
39 // nothing to be done here
41 // -----------------------------------------------
42 // updates particle's logic,
43 // must return TRUE if particle is dead
44 // (particle WILL be _deleted_ from memory
45 // by manager if dead)
46 // -----------------------------------------------
47 bool CBaseParticle::update_logic(CParticleManager &particle_manager)
49 // nothing to be done here, all that this particle needs is taken into account by manager :^D
50 return false; // I'm not dead (the manager will take care of disccount my life)
53 // -----------------------------------------------
54 // renders particle on bmp, displaced by x,y
55 // -----------------------------------------------
56 void CBaseParticle::render_particle(BITMAP *bmp, int xd, int yd)
58 putpixel(bmp, (int)x - xd, (int)y - yd, color);
61 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
62 // The particle manager
63 // This handles a bunch of particles
64 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
65 CParticleManager::CParticleManager()
67 add_x = add_y = 0.0;
68 add_dx = add_dy = 0.0;
71 CParticleManager::~CParticleManager()
73 nuke_particle_list(); // our precious RAM needs freedom! Viva la revolucion de la RAM!
76 // -----------------------------------------------
77 // this will free all memory used by particles,
78 // and particles itself.
79 // -----------------------------------------------
80 void CParticleManager::nuke_particle_list()
82 list<CBaseParticle *>::iterator i = particle_list.begin(); // iterator for our linked list of particle pointers
84 for (i = particle_list.begin(); i != particle_list.end(); i++)
85 delete (*i); // delete object
87 // clear the list
88 particle_list.clear();
91 // -----------------------------------------------
92 // adds a particle to manager (the particle
93 // WILL be nuked automatically when
94 // the manager object gets deleted)
95 // -----------------------------------------------
96 void CParticleManager::add_particle(CBaseParticle *node)
98 particle_list.push_back(node);
101 // -----------------------------------------------
102 // updates logic of all particles
103 // -----------------------------------------------
104 void CParticleManager::update_logic()
106 list<CBaseParticle *>::iterator i = particle_list.begin(); // iterator for our linked list of particle pointers
108 while (i != particle_list.end())
110 // apply wind, and gravity, and own movement of particle
111 (*i)->dx += add_dx;
112 (*i)->dy += add_dy;
113 (*i)->x += add_x + (*i)->dx;
114 (*i)->y += add_y + (*i)->dy;
116 // decrease life of particle
117 (*i)->life--;
119 if ((*i)->update_logic(*this) || ((*i)->life < 0))
121 list<CBaseParticle *>::iterator i2 = i;
122 // particle is dead, erase it
123 i++;
124 delete (*i2);
125 particle_list.erase(i2);
127 else
129 i++; // next
135 // -----------------------------------------------
136 // renders particles on bmp, displaced by x,y
137 // -----------------------------------------------
138 void CParticleManager::render(BITMAP *bmp, int x, int y)
140 list<CBaseParticle *>::iterator i = particle_list.begin(); // iterator for our linked list of particle pointers
142 for (i = particle_list.begin(); i != particle_list.end(); i++)
143 (*i)->render_particle(bmp, x, y); // renders particle on bmp, displaced by x,y