Rebuild autotool system
[construo.git] / particle_factory.cxx
bloba98dbf01d6c938be2ad4dbbb5318a8fe1e36137a
1 // $Id: particle_factory.cxx,v 1.13 2003/07/28 09:23:46 grumbel Exp $
2 //
3 // Construo - A wire-frame construction game
4 // Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include <float.h>
21 #include <algorithm>
22 #include "lisp_reader.hxx"
23 #include "zoom_graphic_context.hxx"
24 #include "particle.hxx"
25 #include "construo_error.hxx"
26 #include "world.hxx"
27 #include "particle_factory.hxx"
29 ParticleFactory::ParticleFactory (World* w)
30 : world (w), particle_id_count(0)
34 ParticleFactory::ParticleFactory (World* w, lisp_object_t* cursor)
35 : world (w),particle_id_count (0)
37 while(!lisp_nil_p(cursor))
39 lisp_object_t* obj = lisp_car(cursor);
40 Vector2d pos;
41 Vector2d velocity;
42 float mass = 1.0f/10.0f;
43 bool fixed = false;
44 int id = -1;
46 obj = lisp_cdr(obj); // skip particle 'marker'
48 LispReader reader(obj);
49 reader.read_vector ("pos", &pos);
50 reader.read_vector ("velocity", &velocity);
51 reader.read_float ("mass", &mass);
52 reader.read_bool ("fixed", &fixed);
53 reader.read_int ("id", &id);
55 switch (world->file_version)
57 case 0:
58 case 1:
59 case 2:
60 mass = 1.0f/10.0f;
61 break;
64 if (id >= particle_id_count)
65 particle_id_count = id + 1;
67 particles.push_back(new Particle (id, pos, velocity, mass, fixed));
69 cursor = lisp_cdr (cursor);
73 ParticleFactory::ParticleFactory (World* w, const ParticleFactory& pmgr)
74 : world (w)
76 particle_id_count = pmgr.particle_id_count;
77 for (CParticleIter i = pmgr.particles.begin (); i != pmgr.particles.end (); ++i)
78 particles.push_back(new Particle(**i));
81 ParticleFactory&
82 ParticleFactory::operator= (const ParticleFactory& pmgr)
84 ConstruoAssert (0, "Don't use this");
85 for (CParticleIter i = pmgr.particles.begin ();
86 i != pmgr.particles.end ();
87 ++i)
89 particles.push_back (new Particle (*(*i)));
91 return *this;
94 Particle*
95 ParticleFactory::add_particle (const Vector2d& arg_pos, const Vector2d& arg_velocity, float m, bool f)
97 Particle* p = new Particle(particle_id_count++,
98 arg_pos,
99 arg_velocity, m, f);
100 particles.push_back(p);
101 return p;
104 Particle*
105 ParticleFactory::add_particle (const Particle& particle)
107 Particle* p = new Particle (particle);
108 p->id = particle_id_count++,
109 particles.push_back(p);
110 return p;
113 void
114 ParticleFactory::remove_particle (Particle* p)
116 // Remove the particle itself
117 for (ParticleIter i = particles.begin (); i != particles.end (); ++i)
119 if (*i == p)
121 delete *i;
122 particles.erase(i);
123 return;
128 struct particle_obsolete
130 inline bool operator()(Particle* p)
132 return (p->spring_links == 0
133 && p->velocity.x == 0
134 && fabsf(p->velocity.y) < 0.1f);
138 void
139 ParticleFactory::update (float delta)
141 for (CParticleIter i = particles.begin (); i != particles.end (); ++i)
142 (*i)->update(delta);
144 // FIXME: There is no need to do this on any update, doing it only
145 //once a second should be enough
146 particles.erase(std::remove_if(particles.begin(), particles.end(),
147 particle_obsolete()),
148 particles.end());
151 void
152 ParticleFactory::draw (ZoomGraphicContext* gc)
154 for (CParticleIter i = particles.begin (); i != particles.end (); ++i)
155 (*i)->draw(gc);
158 Particle*
159 ParticleFactory::lookup_particle (int id)
161 // FIXME: Could need optimization
162 for (ParticleIter i = particles.begin ();
163 i != particles.end ();
164 ++i)
166 if ((*i)->get_id () == id)
167 return *i;
169 return 0;
172 void
173 ParticleFactory::clear ()
175 for (CParticleIter i = particles.begin (); i != particles.end (); ++i)
176 delete *i;
177 particles.clear ();
180 void
181 ParticleFactory::write_lisp(FILE* out)
183 fputs(" (particles\n", out);
184 for (CParticleIter i = particles.begin (); i != particles.end (); ++i)
186 lisp_object_t* obj = (*i)->serialize ();
187 fputs(" ", out);
188 lisp_dump (obj, out);
189 lisp_free(obj);
190 fputc('\n', out);
192 fputs(" )\n", out);
195 /* EOF */