Re: drawing on offscreen surface



I have added the drawing of green rectangle in configure_event_cb() code where I am creating the surface.
Now, when I run this I get BOTH the green rectangle and blue rectangle on the main window.
I assume since I am not calling   cairo_set_source_surface() in configure_event_cb() that means the green rectangle is being drawn on main window and NOT on offscreen surface, whereas
in expose_event(), since I am calling cairo_set_source_surface() and drawing blue rectangle on that surface, it is getting drawn on offscreen.
Please confirm and second my understanding.

Thank you all for your help.

#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, 0, 0, 0);
    cairo_paint(cr);

  cairo_set_source_rgb(cr, 0, 1, 0);
  cairo_rectangle(cr, 100, 100, 200, 200);
  cairo_stroke(cr);

    return FALSE;
}

static gboolean
expose_event_offscr (GtkWidget *widget,GdkEventExpose *event, gpointer   data)
{
  g_print("draw event offscr\n");

  cairo_t *cr = gdk_cairo_create(widget->window);
  cairo_set_source_surface(cr, surface, 0, 0);
  cairo_paint(cr);

  cairo_set_source_rgb(cr, 0, 0, 1);
  cairo_rectangle(cr, 300, 300, 200, 200);
  cairo_stroke(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 (expose_event_offscr), 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;
}


On Fri, Aug 10, 2012 at 2:38 PM, Jean Brefort <jean brefort normalesup org> wrote:
I suppose that surface is the offscreen scene. In that case, ther bule
rectangle should not be drawn during the expose event callback, but when
you build surface. Then you can use the code in my previous message,
replacing the blue color by green color.

Hope this helps.

Le vendredi 10 août 2012 à 14:34 +0530, Prasanta Sadhukhan a écrit :
> Thanks for your code. This draws a blue rectangle on a black
> background and the offscreen surface contents is rendered on main
> window.
> But I also would like to draw green rectangle in main window so after
> final rendering of offscreen contents (the blue rectangle) on main
> window, I would get both blue and green rectangles on main window.
> Can you please clarify how can I achieve that (both offscreen & main
> window contents being shown on main window)?
>
> Thanks in advance!!
>
> Regards
> Prasanta
>
> On Fri, Aug 10, 2012 at 2:22 PM, Jean Brefort
> <jean brefort normalesup org> wrote:
>         You should use something like:
>
>         cairo_set_source_surface(cr, surface, 0, 0);
>         cairo_paint(cr).
>         cairo_set_source_rgb(cr, 0, 0, 1);
>         cairo_rectangle(cr, 300,300, 200, 200);
>         cairo_stroke(cr);
>
>
>         This will paint the window with surface as source and then
>         draw a blue
>         rectangle.
>
>         Le vendredi 10 août 2012 à 14:09 +0530, Prasanta Sadhukhan a
>         écrit :
>         >
>         >
>         > On Fri, Aug 10, 2012 at 1:22 PM, Jean Brefort
>         > <jean brefort normalesup org> wrote:
>         >         This code is quite strange. Your call to
>         >         cairo_set_source_surface() is
>         >         not useful since you use cairo_set_source_rgb() just
>         after.
>         >         You should
>         >         call cairo_paint() just after
>         cairo_set_source_surface() if
>         >         you want it
>         >         to have any effect.
>         >
>         > If I do not call     cairo_set_source_rgb(cr, 0, 0, 1); how
>         will get a
>         > blue rectangle?
>         > Just having   these is giving me a window with black
>         background.
>         > cairo_set_source_surface(cr, surface, 0, 0);
>         > cairo_rectangle(cr, 300,300, 200, 200);
>         > cairo_stroke(cr);
>         > cairo_paint(cr).
>         >
>         > Can you please clarify?
>         > As I told, I would like to get a blue rectangle in offscreen
>         surface
>         > and a green rectangle in main visible surface so after final
>         rendering
>         > of offscreen contents on main window, I would get both
>         rectangles on
>         > main window.
>         >
>         > Regards
>         > Prasanta
>         >
>         >         Hope it helps,
>         >         Jean
>         >
>         >         Le vendredi 10 août 2012 à 13:07 +0530, Prasanta
>         Sadhukhan a
>         >         écrit :
>         >         > expose_event (GtkWidget *widget,GdkEventExpose
>         *event,
>         >         gpointer
>         >         > data)
>         >         > {
>         >         >   g_print("draw event main\n");
>         >         >
>         >         >   cairo_t *cr = gdk_cairo_create(widget->window);
>         >         > // draw blue rectangle into offscreen
>         >         >   cairo_set_source_surface(cr, surface, 0, 0);
>         >         >   cairo_set_source_rgb(cr, 0, 0, 1);
>         >         >   cairo_rectangle(cr, 300, 300, 200, 200);
>         >         >   cairo_stroke(cr);
>         >         >   cairo_paint(cr);
>         >         >
>         >         > //draw green rectangle onto main window
>         >         >     cairo_set_source_rgb(cr, 0, 1, 0);
>         >         >   cairo_rectangle(cr, 100, 100, 200, 200);
>         >         >    cairo_stroke(cr)
>         >         > }
>         >         >
>         >         >
>         >         >
>         >
>         >
>         >         _______________________________________________
>         >         gtk-list mailing list
>         >         gtk-list gnome org
>         >         https://mail.gnome.org/mailman/listinfo/gtk-list
>         >
>
>
>
>





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