[cogl] Rotate according to time not number of frames in the crate example



commit dedee399d622d4453963bf4449a21101851fca2c
Author: Neil Roberts <neil linux intel com>
Date:   Mon Oct 17 13:20:08 2011 +0100

    Rotate according to time not number of frames in the crate example
    
    Previously the crate example incremented the angle of rotation of the
    cube every frame so depending on the framerate the cube might rotate
    too fast to see. This just changes it to calculate the rotation based
    on the elapsed time using a GTimer. The rate that frames are drawn is
    unaffected.
    
    Reviewed-by: Robert Bragg <robert linux intel com>

 examples/cogl-crate.c |   22 ++++++++++++++--------
 1 files changed, 14 insertions(+), 8 deletions(-)
---
diff --git a/examples/cogl-crate.c b/examples/cogl-crate.c
index 506b931..d997b6d 100644
--- a/examples/cogl-crate.c
+++ b/examples/cogl-crate.c
@@ -13,11 +13,6 @@ typedef struct _Data
   CoglTexture *texture;
   CoglPipeline *crate_pipeline;
 
-  /* The cube continually rotates around each axis. */
-  float rotate_x;
-  float rotate_y;
-  float rotate_z;
-
   CoglPangoFontMap *pango_font_map;
   PangoContext *pango_context;
   PangoFontDescription *pango_font_desc;
@@ -26,6 +21,8 @@ typedef struct _Data
   int hello_label_width;
   int hello_label_height;
 
+  GTimer *timer;
+
 } Data;
 
 /* A static identity matrix initialized for convenience. */
@@ -97,6 +94,8 @@ CoglVertexP3T2 vertices[] =
 static void
 paint (Data *data)
 {
+  float rotation;
+
   cogl_clear (&black, COGL_BUFFER_BIT_COLOR|COGL_BUFFER_BIT_DEPTH);
 
   cogl_push_matrix ();
@@ -105,6 +104,11 @@ paint (Data *data)
 
   cogl_scale (75, 75, 75);
 
+  /* Update the rotation based on the time the application has been
+     running so that we get a linear animation regardless of the frame
+     rate */
+  rotation = g_timer_elapsed (data->timer, NULL) * 60.0f;
+
   /* Rotate the cube separately around each axis.
    *
    * Note: Cogl matrix manipulation follows the same rules as for
@@ -114,9 +118,9 @@ paint (Data *data)
    * we want it to be a rotation around the origin, before it is
    * scaled and translated.
    */
-  cogl_rotate (data->rotate_x++, 0, 0, 1);
-  cogl_rotate (data->rotate_y++, 0, 1, 0);
-  cogl_rotate (data->rotate_z++, 1, 0, 0);
+  cogl_rotate (rotation, 0, 0, 1);
+  cogl_rotate (rotation, 0, 1, 0);
+  cogl_rotate (rotation, 1, 0, 0);
 
   /* Whenever you draw something with Cogl using geometry defined by
    * one of cogl_rectangle, cogl_polygon, cogl_path or
@@ -175,6 +179,8 @@ main (int argc, char **argv)
       return 1;
   }
 
+  data.timer = g_timer_new ();
+
   cogl_onscreen_show (onscreen);
 
   cogl_push_framebuffer (fb);



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