Re: drawing on offscreen surface



Thanks for the code.
However, it does not draw anything. I assume the draw rectangle need to be done in draw_cb() so I added this code in
  cairo_set_source_surface(cr, surface, 0, 0);
  cairo_set_source_rgb(cr, 0, 1, 0);
  cairo_rectangle(cr, 300, 300, 200, 200);
  cairo_stroke(cr);
  cairo_paint(cr);
 
but it still does not draw any rectangle. Do I not need to have "expose" handler to draw?

Also, I would like to know how to render contents from main window AND from offscreen both together on main visible window
say, a primitive (Rectangle) on main window and another primitive (circle) from offscreen? Could you please show me how?

Thanks in advance!!

Regards
Prasanta
On Thu, Aug 9, 2012 at 4:59 PM, Tadej Borovšak <tadeboro gmail com> wrote:
Hi.

There is a lot of unnecessary code in your example. I attached a
cleaned-up example that demonstrates how to handle situations like
yours.

Cheers,
Tadej


============================
#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_destroy(cr);

  return FALSE;
}

static gboolean
draw_cb (GtkWidget *widget,
         cairo_t   *cr,
         gpointer   data)
{
  g_print("draw event\n");

  cairo_set_source_surface(cr, surface, 0, 0);
  cairo_paint(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, "draw",
                    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;
}


--
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]