Re: drawing on offscreen surface
- From: Jean Brefort <jean brefort normalesup org>
- To: Prasanta Sadhukhan <psadhukhan gmail com>
- Cc: gtk-list gnome org
- Subject: Re: drawing on offscreen surface
- Date: Fri, 10 Aug 2012 13:19:59 +0200
It is not the sme thing. With your latest code, you create surface each
time the widget needs redraw. configure events only occur when the
widget changes size.
Otherwise, you are leaking surface and off_cr.
Le vendredi 10 août 2012 à 16:41 +0530, Prasanta Sadhukhan a écrit :
> Thanks for the code and the explanation. Now, it's quite clear.
> Last thing I wanted to know is, do I need to always have
> "configure_event" handler to create the offscreen surface.
> Can't we create the offscreen surface in "expose_event" handler
> itself?
> I tried with this (after commenting "configure_event" callback) which
> also seems to do the same thing as your code.
>
> static gboolean
> draw_cb (GtkWidget *widget,
> GdkEventExpose *event,
> gpointer data)
> {
> cairo_t *off_cr, *cr;
>
> g_print("draw event\n");
>
> surface = gdk_window_create_similar_surface (event->window,
> CAIRO_CONTENT_COLOR,
> event->area.width,
> event->area.height);
>
> off_cr = cairo_create(surface);
> cairo_set_source_rgb(off_cr, 1, 0, 0);
> cairo_paint (off_cr);
>
> cairo_move_to (off_cr, 100, 100);
> cairo_set_source_rgb (off_cr, 0, 0, 0);
> cairo_set_font_size (off_cr, 20);
> cairo_show_text (off_cr, "Offscreen buffer is red");
>
>
> 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;
> }
>
> Regards
> Prasanta
> On Fri, Aug 10, 2012 at 4:14 PM, Tadej Borovšak <tadeboro gmail com>
> wrote:
> 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 gmail com
> tadej borovsak gmail com
>
>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]