2012-03-13 31 views
0

我在整理一些舊雜波代碼中的鑄件時遇到問題,試圖使其更新。它有這樣的代碼:鑄造GTK雜波紋理和正常雜波紋理

static void image_init(CtkImage *image) 
{ 
    priv->texture = clutter_texture_new(); 
    ... 
} 

static void refresh_icon (CtkImage *image) 
{ 
    CtkImagePrivate *priv = image->priv; 
    gtk_clutter_texture_set_from_pixbuf (CLUTTER_TEXTURE (priv->texture), priv->pixbuf, NULL); 
} 

這將產生此編譯時錯誤:

error: passing argument 1 of ‘gtk_clutter_texture_set_from_pixbuf’ from incompatible pointer type [-Werror] 
/usr/include/clutter-gtk-1.0/clutter-gtk/gtk-clutter-texture.h:99:17: note: expected ‘struct GtkClutterTexture *’ but argument is of type ‘struct ClutterTexture *’ 

我以爲我可以用GTK_CLUTTER_TEXTURE修復它,這並不編譯,但運行時錯誤和不足pixbufs的:

gtk_clutter_texture_set_from_pixbuf (GTK_CLUTTER_TEXTURE (texture), tiled, NULL); 

結果造成:

GLib-GObject-WARNING **: invalid cast from `ClutterTexture' to `GtkClutterTexture' 

Clutter-Gtk-CRITICAL **: gtk_clutter_texture_set_from_pixbuf: assertion `GTK_CLUTTER_IS_TEXTURE (texture)' failed 

這是怎麼回事,爲什麼這會失敗?以及它如何被調試?

回答

0

您正在將未初始化的GtkClutterTexture *指針傳遞給僅包含垃圾的函數。您需要先使用gtk_clutter_texture_new()創建一個GtkClutterTexture對象,然後才能用pixbuf填充它。

編輯: 在你更新的例子中,你有一個clutter_texture_new()。這與gtk_clutter_texture_new()並不相同,因此使用GTK_CLUTTER_TEXTURE()將它轉換爲不是的類型,會產生運行時警告。

+0

我沒有在示例代碼中顯示它(道歉),但我們有一個gtk_clutter_texture_new,它附加到一個私人對象,所以我不想讓這個例子複雜化。我已經更新了它。 – 2012-03-13 14:05:28

1

GtkClutterTexture是ClutterTexture的子類;這意味着您可以在接受ClutterTexture的每個函數中使用GtkClutterTexture,但不能將ClutterTexture與使用GtkClutterTexture的方法一起使用。

在示例中,您使用clutter_texture_new()創建紋理,然後將該指針傳遞給gtk_clutter_texture_set_from_pixbuf()。你可以創建一個GtkClutterTexture,或者你使用clutter_texture_set_from_rgb_data()函數從GdkPixbuf設置圖像數據,使用類似:

clutter_texture_set_from_rgb_data (CLUTTER_TEXTURE (texture), 
            gdk_pixbuf_get_pixels (pixbuf), 
            gdk_pixbuf_get_has_alpha (pixbuf), 
            gdk_pixbuf_get_width (pixbuf), 
            gdk_pixbuf_get_height (pixbuf), 
            gdk_pixbuf_get_rowstride (pixbuf), 
            gdk_pixbuf_get_has_alpha (pixbuf) ? 4 : 3, 
            CLUTTER_TEXTURE_NONE, 
            &gerror); 

這正是GtkClutterTexture.set_from_pixbuf()一樣。