[cogl/fosdem-2012: 10/17] Update all of the examples to use cogl_poll_get_info



commit 66b47848607fb19d4a390f2a53bb9bafb1c15b8c
Author: Neil Roberts <neil linux intel com>
Date:   Mon Dec 19 15:40:55 2011 +0000

    Update all of the examples to use cogl_poll_get_info
    
    The aim is that it should be a requirement that all Cogl applications
    hook their mainloops into Cogl so we should lead by examples. Most of
    the examples now just call cogl_poll_get_info and then g_poll with a
    zero timeout so that they can continue to constantly redraw.
    
    The SDL example is a bit special because SDL makes it very difficult
    to wait on either a timeout or any file descriptors. The SDL winsys is
    documented not to require blocking on any file descriptors so we can
    ignore that. It implements the timeout by adding an SDL timer which
    pushes an event to the queue to wake up SDL_GetEvent.
    
    The Cogland example was already using the glib main loop so that one
    has been updated to add the CoglGLibSource to it.

 examples/cogl-crate.c       |    8 +++
 examples/cogl-hello.c       |    8 +++
 examples/cogl-msaa.c        |    8 +++
 examples/cogl-sdl-hello.c   |  129 +++++++++++++++++++++++++++++++++---------
 examples/cogl-x11-foreign.c |    8 +++
 examples/cogland.c          |    6 ++
 6 files changed, 139 insertions(+), 28 deletions(-)
---
diff --git a/examples/cogl-crate.c b/examples/cogl-crate.c
index fa9d86f..393e670 100644
--- a/examples/cogl-crate.c
+++ b/examples/cogl-crate.c
@@ -272,8 +272,16 @@ main (int argc, char **argv)
 
   while (1)
     {
+      CoglPollFD *poll_fds;
+      int n_poll_fds;
+      gint64 timeout;
+
       paint (&data);
       cogl_framebuffer_swap_buffers (fb);
+
+      cogl_poll_get_info (ctx, &poll_fds, &n_poll_fds, &timeout);
+      g_poll ((GPollFD *) poll_fds, n_poll_fds, 0);
+      cogl_poll_dispatch (ctx, poll_fds, n_poll_fds);
     }
 
   return 0;
diff --git a/examples/cogl-hello.c b/examples/cogl-hello.c
index 775cc1a..e8e4f5b 100644
--- a/examples/cogl-hello.c
+++ b/examples/cogl-hello.c
@@ -40,9 +40,17 @@ main (int argc, char **argv)
     triangle = cogl_primitive_new_p2c4 (COGL_VERTICES_MODE_TRIANGLES,
                                         3, triangle_vertices);
     for (;;) {
+        CoglPollFD *poll_fds;
+        int n_poll_fds;
+        gint64 timeout;
+
         cogl_clear (&black, COGL_BUFFER_BIT_COLOR);
         cogl_primitive_draw (triangle);
         cogl_framebuffer_swap_buffers (fb);
+
+        cogl_poll_get_info (ctx, &poll_fds, &n_poll_fds, &timeout);
+        g_poll ((GPollFD *) poll_fds, n_poll_fds, 0);
+        cogl_poll_dispatch (ctx, poll_fds, n_poll_fds);
     }
 
     return 0;
diff --git a/examples/cogl-msaa.c b/examples/cogl-msaa.c
index 8d7d358..aecc903 100644
--- a/examples/cogl-msaa.c
+++ b/examples/cogl-msaa.c
@@ -88,6 +88,10 @@ main (int argc, char **argv)
     pipeline = cogl_pipeline_new ();
 
     for (;;) {
+        CoglPollFD *poll_fds;
+        int n_poll_fds;
+        gint64 timeout;
+
         cogl_clear (&black, COGL_BUFFER_BIT_COLOR);
 
         cogl_push_matrix ();
@@ -106,6 +110,10 @@ main (int argc, char **argv)
         cogl_rectangle (0, 1, 1, -1);
 
         cogl_framebuffer_swap_buffers (fb);
+
+        cogl_poll_get_info (ctx, &poll_fds, &n_poll_fds, &timeout);
+        g_poll ((GPollFD *) poll_fds, n_poll_fds, 0);
+        cogl_poll_dispatch (ctx, poll_fds, n_poll_fds);
     }
 
     return 0;
diff --git a/examples/cogl-sdl-hello.c b/examples/cogl-sdl-hello.c
index 315adf2..b74e8a2 100644
--- a/examples/cogl-sdl-hello.c
+++ b/examples/cogl-sdl-hello.c
@@ -12,6 +12,7 @@ typedef struct Data
   CoglPrimitive *triangle;
   float center_x, center_y;
   CoglFramebuffer *fb;
+  gboolean quit;
 } Data;
 
 static void
@@ -28,6 +29,85 @@ redraw (Data *data)
   cogl_framebuffer_swap_buffers (data->fb);
 }
 
