I am trying to embed skia-python's surface inside a window rather than output to a image file. I am using pysdl2 to create the window using the following code from the documentation:
import sys
import sdl2.ext
RESOURCES = sdl2.ext.Resources(__file__, "resources")
sdl2.ext.init()
window = sdl2.ext.Window("Hello World!", size=(640, 480))
window.show()
factory = sdl2.ext.SpriteFactory(sdl2.ext.SOFTWARE)
sprite = factory.from_image(RESOURCES.get_path("hello.bmp"))
spriterenderer = factory.create_sprite_render_system(window)
spriterenderer.render(sprite)
processor = sdl2.ext.TestEventProcessor()
processor.run(window)
sdl2.ext.quit()
And this code to create the surface from skia's documentation:
import skia
surface = skia.Surface(128, 128)
with surface as canvas:
rect = skia.Rect(32, 32, 96, 96)
paint = skia.Paint(
Color=skia.ColorBLUE,
Style=skia.Paint.kFill_Style)
canvas.drawRect(rect, paint)
image = surface.makeImageSnapshot()
image.save('output.png', skia.kPNG)
Now what I want to achieve is to take the image (or surface whichever applicable) object from the skia portion and plug it into pysdl2 so that I can draw with skia but handle window's event loop with pysdl2 and I'd like to avoid ctypes right now because I am not so familiar with it.
I gave up on creating it without
ctypesas all we need from it isctypes.byrefand I am now importingsdl2instead ofsdl2.extwhich was more pythonic, but also restricted a bit of functionality that is required here.Now to answer the question, I followed this guide here (if you are not building a browser it might go a little off topic for you)
So I have also implemented a general enough version from the above guide, you can draw to
Window.skia_surfaceand then callWindow.updateto copy skia surface to the window screen:Explanation: Primarily the above code does four things create a skia surface that you will draw on, create an SDL window, convert the skia surface to a SDL surface and at last copy the data in the newly created surface to the SDL surface associated with the window and update it. For a bit more explanation I recommend you look into the above guide and also check out skia-python docs and SDL2's API Reference.