Calling gdk_texture_new_for_pixbuf() whenever pixbuf has changed?

Hello!

So, I would like to do some low-level graphics drawing in my code. (Blablabla… Hardware independency… Future proof… because of reasons…)

Thus, I started by initializing a buffer and turning it into a GTK_PICTURE widget:

        memset(pThis->drawbuf,0xff,sizeof(pThis->drawbuf));
        pThis->pixbuf=gdk_pixbuf_new_from_data(pThis->drawbuf,GDK_COLORSPACE_RGB,true,8,TESTWIDTH,TESTHEIGHT,TESTWIDTH*4,NULL,NULL);
        pThis->texture=gdk_texture_new_for_pixbuf(pThis->pixbuf);
        pThis->picture=gtk_picture_new_for_paintable(GDK_PAINTABLE(pThis->texture));

Now, when I want to change my graphics, it works by changing the contents of the buffer. But when I want to see the update, I always have to create a new gdk_texture.

        memset(pThis->drawbuf,0x7f,sizeof(pThis->drawbuf));


        g_object_unref(pThis->texture);
        pThis->texture=gdk_texture_new_for_pixbuf(pThis->pixbuf);
        gtk_picture_set_paintable(GTK_PICTURE(pThis->picture),GDK_PAINTABLE(pThis->texture));
        gtk_widget_queue_draw(pThis->window);

Is there a better way to do this? invalidating the paintable does not work, because a texture is static.

Thomas

P.S.: My apologies if this has been asked before (seems like a standard problem), but I was not able to find the answer…

Write your own implementation of GdkPaintable.

GdkTexture is an immutable buffer after creation, so you cannot update its contents.

Okay… Are there any best practices as on how to do this?
So, there is no other paintable which is not immutable AND can be used with pixbuf?

Nothing specific: a Paintable implementation only knows how to paint itself when asked, so you get to track changes yourself.

No, and a word of advice: GdkPixbuf is definitely not something you should be using if all you care about is having a pixel buffer.

Question for advice: What should I be using?
My current idea is to solve my problem by manipulating each pixel’s RGB value directly.

So… If you do not know…
Why did you reply to my question?

If you’re just dealing with a pixel buffer you’re filling out manually, you can use a simple linear array of pixels. GdkPixbuf provides that, but it also comes with a ton of code to load image data from various formats.

From that linear buffer, you can create GdkTexture instances whenever you want to draw it. There’s not much else you can do, because changing pixels requires resubmitting the buffer to the GPU.

That’s a very funny way to ask people for help. Let me know if it works out for you…

2 Likes

Emmanuele Bassi is one of the senior GTK / gdk-pixbuf maintainers and probably your best chance of getting this question answered :slight_smile:.

Most devs in Discourse are quite busy and sometimes things might get missed out. In that case, a gentle ping on the topic would be the best option to get their attention.

Alright! My apologies, I do not know anyone around here. It was not my intention belittle anyones work or contribution.

Let me rephrase my question:
Using GTK, I would like to manipulate pixels on the screen by modifying bits in a linear buffer.

Thus far, I was not able to find a suitable example. The best one I got was the one in my opening post.

Could you give me/point me towards a better example than the amateurish code I have given you?

Did you try the solution suggested in above post ?

No, I am still struggling to create working source code for that.
(It would be helpful, if seasoned developers could provide me with it. :wink: )

Plus, it does not seem to solve my problem: If I understand correctly, a GdkTexture with a linear array as pixel buffer is still a GdkTexture, an immutable Paintable.
(please correct me if I am wrong)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.