return CAIRO_STATUS_SUCCESS;
}
-void generate_png(int width, int height, Universe *universe, FILE *file) {
+void generate_png(Universe *universe, scalar_t scale, FILE *file) {
const int depth = 4;
+ const int width = int(universe->width() * scale);
+ const int height = int(universe->height() * scale);
+
cairo_surface_t *image;
cairo_t* context_resource;
unsigned char *data;
+
data = new unsigned char [width * height * depth];
image = cairo_image_surface_create_for_data(data,
context_resource = cairo_create(image);
+ cairo_scale(context_resource, scale, scale);
+
cairo_set_source_rgb(context_resource, 1.0, 1.0, 1.0);
- cairo_rectangle(context_resource, 0, 0, width, height);
+
+ cairo_rectangle(context_resource, 0, 0,
+ universe->width(), universe->height());
+
cairo_fill(context_resource);
universe->draw(context_resource);
#ifdef CAIRO_SUPPORT
{
FILE *file = fopen("/tmp/universe.png", "w");
- generate_png(task->width(), task->height(), &universe, file);
+ generate_png(&universe, 0.25, file);
// generate_png(task->width(), task->height(), &universe, "/tmp/universe.png");
cout << "Universe image saved in /tmp/universe.png" << endl;
}
XSetState(_display, _gc, 0, 0, GXcopy, AllPlanes);
XFillRectangle(_display, _pixmap, _gc, 0, 0, _width, _height);
XFlush(_display);
+
+#ifdef CAIRO_SUPPORT
+ _cairo_surface = cairo_xlib_surface_create(_display, _pixmap, _visual, _width, _height);
+ cairo_xlib_surface_set_size(_cairo_surface, _width, _height);
+ _cairo_context = cairo_create(_cairo_surface);
+#endif
+
} else abort();
}
XUnmapWindow(_display, _window);
XDestroyWindow(_display, _window);
XCloseDisplay(_display);
+#ifdef CAIRO_SUPPORT
+ cairo_destroy(_cairo_context);
+ cairo_surface_destroy(_cairo_surface);
+#endif
}
int SimpleWindow::width() {
#ifdef CAIRO_SUPPORT
cairo_t *SimpleWindow::get_cairo_context_resource() {
- cairo_surface_t *surface;
-
- surface = cairo_xlib_surface_create(_display, _pixmap, _visual, _width, _height);
-
- cairo_xlib_surface_set_size(surface, _width, _height);
-
- return cairo_create(surface);
+ return _cairo_context;
}
#endif
Pixmap _pixmap;
GC _gc;
+#ifdef CAIRO_SUPPORT
+ cairo_t *_cairo_context;
+ cairo_surface_t *_cairo_surface;
+#endif
+
protected:
int _red_mask, _green_mask, _blue_mask;
int _red_shift, _green_shift, _blue_shift;
int _nb_max_polygons, _nb_polygons;
Polygon **_polygons;
+ inline scalar_t width() { return _xmax; }
+ inline scalar_t height() { return _ymax; }
+
Universe(int nb_max_polygons, scalar_t xmax, scalar_t ymax);
// The destructor deletes all the added polygons
~Universe();