cairo graphics 0.6 Circle animation

/* Jeffrin Jose Licensed GPL v3 Copyright
August 2010 GPL --> */


//the global pixmap that will serve as our buffer
static GdkPixmap *pixmap = NULL;

gboolean on_window_configure_event(GtkWidget * da, GdkEventConfigure * event, gpointer user_data){
static int oldw = 0;
static int oldh = 0;
//make our selves a properly sized pixmap if our window has been resized
if (oldw != event->width || oldh != event->height){
//create our new pixmap with the correct size.
GdkPixmap *tmppixmap = gdk_pixmap_new(da->window, event->width, event->height, -1);
//copy the contents of the old pixmap to the new pixmap. This keeps ugly uninitialized
//pixmaps from being painted upon resize
int minw = oldw, minh = oldh;
if( event->width width; }
if( event->height height; }
gdk_draw_drawable(tmppixmap, da->style->fg_gc[GTK_WIDGET_STATE(da)], pixmap, 0, 0, 0, 0, minw, minh);
//we're done with our old pixmap, so we can get rid of it and replace it with our properly-sized one.
pixmap = tmppixmap;
oldw = event->width;
oldh = event->height;
return TRUE;

gboolean on_window_expose_event(GtkWidget * da, GdkEventExpose * event, gpointer user_data){
da->style->fg_gc[GTK_WIDGET_STATE(da)], pixmap,
// Only copy the area that was exposed.
event->area.x, event->area.y,
event->area.x, event->area.y,
event->area.width, event->area.height);
return TRUE;

static int currently_drawing = 0;
//do_draw will be executed in a separate thread whenever we would like to update
//our animation
void *do_draw(void *ptr){

currently_drawing = 1;

int width, height;
gdk_drawable_get_size(pixmap, &width, &height);

//create a gtk-independant surface to draw on
cairo_surface_t *cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
cairo_t *cr = cairo_create(cst);

//do some time-consuming drawing
static int i = 0;
++i; i = i % 300; //give a little movement to our animation
cairo_set_source_rgb (cr, .9, .9, .9);
cairo_set_source_rgb (cr, 0.8, 0.5, 0.9);
cairo_move_to(cr, 50,50);

int j,k;
/* for(k=0; k<100; ++k){*/ //lets just redraw lots of times to use a lot of proc power
for(j=0; j window,700,700,-1);
//because we will be painting our pixmap manually during expose events
//we can turn off gtk's automatic painting and double buffering routines.
gtk_widget_set_app_paintable(window, TRUE);
gtk_widget_set_double_buffered(window, FALSE);

(void)g_timeout_add(33, (GSourceFunc)timer_exe, window);


return 0;

%d bloggers like this: