Convert pixbuf animation demo (both copies) to GdkFrameClock

Use the frame clock instead of a 50ms timeout to animate the
spinning pixbufs.

https://bugzilla.gnome.org/show_bug.cgi?id=694302
This commit is contained in:
Owen W. Taylor
2013-02-20 14:46:11 -05:00
parent df3e19b449
commit 983e266fad
2 changed files with 29 additions and 38 deletions

View File

@ -16,8 +16,6 @@
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <math.h> #include <math.h>
#define FRAME_DELAY 50
#define BACKGROUND_NAME "/pixbufs/background.jpg" #define BACKGROUND_NAME "/pixbufs/background.jpg"
static const char *image_names[] = { static const char *image_names[] = {
@ -88,14 +86,17 @@ draw_cb (GtkWidget *widget,
return TRUE; return TRUE;
} }
#define CYCLE_LEN 60 #define CYCLE_TIME 3000000 /* 3 seconds */
static int frame_num; static gint64 start_time;
/* Timeout handler to regenerate the frame */ /* Handler to regenerate the frame */
static gint static gboolean
timeout (gpointer data) on_tick (GtkWidget *widget,
GdkFrameClock *frame_clock,
gpointer data)
{ {
gint64 current_time;
double f; double f;
int i; int i;
double xmid, ymid; double xmid, ymid;
@ -104,7 +105,11 @@ timeout (gpointer data)
gdk_pixbuf_copy_area (background, 0, 0, back_width, back_height, gdk_pixbuf_copy_area (background, 0, 0, back_width, back_height,
frame, 0, 0); frame, 0, 0);
f = (double) (frame_num % CYCLE_LEN) / CYCLE_LEN; if (start_time == 0)
start_time = gdk_frame_clock_get_frame_time (frame_clock);
current_time = gdk_frame_clock_get_frame_time (frame_clock);
f = ((current_time - start_time) % CYCLE_TIME) / (double)CYCLE_TIME;
xmid = back_width / 2.0; xmid = back_width / 2.0;
ymid = back_height / 2.0; ymid = back_height / 2.0;
@ -159,20 +164,9 @@ timeout (gpointer data)
gtk_widget_queue_draw (da); gtk_widget_queue_draw (da);
frame_num++;
return G_SOURCE_CONTINUE; return G_SOURCE_CONTINUE;
} }
static guint timeout_id;
static void
cleanup_callback (GObject *object,
gpointer data)
{
g_source_remove (timeout_id);
timeout_id = 0;
}
GtkWidget * GtkWidget *
do_pixbufs (GtkWidget *do_widget) do_pixbufs (GtkWidget *do_widget)
{ {
@ -188,8 +182,6 @@ do_pixbufs (GtkWidget *do_widget)
g_signal_connect (window, "destroy", g_signal_connect (window, "destroy",
G_CALLBACK (gtk_widget_destroyed), &window); G_CALLBACK (gtk_widget_destroyed), &window);
g_signal_connect (window, "destroy",
G_CALLBACK (cleanup_callback), NULL);
error = NULL; error = NULL;
@ -224,7 +216,7 @@ do_pixbufs (GtkWidget *do_widget)
gtk_container_add (GTK_CONTAINER (window), da); gtk_container_add (GTK_CONTAINER (window), da);
timeout_id = g_timeout_add (FRAME_DELAY, timeout, NULL); gtk_widget_add_tick_callback (da, on_tick, NULL, NULL);
} }
} }

View File

@ -25,8 +25,6 @@
#define FRAME_DELAY 50
#define BACKGROUND_NAME "background.jpg" #define BACKGROUND_NAME "background.jpg"
static const char *image_names[] = { static const char *image_names[] = {
@ -93,14 +91,17 @@ draw_cb (GtkWidget *widget, cairo_t *cr, gpointer data)
return TRUE; return TRUE;
} }
#define CYCLE_LEN 60 #define CYCLE_TIME 3000000 /* 3 seconds */
static int frame_num; static gint64 start_time;
/* Timeout handler to regenerate the frame */ /* Handler to regenerate the frame */
static gint static gboolean
timeout (gpointer data) on_tick (GtkWidget *widget,
GdkFrameClock *frame_clock,
gpointer data)
{ {
gint64 current_time;
double f; double f;
int i; int i;
double xmid, ymid; double xmid, ymid;
@ -109,7 +110,11 @@ timeout (gpointer data)
gdk_pixbuf_copy_area (background, 0, 0, back_width, back_height, gdk_pixbuf_copy_area (background, 0, 0, back_width, back_height,
frame, 0, 0); frame, 0, 0);
f = (double) (frame_num % CYCLE_LEN) / CYCLE_LEN; if (start_time == 0)
start_time = gdk_frame_clock_get_frame_time (frame_clock);
current_time = gdk_frame_clock_get_frame_time (frame_clock);
f = ((current_time - start_time) % CYCLE_TIME) / (double)CYCLE_TIME;
xmid = back_width / 2.0; xmid = back_width / 2.0;
ymid = back_height / 2.0; ymid = back_height / 2.0;
@ -163,19 +168,13 @@ timeout (gpointer data)
gtk_widget_queue_draw (da); gtk_widget_queue_draw (da);
frame_num++; return G_SOURCE_CONTINUE;
return TRUE;
} }
static guint timeout_id;
/* Destroy handler for the window */ /* Destroy handler for the window */
static void static void
destroy_cb (GObject *object, gpointer data) destroy_cb (GObject *object, gpointer data)
{ {
g_source_remove (timeout_id);
timeout_id = 0;
gtk_main_quit (); gtk_main_quit ();
} }
@ -208,7 +207,7 @@ main (int argc, char **argv)
gtk_container_add (GTK_CONTAINER (window), da); gtk_container_add (GTK_CONTAINER (window), da);
timeout_id = gdk_threads_add_timeout (FRAME_DELAY, timeout, NULL); gtk_widget_add_tick_callback (da, on_tick, NULL, NULL);
gtk_widget_show_all (window); gtk_widget_show_all (window);
gtk_main (); gtk_main ();