[cairo-commit] pycairo/test Makefile.am, 1.10, 1.11 pygame-test1.py, NONE, 1.1 pygame-test2.py, NONE, 1.1

Steve Chaplin commit at pdx.freedesktop.org
Wed Jul 16 20:41:59 PDT 2008


Committed by: stevech1097

Update of /cvs/cairo/pycairo/test
In directory kemper:/tmp/cvs-serv11561/test

Modified Files:
	Makefile.am 
Added Files:
	pygame-test1.py pygame-test2.py 
Log Message:
'SC'

Index: Makefile.am
===================================================================
RCS file: /cvs/cairo/pycairo/test/Makefile.am,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- Makefile.am	17 Jan 2007 05:40:01 -0000	1.10
+++ Makefile.am	17 Jul 2008 03:41:57 -0000	1.11
@@ -3,6 +3,8 @@
 	isurface_create_for_data2.py \
 	isurface_create_from_png.py \
 	isurface_get_data.py \
+	pygame-test1.py \
+	pygame-test2.py \
 	surface_create_for_stream.py \
 	surface_write_to_png.py \
 	test.py \

--- NEW FILE: pygame-test1.py ---
#!/usr/bin/env python
"""demonstrate pycairo and pygame
method1: use a 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!
"""

--- NEW FILE: pygame-test2.py ---
#!/usr/bin/env python
"""demonstrate pycairo and pygame
method1: use an intermediate Python array object
"""

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
data = array.array('c', chr(0) * Width * Height * 4)
stride = Width * 4
surface = cairo.ImageSurface.create_for_data(data, cairo.FORMAT_ARGB32,Width, Height, stride)

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

draw(surface)

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

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



More information about the cairo-commit mailing list