From: Francois Fleuret Date: Sun, 12 Jul 2009 19:48:54 +0000 (+0200) Subject: Automatic commit X-Git-Url: https://ant.fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=commitdiff_plain;h=3e51d77116e49ba279a6cbbf1dbbc893c4117eb4;p=universe.git Automatic commit --- diff --git a/Makefile b/Makefile index 3b4ea46..05e9586 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,7 @@ LDFLAGS = -L/usr/X11R6/lib/ TASK_SRC = dummy.cc move_square.cc hit_shape.cc TASK_OBJ = $(TASK_SRC:.cc=.task) -all: main TAGS $(TASK_OBJ) +all: mash main TAGS $(TASK_OBJ) TAGS: *.cc *.h etags *.cc *.h @@ -46,8 +46,7 @@ main: main.o misc.o \ intelligence.o $(CXX) -lX11 $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -art: art.o misc.o simple_window.o universe.o polygon.o map.o \ - task.o retina.o manipulator.o approximer.o intelligence.o +mash: misc.o universe.o polygon.o xfig_tracer.o mash.o $(CXX) -lX11 $(CXXFLAGS) -o $@ $^ $(LDFLAGS) %.task: %.cc misc.o universe.o polygon.o map.o task.o manipulator.o diff --git a/main.cc b/main.cc index 405354c..9871ce0 100644 --- a/main.cc +++ b/main.cc @@ -396,7 +396,7 @@ int main(int argc, char **argv) { cout << "Retina screen shot saved in /tmp/retina.ppm" << endl; { XFigTracer tracer("/tmp/universe.fig"); - universe.print_fig(&tracer); + universe.print_xfig(&tracer); } } else if(strcmp(se.key, "Shift_L") == 0 || strcmp(se.key, "Shift_R") == 0) press_shift = true; diff --git a/mash.cc b/mash.cc index 04a49b1..da376b5 100644 --- a/mash.cc +++ b/mash.cc @@ -1,4 +1,3 @@ -// -*- compile-command:"g++ -lX11 -Wall -g -O3 -o mash misc.o universe.o polygon.o mash.cc xfig_tracer.o -L/usr/X11R6/lib/"; -*- #include #include @@ -35,7 +34,7 @@ int main(int argc, char **argv) { ys[k] = scalar_t(h)/2 + radius * sin(-alpha); } Polygon *p; - p = new Polygon(1.0, 0.5, 0.5, 0.5, xs, ys, nb); + p = new Polygon(1.0, 0.0, 0.0, 0.0, xs, ys, nb); p->set_position(scalar_t(w/2), scalar_t(h) * 0.75, 0); p->set_speed(0, 0, 0); universe.initialize(p); @@ -88,9 +87,16 @@ int main(int argc, char **argv) { cout << "n = " << n << " nb_edges = " << nb_edges << endl; - blocks[i] = new Polygon(1.0, 1.0, 1.0, 0.0, xs, ys, nb_edges); + scalar_t red, green, blue; do { - blocks[i]->set_position(drand48() * w/2 + w/4, drand48() * h/2 + h/4, M_PI * drand48()); + red = scalar_t(16 * int(drand48() * 16))/255.0; + green = scalar_t(16 * int(drand48() * 16))/255.0; + blue = scalar_t(16 * int(drand48() * 16))/255.0; + } while((red > 0.1 && green > 0.1 && blue > 0.1) || + (red < 0.9 && green < 0.9 && blue < 0.9)); + blocks[i] = new Polygon(1.0, red, green, blue, xs, ys, nb_edges); + do { + blocks[i]->set_position(drand48() * w * 0.9 + w * 0.05, drand48() * 3 * h / 4 + h/8, M_PI * drand48()); blocks[i]->set_speed(0, 0, 0); // universe.initialize(blocks[i]); for(int j = 0; j < i + 1; j++) { @@ -112,13 +118,13 @@ int main(int argc, char **argv) { char buffer[1024]; sprintf(buffer, "/tmp/mash_%06d.fig", n); XFigTracer tracer(buffer); - universe.print_fig(&tracer); + universe.print_xfig(&tracer); cout << "Wrote " << buffer << endl; } } XFigTracer tracer("/tmp/mash.fig"); - universe.print_fig(&tracer); + universe.print_xfig(&tracer); exit(0); } diff --git a/polygon.cc b/polygon.cc index 1b8b362..2ae238d 100644 --- a/polygon.cc +++ b/polygon.cc @@ -47,8 +47,13 @@ Polygon *Polygon::clone() { return new Polygon(_mass, _red, _green, _blue, _relative_x, _relative_y, _nb_vertices); } -void Polygon::print_fig(XFigTracer *tracer) { - tracer->draw_polygon(_nb_vertices, _x, _y); +void Polygon::color_xfig(XFigTracer *tracer) { + tracer->add_color(int(255 * _red), int(255 * _green), int(255 * _blue)); +} + +void Polygon::print_xfig(XFigTracer *tracer) { + tracer->draw_polygon(int(255 * _red), int(255 * _green), int(255 * _blue), + _nb_vertices, _x, _y); } void Polygon::draw(SimpleWindow *window) { diff --git a/polygon.h b/polygon.h index 87181e0..add154b 100644 --- a/polygon.h +++ b/polygon.h @@ -66,7 +66,9 @@ public: Polygon *clone(); - void print_fig(XFigTracer *tracer); + void color_xfig(XFigTracer *tracer); + void print_xfig(XFigTracer *tracer); + void draw(SimpleWindow *window); void draw_contours(SimpleWindow *window); void set_vertex(int k, scalar_t x, scalar_t y); diff --git a/universe.cc b/universe.cc index 5d6ed91..48d8efc 100644 --- a/universe.cc +++ b/universe.cc @@ -116,10 +116,15 @@ Polygon *Universe::pick_polygon(scalar_t x, scalar_t y) { return 0; } -void Universe::print_fig(XFigTracer *tracer) { +void Universe::print_xfig(XFigTracer *tracer) { for(int n = 0; n < _nb_polygons; n++) { if(_polygons[n]) { - _polygons[n]->print_fig(tracer); + _polygons[n]->color_xfig(tracer); + } + } + for(int n = 0; n < _nb_polygons; n++) { + if(_polygons[n]) { + _polygons[n]->print_xfig(tracer); } } } diff --git a/universe.h b/universe.h index d411bb6..74339a3 100644 --- a/universe.h +++ b/universe.h @@ -32,7 +32,7 @@ public: void apply_collision_forces(scalar_t dt); bool update(scalar_t dt); Polygon *pick_polygon(scalar_t x, scalar_t y); - void print_fig(XFigTracer *tracer); + void print_xfig(XFigTracer *tracer); void draw(SimpleWindow *window); }; diff --git a/xfig_tracer.cc b/xfig_tracer.cc new file mode 100644 index 0000000..7e61328 --- /dev/null +++ b/xfig_tracer.cc @@ -0,0 +1,89 @@ + +//////////////////////////////////////////////////////////////////// +// START_IP_HEADER // +// // +// Written by Francois Fleuret // +// Contact for comments & bug reports // +// // +// END_IP_HEADER // +//////////////////////////////////////////////////////////////////// + + // ================================================ + // (3.1) Color Pseudo-objects (user-defined colors) + // ================================================ + // This is used to define arbitrary colors beyond the 32 standard colors. + // The color objects must be defined before any other Fig objects. + + // First line: + // type name (brief description) + // ---- ---- ------------------- + // int object_code (always 0) + // int color_number (color number, from 32-543 (512 total)) + // hex string rgb values (hexadecimal string describing red, + +#include "xfig_tracer.h" + +XFigTracer::XFigTracer(const char *name) { + _file = new ofstream(name); + (*_file) << "#FIG 3.2" << endl; + (*_file) << "Portrait" << endl; + (*_file) << "Center" << endl; + (*_file) << "Metric" << endl; + (*_file) << "A4 " << endl; + (*_file) << "100.00" << endl; + (*_file) << "Single" << endl; + (*_file) << "-2" << endl; + (*_file) << "1200 2" << endl; + _nb_user_colors = 0; +} + +XFigTracer::~XFigTracer() { + _file->flush(); + delete _file; +} + +int XFigTracer::user_color(int red, int green, int blue) { + for(int c = 0; c < _nb_user_colors; c++) { + if(red == _palette_red[c] && + green == _palette_green[c] && + blue == _palette_blue[c]) return 32 + c; + } + cerr << "Unknown color!" << endl; + exit(1); +} + +void XFigTracer::add_color(int red, int green, int blue) { + for(int c = 0; c < _nb_user_colors; c++) { + if(red == _palette_red[c] && + green == _palette_green[c] && + blue == _palette_blue[c]) return; + } + if(_nb_user_colors < max_nb_user_colors) { + _palette_red[_nb_user_colors] = red; + _palette_green[_nb_user_colors] = green; + _palette_blue[_nb_user_colors] = blue; + char buffer[2]; + (*_file) << "0 " << 32 + _nb_user_colors << " #"; + sprintf(buffer, "%02x", _palette_red[_nb_user_colors]); + (*_file) << buffer; + sprintf(buffer, "%02x", _palette_green[_nb_user_colors]); + (*_file) << buffer; + sprintf(buffer, "%02x", _palette_blue[_nb_user_colors]); + (*_file) << buffer; + (*_file) << endl; + _nb_user_colors++; + } else { + cerr << "Too many colors!" << endl; + exit(1); + } +} + +void XFigTracer::draw_polygon(int red, int green, int blue, + int nb_vertices, scalar_t *x, scalar_t *y) { + int c = user_color(red, green, blue); + (*_file) << "2 3 0 1 7 " << c << " 50 -1 20 0.000 0 0 -1 0 0 " << nb_vertices + 1 << endl; + (*_file) << " "; + for(int n = 0; n < nb_vertices; n++) (*_file) << " " << int(x[n]*10) << " " << int(y[n]*10); + (*_file) << " " << int(x[0]*10) << " " << int(y[0]*10); + (*_file) << endl; +} diff --git a/xfig_tracer.h b/xfig_tracer.h new file mode 100644 index 0000000..77a24c0 --- /dev/null +++ b/xfig_tracer.h @@ -0,0 +1,37 @@ + +//////////////////////////////////////////////////////////////////// +// START_IP_HEADER // +// // +// Written by Francois Fleuret // +// Contact for comments & bug reports // +// // +// END_IP_HEADER // +//////////////////////////////////////////////////////////////////// + +#ifndef XFIG_TRACER_H +#define XFIG_TRACER_H + +#include +#include + +using namespace std; + +#include "misc.h" + +class XFigTracer { + ofstream *_file; + int _nb_user_colors; + static const int max_nb_user_colors = 100; + int _palette_red[max_nb_user_colors]; + int _palette_green[max_nb_user_colors]; + int _palette_blue[max_nb_user_colors],; + int user_color(int red, int green, int blue); +public: + XFigTracer(const char *name); + ~XFigTracer(); + void add_color(int red, int green, int blue); + void draw_polygon(int red, int green, int blue, + int nb_vertices, scalar_t *x, scalar_t *y); +}; + +#endif