Re: drawing on offscreen surface

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello.

Have a look at this code (targeted at GTK+-2.x):

#include <gtk/gtk.h>

static cairo_surface_t *surface = NULL;

static gboolean
configure_event_cb (GtkWidget         *widget,
                    GdkEventConfigure *event,
                    gpointer           data)
{
  cairo_t *cr;

  g_print("configure event: %d, %d\n", event->width, event->height);

  if (surface)
    cairo_surface_destroy (surface);

  surface = gdk_window_create_similar_surface (event->window,
                                               CAIRO_CONTENT_COLOR,
                                               event->width,
                                               event->height);

  cr = cairo_create(surface);
  cairo_set_source_rgb(cr, 1, 0, 0);
  cairo_paint (cr);

  cairo_move_to (cr, 100, 100);
  cairo_set_source_rgb (cr, 0, 0, 0);
  cairo_set_font_size (cr, 20);
  cairo_show_text (cr, "Offscreen buffer is red");

  cairo_destroy(cr);

  return FALSE;
}

static gboolean
draw_cb (GtkWidget      *widget,
         GdkEventExpose *event,
         gpointer        data)
{
  cairo_t *cr;

  g_print("draw event\n");

  cr = gdk_cairo_create (event->window);

  cairo_set_source_surface(cr, surface, 0, 0);
  cairo_paint(cr);

  cairo_move_to (cr, 200, 200);
  cairo_set_source_rgb (cr, 0, 1, 0);
  cairo_set_font_size (cr, 20);
  cairo_show_text (cr, "This text is not blitted");

  cairo_destroy (cr);

  return TRUE;
}

int
main(int    argc,
     char **argv)
{
  GtkWidget *window,
            *area;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size (GTK_WINDOW (window), 1200, 800);
  g_signal_connect (window, "destroy", gtk_main_quit, NULL);

  area = gtk_drawing_area_new();
  g_signal_connect (area, "expose-event",
                    G_CALLBACK (draw_cb), NULL);
  g_signal_connect (area, "configure-event",
                    G_CALLBACK(configure_event_cb), NULL);
  gtk_container_add (GTK_CONTAINER (window), area);

  gtk_widget_show_all(window);
  gtk_main();

  return 0;
}


surface is your offscreen buffer and contains red background and black
text. This is then blitted during expose-event and to finish off, some
green text is added. And that's it.

Cheers,
Tadej


-- 
Tadej Borovšak
blog.borovsak.si
tadeboro@xxxxxxxxx
tadej.borovsak@xxxxxxxxx
_______________________________________________
gtk-list mailing list
gtk-list@xxxxxxxxx
https://mail.gnome.org/mailman/listinfo/gtk-list



[Index of Archives]     [Touch Screen Library]     [GIMP Users]     [Gnome]     [KDE]     [Yosemite News]     [Steve's Art]

  Powered by Linux