From a845456cf185afc67d4fdb3aee7fed27c488b4fe Mon Sep 17 00:00:00 2001 From: Francois Fleuret Date: Sat, 27 Aug 2016 23:51:30 +0200 Subject: [PATCH] Cleaned up a bit. SimpleWindow now embed the cairo fields to allow proper deallocation in the destructor. --- main.cc | 15 ++++++++++++--- simple_window.cc | 19 ++++++++++++------- simple_window.h | 5 +++++ universe.h | 3 +++ 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/main.cc b/main.cc index e135189..5a1d562 100644 --- a/main.cc +++ b/main.cc @@ -32,11 +32,15 @@ static cairo_status_t write_cairo_to_file(void *closure, 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, @@ -47,8 +51,13 @@ void generate_png(int width, int height, Universe *universe, FILE *file) { 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); @@ -460,7 +469,7 @@ int main(int argc, char **argv) { #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; } diff --git a/simple_window.cc b/simple_window.cc index ae981ed..cbb3506 100644 --- a/simple_window.cc +++ b/simple_window.cc @@ -77,6 +77,13 @@ SimpleWindow::SimpleWindow(const char *name, int x, int y, int w, int h) { 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(); } @@ -85,6 +92,10 @@ SimpleWindow::~SimpleWindow() { XUnmapWindow(_display, _window); XDestroyWindow(_display, _window); XCloseDisplay(_display); +#ifdef CAIRO_SUPPORT + cairo_destroy(_cairo_context); + cairo_surface_destroy(_cairo_surface); +#endif } int SimpleWindow::width() { @@ -214,12 +225,6 @@ SimpleEvent SimpleWindow::event() { #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 diff --git a/simple_window.h b/simple_window.h index 6339609..5981aef 100644 --- a/simple_window.h +++ b/simple_window.h @@ -43,6 +43,11 @@ class SimpleWindow { 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; diff --git a/universe.h b/universe.h index 3bbbf4a..77980b8 100644 --- a/universe.h +++ b/universe.h @@ -24,6 +24,9 @@ public: 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(); -- 2.39.5