Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cairo.h>
- #include <gtk/gtk.h>
- cairo_surface_t *image;
- cairo_t *cr;
- gdouble rotation = 0;
- GtkWidget *window;
- gint image_w, image_h;
- gboolean rotate_cb( void )
- {
- // Any rotation applied to cr here will be lost, as we create
- // a new cairo context on every expose event
- //cairo_rotate (cr, 4);
- rotation += 0.1;
- //cairo_paint(cr);
- // printf("rotating\n");
- // Tell our window that it should repaint itself (ie. emit an expose event)
- gtk_widget_queue_draw(window);
- return( TRUE );
- }
- static gboolean on_expose_event(GtkWidget *widget, GdkEventExpose *event,gpointer data)
- {
- // Make sure our window wasn't destroyed yet
- // (to silence a warning)
- g_return_if_fail(GTK_IS_WIDGET(widget));
- cr = gdk_cairo_create (widget->window);
- // We need to apply transformation before setting the source surface
- // We translate (0, 0) to the center of the screen,
- // so we can rotate the image around its center point,
- // not its upper left corner
- cairo_translate(cr, image_w/2, image_h/2);
- cairo_rotate(cr, rotation);
- cairo_set_source_surface(cr, image, -image_w/2, -image_h/2);
- // We need to clip around the image, or cairo will paint garbage data
- cairo_rectangle(cr, -image_w/2, -image_h/2, image_w, image_h);
- cairo_clip(cr);
- cairo_paint(cr);
- // printf("Paint\n");
- cairo_destroy(cr);
- return FALSE;
- }
- int main(int argc, char *argv[])
- {
- image = cairo_image_surface_create_from_png("wheel.png");
- image_w = cairo_image_surface_get_width(image);
- image_h = cairo_image_surface_get_height(image);
- gtk_init(&argc, &argv);
- window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
- g_signal_connect(window, "expose-event",
- G_CALLBACK (on_expose_event), NULL);
- g_signal_connect(window, "destroy",
- G_CALLBACK (gtk_main_quit), NULL);
- gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
- gtk_window_set_default_size(GTK_WINDOW(window), image_w, image_h);
- gtk_widget_set_app_paintable(window, TRUE);
- gtk_widget_show_all(window);
- g_timeout_add(40, (GSourceFunc) rotate_cb, NULL);
- gtk_main();
- // cairo_destroy(cr);
- cairo_surface_destroy(image);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement