SFML community forums
Bindings - other languages => Python => Topic started by: BilbyCoder on November 11, 2008, 02:12:23 pm
-
They work well together :)
import cairo, math
from PySFML import *
WIDTH, HEIGHT = 256, 256
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
ctx = cairo.Context(surface)
ctx.scale (WIDTH/1.0, HEIGHT/1.0)
pat = cairo.LinearGradient (0.2, 1.0, 0.5, 1.0)
pat.add_color_stop_rgba (.8, 1, 0, 1, 1)
pat.add_color_stop_rgba (1, 1, 1, 1, 1)
ctx.rectangle (0,0,1,1)
ctx.set_source(pat)
ctx.fill ()
pat = cairo.RadialGradient (0.45, 0.4, 0.1,
0.4, 0.4, 0.5)
pat.add_color_stop_rgba (0, 1, 1, 1, 1)
pat.add_color_stop_rgba (1, 0, 1, 0, 1)
ctx.set_source (pat)
ctx.arc (0.5, 0.5, 0.3, 0, 2 * math.pi)
ctx.fill ()
window = sf.RenderWindow(sf.VideoMode(800, 600, 32), "cairoTest")
image = sf.Image()
# here is the massive amount of code to get a cairo surface into sfml
image.LoadFromPixels(WIDTH, HEIGHT, surface.get_data())
# and we are done.
sprite = sf.Sprite(image)
sprite.SetPosition(100, 100)
running = True
while running:
event = sf.Event()
while window.GetEvent(event):
if event.Type == sf.Event.Closed:
window.Close()
running = False
window.Draw(sprite)
window.Display()
Ugly as a very ugly thing, but it works :)
There isn't SVG support in PyCairo unfortunately (except for writing an SVG file). The format states it's ARGB but it seems to work without problem. I wrote the image to an SVG file and the output was the same.