-////////////////////////////////////////////////////////////////////////////////
-// This program is free software; you can redistribute it and/or //
-// modify it under the terms of the GNU General Public License //
-// version 2 as published by the Free Software Foundation. //
-// //
-// This program is distributed in the hope that it will be useful, but //
-// WITHOUT ANY WARRANTY; without even the implied warranty of //
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //
-// General Public License for more details. //
-// //
-// Written and (C) by François Fleuret //
-// Contact <francois.fleuret@epfl.ch> for comments & bug reports //
-////////////////////////////////////////////////////////////////////////////////
+// Written and (C) by Francois Fleuret
+// Contact <francois.fleuret@idiap.ch> for comments & bug reports
+
+#include <string.h>
#include "universe.h"
Universe::Universe(int nb_max_polygons,
- scalar_t xmax, scalar_t ymax) : _xmax(xmax), _ymax(ymax),
- _nb_max_polygons(nb_max_polygons), _nb_polygons(0) {
+ scalar_t width, scalar_t height) : _width(width),
+ _height(height),
+ _nb_max_polygons(nb_max_polygons),
+ _nb_polygons(0) {
_polygons = new Polygon *[_nb_max_polygons];
for(int n = 0; n < _nb_max_polygons; n++) _polygons[n] = 0;
}
Universe::~Universe() {
- for(int n = 0; n < _nb_polygons; n++) if(_polygons[n]) delete _polygons[n];
+ clear();
delete[] _polygons;
}
-void Universe::initialize(Polygon *p) {
+void Universe::initialize_polygon(Polygon *p) {
p->initialize(_nb_max_polygons);
}
}
_polygons[_nb_polygons++] = p;
} else {
- cerr << "To many polygons!" << endl;
+ cerr << "Too many polygons!" << endl;
exit(1);
}
}
bool result = false;
apply_collision_forces(dt);
for(int n = 0; n < _nb_polygons; n++) if(_polygons[n]) {
- _polygons[n]->apply_border_forces(dt, _xmax, _ymax);
+ _polygons[n]->apply_border_forces(dt, _width, _height);
result |= _polygons[n]->update(dt);
}
return result;
return 0;
}
-void Universe::print_fig(ostream &os) {
- os << "#FIG 3.2" << endl;
- os << "Portrait" << endl;
- os << "Center" << endl;
- os << "Metric" << endl;
- os << "A4 " << endl;
- os << "100.00" << endl;
- os << "Single" << endl;
- os << "-2" << endl;
- os << "1200 2" << endl;
- for(int n = 0; n < _nb_polygons; n++) if(_polygons[n]) _polygons[n]->print_fig(os);
+#ifdef XFIG_SUPPORT
+void Universe::print_xfig(XFigTracer *tracer) {
+ for(int n = 0; n < _nb_polygons; n++) {
+ if(_polygons[n]) {
+ _polygons[n]->color_xfig(tracer);
+ }
+ }
+ for(int n = 0; n < _nb_polygons; n++) {
+ if(_polygons[n]) {
+ _polygons[n]->print_xfig(tracer);
+ }
+ }
}
+#endif
+#ifdef X11_SUPPORT
void Universe::draw(SimpleWindow *window) {
- for(int n = 0; n < _nb_polygons; n++) if(_polygons[n]) _polygons[n]->draw(window);
- for(int n = 0; n < _nb_polygons; n++) if(_polygons[n]) _polygons[n]->draw_contours(window);
+ for(int n = 0; n < _nb_polygons; n++) {
+ if(_polygons[n]) {
+ _polygons[n]->draw(window);
+ }
+ }
+
+ for(int n = 0; n < _nb_polygons; n++) {
+ if(_polygons[n]) {
+ _polygons[n]->draw_contours(window);
+ }
+ }
+}
+#endif
+
+void Universe::draw(Canvas *canvas) {
+ for(int n = 0; n < _nb_polygons; n++) {
+ if(_polygons[n]) {
+ _polygons[n]->draw(canvas);
+ }
+ }
+
+ for(int n = 0; n < _nb_polygons; n++) {
+ if(_polygons[n]) {
+ _polygons[n]->draw_contours(canvas);
+ }
+ }
+}
+
+void Universe::apply_gravity(scalar_t dt, scalar_t fx, scalar_t fy) {
+ for(int n = 0; n < _nb_polygons; n++)
+ if(_polygons[n])
+ _polygons[n]->apply_force(dt,
+ _polygons[n]->_center_x, _polygons[n]->_center_y,
+ fx, fy);
}
void Universe::apply_collision_forces(scalar_t dt) {