Skip to content

Instantly share code, notes, and snippets.

@napsy
Created May 31, 2010 13:13
Show Gist options
  • Save napsy/419822 to your computer and use it in GitHub Desktop.
Save napsy/419822 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <gtk/gtk.h>
struct MainWindow {
GtkWidget *window;
gboolean draw;
GAsyncQueue *draw_queue;
cairo_surface_t *surface;
} *main_window;
gpointer main_window_painter(gpointer data)
{
int w = 10, h = 20, flip = 0;
while (1) {
g_usleep(1000);
cairo_t *c = cairo_create(main_window->surface);
if (w > 400) {
w = 10; h = 20;
cairo_set_source_rgb(c, 1, 1, 1);
cairo_rectangle(c, 0, 0, 640, 480);
cairo_fill(c);
}
cairo_set_source_rgb(c, 0.3, 0.7, 0.2);
cairo_rectangle(c, 0, 0, w++, h++);
cairo_fill(c);
cairo_destroy(c);
}
return NULL;
}
gboolean main_window_exposer(gpointer data)
{
gtk_widget_queue_draw(main_window->window);
return TRUE;
}
gboolean main_window_expose(GtkWidget *widget, GdkEventExpose *e)
{
cairo_t *c = gdk_cairo_create(widget->window);
cairo_set_source_surface(c, main_window->surface, 0, 0);
cairo_rectangle(c, 0, 0, 640, 480);
cairo_fill(c);
cairo_destroy(c);
return TRUE;
}
void main_window_new()
{
main_window = g_malloc(sizeof(*main_window));
main_window->draw = FALSE;
main_window->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
main_window->surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 640, 480);
gtk_window_set_title(GTK_WINDOW(main_window->window), "Animation example");
gtk_window_set_default_size(GTK_WINDOW(main_window->window), 640, 480);
/* Refresh at 25 fps */
g_timeout_add(40, main_window_exposer, NULL);
g_signal_connect(G_OBJECT(main_window->window), "expose-event", G_CALLBACK(main_window_expose), NULL);
gtk_widget_show_all(main_window->window);
}
int main(int argc, char **argv)
{
GError *error = NULL;
g_thread_init(NULL);
gdk_threads_init();
gdk_threads_enter();
gtk_init(&argc, &argv);
g_thread_create(main_window_painter, NULL, FALSE, &error);
main_window_new();
gtk_main();
gdk_threads_leave();
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment