SVG rendering in a PyGame application

前端 未结 9 1143
悲哀的现实
悲哀的现实 2020-12-02 12:34

In a pyGame application, I would like to render resolution-free GUI widgets described in SVG.

What tool and/or library can I use to reach this goal ?

(I like

9条回答
  •  误落风尘
    2020-12-02 13:19

    The last comment crashed when I ran it because svg.render_cairo() is expecting a cairo context and not a cairo surface. I created and tested the following function and it seems to run fine on my system.

    import array,cairo, pygame,rsvg
    
    def loadsvg(filename,surface,position):
        WIDTH = surface.get_width()
        HEIGHT = surface.get_height()
        data = array.array('c', chr(0) * WIDTH * HEIGHT * 4)
        cairosurface = cairo.ImageSurface.create_for_data(data, cairo.FORMAT_ARGB32, WIDTH, HEIGHT, WIDTH * 4)
        svg = rsvg.Handle(filename)
        svg.render_cairo(cairo.Context(cairosurface))
        image = pygame.image.frombuffer(data.tostring(), (WIDTH, HEIGHT),"ARGB")
        surface.blit(image, position) 
    
    WIDTH = 800
    HEIGHT = 600
    pygame.init()
    window = pygame.display.set_mode((WIDTH, HEIGHT))
    screen = pygame.display.get_surface()
    
    loadsvg("test.svg",screen,(0,0))
    
    pygame.display.flip() 
    
    clock = pygame.time.Clock()
    while True:
        clock.tick(15)
        event = pygame.event.get()
        for e in event:
            if e.type == 12:
                raise SystemExit
    

提交回复
热议问题