Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

particle_factory.cxx

Go to the documentation of this file.
00001 //  $Id: particle_factory.cxx,v 1.9 2003/01/06 19:23:16 grumbel Exp $
00002 //
00003 //  Construo - A wire-frame construction game
00004 //  Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
00005 //
00006 //  This program is free software; you can redistribute it and/or
00007 //  modify it under the terms of the GNU General Public License
00008 //  as published by the Free Software Foundation; either version 2
00009 //  of the License, or (at your option) any later version.
00010 //
00011 //  This program is distributed in the hope that it will be useful,
00012 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //  GNU General Public License for more details.
00015 //
00016 //  You should have received a copy of the GNU General Public License
00017 //  along with this program; if not, write to the Free Software
00018 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020 #include "lisp_reader.hxx"
00021 #include "zoom_graphic_context.hxx"
00022 #include "particle.hxx"
00023 #include "construo_error.hxx"
00024 #include "world.hxx"
00025 #include "particle_factory.hxx"
00026 
00027 ParticleFactory::ParticleFactory (World* w)
00028   : world (w), particle_id_count(0)
00029 {
00030 }
00031 
00032 ParticleFactory::ParticleFactory (World* w, lisp_object_t* cursor)
00033   : world (w),particle_id_count (0)
00034 {
00035   while(!lisp_nil_p(cursor))
00036     {
00037       lisp_object_t* obj = lisp_car(cursor);
00038       Vector2d pos;
00039       Vector2d velocity;
00040       float mass = 1.0f/10.0f;
00041       bool fixed = false;
00042       int id = -1;
00043             
00044       obj = lisp_cdr(obj); // skip particle 'marker'
00045 
00046       LispReader reader(obj);
00047       reader.read_vector ("pos", &pos);
00048       reader.read_vector ("velocity", &velocity);
00049       reader.read_float ("mass", &mass);
00050       reader.read_bool ("fixed", &fixed);
00051       reader.read_int ("id", &id);
00052 
00053       switch (world->file_version)
00054         {
00055         case 0:
00056         case 1:
00057         case 2:
00058           mass = 1.0f/10.0f;
00059           break;
00060         }
00061 
00062       if (id >= particle_id_count)
00063         particle_id_count = id + 1;
00064 
00065       particles.push_back(new Particle (id, pos, velocity, mass, fixed));
00066 
00067       cursor = lisp_cdr (cursor);
00068     }
00069 }
00070 
00071 ParticleFactory::ParticleFactory (World* w, const ParticleFactory& pmgr)
00072   : world (w)
00073 {
00074   particle_id_count = pmgr.particle_id_count;
00075   for (CParticleIter i = pmgr.particles.begin (); i != pmgr.particles.end (); ++i)
00076     particles.push_back(new Particle(**i));
00077 }
00078 
00079 ParticleFactory&
00080 ParticleFactory::operator= (const ParticleFactory& pmgr)
00081 {  
00082   ConstruoAssert (0, "Don't use this");
00083   for (CParticleIter i = pmgr.particles.begin ();
00084        i != pmgr.particles.end (); 
00085        ++i)
00086     {
00087       particles.push_back (new Particle (*(*i)));
00088     }
00089   return *this;
00090 }
00091 
00092 Particle*
00093 ParticleFactory::add_particle (const Vector2d& arg_pos, const Vector2d& arg_velocity, float m, bool f)
00094 {
00095   Particle* p = new Particle(particle_id_count++,
00096                              arg_pos, 
00097                              arg_velocity, m, f);
00098   particles.push_back(p);
00099   return p;
00100 }
00101 
00102 Particle*
00103 ParticleFactory::add_particle (const Particle& particle)
00104 {
00105   Particle* p = new Particle (particle);
00106   p->id = particle_id_count++,
00107   particles.push_back(p);
00108   return p;
00109 }
00110 
00111 void
00112 ParticleFactory::remove_particle (Particle* p)
00113 {
00114   // Remove the particle itself
00115   for (ParticleIter i = particles.begin (); i != particles.end (); ++i)
00116     {
00117       if (*i == p)
00118         {
00119           delete *i;
00120           particles.erase(i);
00121           return;
00122         }
00123     }
00124 }
00125 
00126 struct particle_obsolete
00127 {
00128   inline bool operator()(Particle* p)
00129   {
00130     return (p->spring_links == 0 && p->velocity.x == 0);
00131   }
00132 };
00133 
00134 void
00135 ParticleFactory::update (float delta)
00136 {
00137   for (CParticleIter i = particles.begin (); i != particles.end (); ++i)
00138     (*i)->update(delta);
00139 
00140   particles.erase(std::remove_if(particles.begin(), particles.end(), particle_obsolete()),
00141                   particles.end());
00142 }
00143 
00144 void
00145 ParticleFactory::draw (ZoomGraphicContext* gc)
00146 {
00147   for (CParticleIter i = particles.begin (); i != particles.end (); ++i)
00148     (*i)->draw(gc);
00149 }
00150 
00151 Particle*
00152 ParticleFactory::lookup_particle (int id)
00153 {
00154   // FIXME: Could need optimization
00155   for (ParticleIter i = particles.begin (); 
00156        i != particles.end (); 
00157        ++i)
00158     {
00159       if ((*i)->get_id () == id)
00160         return *i;
00161     }
00162   return 0;
00163 }
00164 
00165 void
00166 ParticleFactory::clear ()
00167 {
00168  for (CParticleIter i = particles.begin (); i != particles.end (); ++i)
00169    delete *i;
00170  particles.clear ();
00171 }
00172 
00173 void
00174 ParticleFactory::write_lisp(FILE* out)
00175 {
00176   fputs("  (particles\n", out);
00177   for (CParticleIter i = particles.begin (); i != particles.end (); ++i)
00178     {
00179       lisp_object_t* obj = (*i)->serialize ();
00180       fputs("    ", out);
00181       lisp_dump (obj, out);
00182       lisp_free(obj);
00183       fputc('\n', out);
00184     }
00185   fputs("  )\n", out);
00186 }
00187 
00188 /* EOF */

Generated on Thu Jul 24 10:24:30 2003 for Construo by doxygen1.3-rc3