+static void
+handle_event (Data *data, SDL_Event *event)
+{
+  switch (event->type)
+    {
+    case SDL_VIDEOEXPOSE:
+      redraw (data);
+      break;
+
+    case SDL_MOUSEMOTION:
+      {
+        int width =
+          cogl_framebuffer_get_width (COGL_FRAMEBUFFER (data->fb));
+        int height =
+          cogl_framebuffer_get_height (COGL_FRAMEBUFFER (data->fb));
+
+        data->center_x = event->motion.x * 2.0f / width - 1.0f;
+        data->center_y = event->motion.y * 2.0f / height - 1.0f;
+
+        redraw (data);
+      }
+      break;
+
+    case SDL_QUIT:
+      data->quit = TRUE;
+      break;
+    }
+}
+
+static Uint32
+timer_handler (Uint32 interval, void *user_data)
+{
+  static const SDL_UserEvent dummy_event =
+    {
+      SDL_USEREVENT
+    };
+
+  /* Post an event to wake up from SDL_WaitEvent */
+  SDL_PushEvent ((SDL_Event *) &dummy_event);
+
+  return 0;
+}
+
+static gboolean
+wait_event_with_timeout (Data *data, SDL_Event *event, gint64 timeout)
+{
+  if (timeout == -1)
+    {
+      if (SDL_WaitEvent (event))
+        return TRUE;
+      else
+        {
+          data->quit = TRUE;
+          return FALSE;
+        }
+    }
+  else if (timeout == 0)
+    return SDL_PollEvent (event);
+  else
+    {
+      gboolean ret;
+      /* Add a timer so that we can wake up the event loop */
+      SDL_TimerID timer_id =
+        SDL_AddTimer (timeout / 1000, timer_handler, data);
+
+      if (SDL_WaitEvent (event))
+        ret = TRUE;
+      else
+        {
+          data->quit = TRUE;
+          ret = FALSE;
+        }
+
+      SDL_RemoveTimer (timer_id);
+
+      return ret;
+    }
+}
+
 int
 main (int argc, char **argv)
 {
@@ -55,6 +135,8 @@ main (int argc, char **argv)
       return 1;
     }
 
+  SDL_InitSubSystem (SDL_INIT_TIMER);
+
   onscreen = cogl_onscreen_new (ctx, 800, 600);
   /* Eventually there will be an implicit allocate on first use so this
    * will become optional... */
@@ -69,6 +151,7 @@ main (int argc, char **argv)
   cogl_color_set_from_4ub (&data.black, 0, 0, 0, 255);
   data.center_x = 0.0f;
   data.center_y = 0.0f;
+  data.quit = FALSE;
 
   cogl_onscreen_show (onscreen);
 
@@ -76,37 +159,27 @@ main (int argc, char **argv)
 
   data.triangle = cogl_primitive_new_p2c4 (COGL_VERTICES_MODE_TRIANGLES,
                                            3, triangle_vertices);
-  while (SDL_WaitEvent (&event))
+  while (!data.quit)
     {
-      switch (event.type)
-        {
-        case SDL_VIDEOEXPOSE:
-          redraw (&data);
-          break;
-
-        case SDL_MOUSEMOTION:
-          {
-            int width =
-              cogl_framebuffer_get_width (COGL_FRAMEBUFFER (data.fb));
-            int height =
-              cogl_framebuffer_get_height (COGL_FRAMEBUFFER (data.fb));
-
-            data.center_x = event.motion.x * 2.0f / width - 1.0f;
-            data.center_y = event.motion.y * 2.0f / height - 1.0f;
-
-            redraw (&data);
-          }
-          break;
-
-        case SDL_QUIT:
-          goto done;
-        }
-    }
+      CoglPollFD *poll_fds;
+      int n_poll_fds;
+      gint64 timeout;
+
+      cogl_poll_get_info (ctx, &poll_fds, &n_poll_fds, &timeout);
 
-  fprintf (stderr, "Error waiting for an event: %s\n",
-           SDL_GetError ());
+      /* It's difficult to wait for file descriptors using the SDL
+         event mechanism, but it the SDL winsys is documented that it
+         will never require this so we can assert that there are no
+         fds */
+      g_assert (n_poll_fds == 0);
 
- done:
+      if (wait_event_with_timeout (&data, &event, timeout))
+        do
+          handle_event (&data, &event);
+        while (SDL_PollEvent (&event));
+
+      cogl_poll_dispatch (ctx, poll_fds, n_poll_fds);
+    }
 
   cogl_pop_framebuffer ();
 
diff --git a/examples/cogl-x11-foreign.c b/examples/cogl-x11-foreign.c
index 6c9ac40..b1d6acf 100644
--- a/examples/cogl-x11-foreign.c
+++ b/examples/cogl-x11-foreign.c
@@ -163,6 +163,10 @@ main (int argc, char **argv)
                                       3, triangle_vertices);
   for (;;)
     {
+      CoglPollFD *poll_fds;
+      int n_poll_fds;
+      gint64 timeout;
+
       while (XPending (xdpy))
         {
           XEvent event;
@@ -178,6 +182,10 @@ main (int argc, char **argv)
       cogl_clear (&black, COGL_BUFFER_BIT_COLOR);
       cogl_primitive_draw (triangle);
       cogl_framebuffer_swap_buffers (fb);
+
+      cogl_poll_get_info (ctx, &poll_fds, &n_poll_fds, &timeout);
+      g_poll ((GPollFD *) poll_fds, n_poll_fds, 0);
+      cogl_poll_dispatch (ctx, poll_fds, n_poll_fds);
     }
 
   return 0;
diff --git a/examples/cogland.c b/examples/cogland.c
index e7f0a83..03acf39 100644
--- a/examples/cogland.c
+++ b/examples/cogland.c
@@ -705,6 +705,7 @@ main (int argc, char **argv)
       {-0.7, -0.7, 0x00, 0xff, 0x00, 0xff},
       {0.7, -0.7, 0x00, 0x00, 0xff, 0xff}
   };
+  GSource *cogl_source;
 
   memset (&compositor, 0, sizeof (compositor));
 
@@ -761,6 +762,11 @@ main (int argc, char **argv)
 
   g_timeout_add (16, paint_cb, &compositor);
 
+  cogl_source = cogl_glib_source_new (compositor.cogl_context,
+                                      G_PRIORITY_DEFAULT);
+
+  g_source_attach (cogl_source, NULL);
+
   g_main_loop_run (loop);
 
   return 0;



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