Created
May 20, 2023 06:42
-
-
Save rickardlindberg/d3975038cd1c93b7846e82896c682bbe to your computer and use it in GitHub Desktop.
Example how to display images drawn with Cairo in Pygame.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cairo | |
import pygame | |
import sys | |
SIZE = (400, 400) | |
def draw_frame(cairo_context, position): | |
cairo_context.set_source_rgb(1, 0, 0) | |
cairo_context.rectangle(position, position, 10, 10) | |
cairo_context.fill() | |
def loop(): | |
pygame.init() | |
screen = pygame.display.set_mode(SIZE) | |
clock = pygame.time.Clock() | |
position = 0 | |
while True: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
sys.exit(0) | |
cairo_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, *SIZE) | |
draw_frame(cairo.Context(cairo_surface), position) | |
screen.fill("black") | |
screen.blit( | |
pygame.image.frombuffer(cairo_surface.get_data(), SIZE, "BGRA"), | |
(0, 0) | |
) | |
pygame.display.flip() | |
clock.tick(60) | |
position = (position + 1) % (SIZE[0]-10) | |
if __name__ == "__main__": | |
loop() |
Author
rickardlindberg
commented
May 20, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment