I'm working on a GtkDrawingArea which, in the expose event, is drawing a section of a pixbuf in a tiled manner. The tiling type varies - depending on the source image; it can be orthogonal, isometric or hexagonal. Drawing this with gdk_draw_pixmap is simple enough; here is an example on how it looks for isometric tiling:
for(y=0,row=0; y+tile_height<height; y+=tile_half_height,++row)
for(x=((row&1)? tile_half_width : 0); x+tile_width<width; x+=tile_width)
gdk_draw_pixbuf(widget->window,NULL,
pixbuf,src_x,src_y,
x,y,tile_width,tile_height,
GDK_RGB_DITHER_NONE,0,0);
Result:
However, drawing the same with Cairo proved to be quite not the same. This is what I have so far, which isn't working:
cairo_t *cr = gdk_cairo_create(widget->window);
gdk_cairo_set_source_pixbuf(cr,pixbuf,src_x,src_y);
for(y=0,row=0; y+tile_height<height; y+=tile_half_height,++row)
for(x=((row&1)? tile_half_width : 0); x+tile_width<width; x+=tile_width) {
cairo_rectangle(cr,x,y,tile_width,tile_height);
cairo_paint(cr);
}
}
cairo_destroy(cr);
Result:
Cairo simply refuses to draw the image as a normal raster image would be drawn. What am I doing wrong, how is this solved?
cairo_paint()
fills the clip region, not the current path; you will need to convert the path you created withcairo_rectangle()
into a clip region withcairo_clip()
. (Remember that the clip region is preserved over time, so you will also needcairo_save()
andcairo_restore()
in your for loop.) – andlabscairo_fill
handles image sources; you could try that. I've only ever used it with solid colors. – oldtechaacairo_translate()
, but it seems trial and error amounts to infinity in this case, that's why I ask. – Per Löwgren