diff --git a/doc/cookbook/textures.xml b/doc/cookbook/textures.xml
index efc190de9..9a2cd0142 100644
--- a/doc/cookbook/textures.xml
+++ b/doc/cookbook/textures.xml
@@ -1187,7 +1187,7 @@ clutter_state_set_state (transitions, "show-top");
different dimensions, the source will
appear distorted.
- There are a few ways you can remedy this:
+ There are a couple of ways you can remedy this:
@@ -1209,91 +1209,92 @@ clutter_state_set_state (transitions, "show-top");
- If the bounds of the images you're going to be loading
- are known (i.e. you know the greatest width and height
- of all the images you're going to load), you can size
- the ClutterTexture to those bounds.
+ Use GdkPixbuf (or similar) to load the images into a temporary
+ data structure. (GdkPixbuf works well for this as it can resize
+ the image while retaining its aspect ratio.) Then load the data from
+ the pixbuf into a region of a
+ CoglTexture which has the same dimensions as
+ the ClutterTexture.
- Then, as you load each image (e.g. you could do this in the
+ Here's an example of how you can rewrite the
load_cogl_texture() function of
- the code
- example):
+ the single
+ texture example to do this:
-
-
- Load the image into a temporary
- CoglTexture (e.g. called
- filetex). Then use the
- CoglTexture API to get the data from it.
- For example:
-
-
-
+
+
-CoglPixelFormat format = cogl_texture_get_format (filetex);
-guint rowstride = cogl_texture_get_rowstride (filetex);
-guint width = cogl_texture_get_width (filetex);
-guint height = cogl_texture_get_height (filetex);
+static CoglHandle
+load_cogl_texture (const char *type,
+ const char *file,
+ const guint texture_width,
+ const guint texture_height)
+{
+ GError *error = NULL;
-/* allocate memory large enough for the data */
-gint size = cogl_texture_get_data (filetex, format, 0, NULL);
-guchar *data = g_new0 (guchar, size);
+ /*
+ * Load image data from a file into a GdkPixbuf,
+ * but constrained to the size of the target ClutterTexture;
+ * aspect ratio is maintained
+ *
+ * texture_width and texture_height are set elsewhere to
+ * the width and height of the ClutterTexture
+ */
+ GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file_at_size (file,
+ texture_width,
+ texture_height,
+ &error);
-/* get the data from the texture */
-cogl_texture_get_data (filetex,
- format,
- rowstride,
- data);
+ if (error != NULL)
+ {
+ g_print ("Unable to load %s image: %s\n", type, error->message);
+ g_error_free (error);
+ exit (EXIT_FAILURE);
+ }
+
+ guint rowstride = gdk_pixbuf_get_rowstride (pixbuf);
+ guint width = gdk_pixbuf_get_width (pixbuf);
+ guint height = gdk_pixbuf_get_height (pixbuf);
+ guchar *data = gdk_pixbuf_get_pixels (pixbuf);
+
+ CoglPixelFormat format = COGL_PIXEL_FORMAT_RGB_888;
+ if (gdk_pixbuf_get_has_alpha (pixbuf) == TRUE)
+ format = COGL_PIXEL_FORMAT_RGBA_8888;
+
+ /* CoglTexture with the same dimensions as the ClutterTexture */
+ CoglHandle *tex = cogl_texture_new_with_size (texture_width,
+ texture_height,
+ COGL_TEXTURE_NO_SLICING,
+ format);
+
+ /*
+ * load the texture data into a region of the full-sized texture;
+ * the size of the region is set from the size of the image data
+ * (as resized by GdkPixbuf)
+ */
+ cogl_texture_set_region (tex,
+ 0, 0, /* from top-left corner of the pixbuf */
+ (texture_width - width) / 2, /* center on the CoglTexture */
+ (texture_height - height) / 2, /* center on the CoglTexture */
+ width, height,
+ width, height,
+ format,
+ rowstride,
+ data);
+
+ return tex;
+}
]]>
-
-
-
-
- Create another CoglTexture as
- large as the ClutterTexture (i.e. big enough
- to fit any image you're going to load). Then copy the
- data from filetex into a region of
- it. For example:
+
+
-
-
-
-
-
-
- Because you're copying the image data from the
- file into a region of the CoglTexture
- that's the same size as the image data, it won't be
- distorted. However, it's worth stressing that the
- ClutterTexture needs to be as wide as
- the widest image and as tall as the tallest, so
- all the images can be accommodated. Otherwise this
- works, but images might be clipped.
-
-
+ Because you're copying the image data from the
+ file into a region of the CoglTexture
+ that's the same size as the image data in the pixbuf, it isn't
+ distorted.