File: pygame-test1.py

package info (click to toggle)
py3cairo 1.10.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,520 kB
  • ctags: 2,416
  • sloc: python: 12,338; ansic: 4,885; makefile: 160; sh: 32
file content (65 lines) | stat: -rwxr-xr-x 1,467 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
"""demonstrate pycairo and pygame
method1: use pycairo and pygame directly
"""

import array
import math
import sys

import cairo
import pygame

def draw(surface):
  x,y, radius = (250,250, 200)
  ctx = cairo.Context(surface)
  ctx.set_line_width(15)
  ctx.arc(x, y, radius, 0, 2.0 * math.pi)
  ctx.set_source_rgb(0.8, 0.8, 0.8)
  ctx.fill_preserve()
  ctx.set_source_rgb(1, 1, 1)
  ctx.stroke()

def input(events):
 for event in events:
  if event.type == pygame.QUIT:
      sys.exit(0)
  else:
      print event


Width, Height = 512, 512
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, Width, Height)

pygame.init()
window = pygame.display.set_mode( (Width,Height) )
screen = pygame.display.get_surface()

draw(surface)

#Create PyGame surface from Cairo Surface
buf = surface.get_data()
image = pygame.image.frombuffer(buf,(Width,Height),"ARGB",)
#Tranfer to Screen
screen.blit(image, (0,0))
pygame.display.flip()

while True:
  input(pygame.event.get())


"""
with pycairo 1.4.12 and pygame 1.7.1 you get the error message:

Traceback (most recent call last):
  File "./pygame-test1.py", line 42, in <module>
    image = pygame.image.frombuffer(buf,(Width,Height),"ARGB",)
TypeError: char buffer type not available

This is because with
    buf = surface.get_data()
pycairo provides a binary image buffer,
whereas with
    image = pygame.image.frombuffer(buf,(Width,Height),"ARGB",)
pygame is expecting a text-based character buffer!
"""