Advertisement
Ancurio

cairo_rotate.c

Jun 12th, 2012
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.16 KB | None | 0 0
  1. #include <cairo.h>
  2. #include <gtk/gtk.h>
  3.  
  4. cairo_surface_t *image;
  5. cairo_t *cr;
  6. gdouble rotation = 0;
  7. GtkWidget *window;
  8.  
  9. gint image_w, image_h;
  10.  
  11.  
  12. gboolean rotate_cb( void )
  13. {
  14.     // Any rotation applied to cr here will be lost, as we create
  15.     // a new cairo context on every expose event
  16.     //cairo_rotate (cr, 4);
  17.     rotation += 0.1;
  18.     //cairo_paint(cr);
  19. //  printf("rotating\n");
  20.     // Tell our window that it should repaint itself (ie. emit an expose event)
  21.     gtk_widget_queue_draw(window);
  22.  
  23.     return( TRUE );
  24. }
  25.  
  26. static gboolean on_expose_event(GtkWidget *widget, GdkEventExpose *event,gpointer data)
  27. {
  28.   // Make sure our window wasn't destroyed yet
  29.   // (to silence a warning)
  30.   g_return_if_fail(GTK_IS_WIDGET(widget));
  31.  
  32.   cr = gdk_cairo_create (widget->window);
  33.   // We need to apply transformation before setting the source surface
  34.   // We translate (0, 0) to the center of the screen,
  35.   // so we can rotate the image around its center point,
  36.   // not its upper left corner
  37.   cairo_translate(cr, image_w/2, image_h/2);
  38.   cairo_rotate(cr, rotation);
  39.   cairo_set_source_surface(cr, image, -image_w/2, -image_h/2);
  40.   // We need to clip around the image, or cairo will paint garbage data
  41.   cairo_rectangle(cr, -image_w/2, -image_h/2, image_w, image_h);
  42.   cairo_clip(cr);
  43.  
  44.   cairo_paint(cr);
  45. //  printf("Paint\n");
  46.   cairo_destroy(cr);
  47.   return FALSE;
  48. }
  49.  
  50.  
  51. int main(int argc, char *argv[])
  52. {
  53.   image = cairo_image_surface_create_from_png("wheel.png");
  54.   image_w = cairo_image_surface_get_width(image);
  55.   image_h = cairo_image_surface_get_height(image);
  56.  
  57.   gtk_init(&argc, &argv);
  58.  
  59.   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  60.  
  61.   g_signal_connect(window, "expose-event",
  62.       G_CALLBACK (on_expose_event), NULL);
  63.   g_signal_connect(window, "destroy",
  64.       G_CALLBACK (gtk_main_quit), NULL);
  65.  
  66.   gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  67.   gtk_window_set_default_size(GTK_WINDOW(window), image_w, image_h);
  68.   gtk_widget_set_app_paintable(window, TRUE);
  69.  
  70.   gtk_widget_show_all(window);
  71.   g_timeout_add(40, (GSourceFunc) rotate_cb, NULL);
  72.  
  73.   gtk_main();
  74. //  cairo_destroy(cr);
  75.   cairo_surface_destroy(image);
  76.  
  77.   return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement