Advertisement
Guest User

Cairo animation example

a guest
Oct 26th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.32 KB | None | 0 0
  1. /**
  2.  * A simple animation using cairo and GTK+
  3.  *
  4.  * This program shows high /usr/libexec/Xorg load while running full screen on Linux
  5.  *
  6.  * Compile with:
  7.  *   gcc `pkg-config --cflags --libs gtk+-3.0` -lm main.c
  8.  */
  9.  
  10. #include <gtk/gtk.h>
  11. #include <math.h>
  12.  
  13. #define NUM_WINDOWS (1u)
  14. #define NUM_POINTS (1000u)
  15. #define PERIOD (100u)
  16.  
  17. /* Local function prototypes */
  18. static gboolean invalidate_cb(void *);
  19. static gboolean drawing_area_draw_cb(GtkWidget *, cairo_t *, void *);
  20.  
  21. int main(int argc, char **argv)
  22. {
  23.     int i;
  24.     gtk_init(&argc, &argv);
  25.    
  26.     for (i = 0; i < NUM_WINDOWS; i++)
  27.     {
  28.         GtkWidget *main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  29.         gtk_window_set_title(GTK_WINDOW(main_window), "Drawing example");
  30.         gtk_window_set_default_size(GTK_WINDOW(main_window), 400, 400);
  31.         GtkWidget *drawing_area = gtk_drawing_area_new();
  32.        
  33.         gtk_container_add(GTK_CONTAINER(main_window), drawing_area);
  34.         gtk_widget_show_all(main_window);
  35.  
  36.         /* Create a  timer to invalidate our window at 60Hz */
  37.         g_timeout_add(1000 / 60, invalidate_cb, drawing_area);
  38.  
  39.         /* Connect our redraw callback */
  40.         g_signal_connect(drawing_area, "draw", G_CALLBACK(drawing_area_draw_cb), NULL);
  41.  
  42.         /* Connect the destroy signal */
  43.         g_signal_connect(main_window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
  44.     }
  45.     gtk_main();
  46. }
  47.  
  48. static gboolean invalidate_cb(void *ptr)
  49. {
  50.     if (GTK_IS_WIDGET(ptr))
  51.     {
  52.         gtk_widget_queue_draw(GTK_WIDGET(ptr));
  53.         return TRUE;
  54.     }  
  55.     return FALSE;
  56. }
  57.  
  58. static inline float sine_to_point(int x, int width, int height)
  59. {
  60.  
  61.     return (height / 2.0) * sin(x * 2 * M_PI / (PERIOD)) + height / 2.0;
  62. }
  63.  
  64. static gboolean drawing_area_draw_cb(GtkWidget *widget, cairo_t *context, void *ptr)
  65. {
  66.     static int redraw_number = 0;
  67.     int width, height, i;
  68.     width = gtk_widget_get_allocated_width(widget);
  69.     height = gtk_widget_get_allocated_height(widget);
  70.  
  71.     /* Draw the background */  
  72.     cairo_set_source_rgb(context, 1, 1, 1);
  73.     cairo_rectangle(context, 0, 0, width, height);
  74.     cairo_fill(context);
  75.  
  76.     /* Draw a moving sine wave */
  77.     cairo_set_source_rgb(context, 0.5, 0.5, 0);
  78.     cairo_move_to(context, 0, sine_to_point(0 + redraw_number, width, height));
  79.     for (i = 1; i < NUM_POINTS; i++)
  80.     {
  81.         cairo_line_to(context,  i, sine_to_point(i + redraw_number, width, height));   
  82.     }  
  83.     cairo_stroke(context);
  84.  
  85.     redraw_number++;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement