Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: PySFML and PyCairo  (Read 6693 times)

0 Members and 1 Guest are viewing this topic.

BilbyCoder

  • Newbie
  • *
  • Posts: 1
    • View Profile
PySFML and PyCairo
« on: November 11, 2008, 02:12:23 pm »
They work well together :)


Code: [Select]

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.