Debian Codegraph @

docs/tutorial/other.rst

raw
1 Integration with other Libraries
2 ================================
3
4 Pyglet & ImageSurface as Texture
5 --------------------------------
6
7 Creating a pyglet.Texture from an ImageSurface:
8 .. code:: python
9
10 import ctypes
11 import cairo
12
13 from pyglet import app, clock, gl, image, window
14
15 # create data shared by ImageSurface and Texture
16 width, height = 400, 400
17
18 surface_data = (ctypes.c_ubyte * (width * height * 4))()
19 surface = cairo.ImageSurface.create_for_data (surface_data, cairo.FORMAT_ARGB32,
20 width, height, width * 4);
21 texture = image.Texture.create_for_size(gl.GL_TEXTURE_2D, width, height, gl.GL_RGBA)
22
23
24 Draw pyglet.Texture bound to ImageSurface
25 .. code:: python
26
27 window = window.Window(width=width, height=height)
28
29 @window.event
30 def on_draw():
31 window.clear()
32
33 # Draw texture backed by ImageSurface
34 gl.glEnable(gl.GL_TEXTURE_2D)
35
36 gl.glBindTexture(gl.GL_TEXTURE_2D, texture.id)
37 gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA, width, height, 0, gl.GL_BGRA,
38 gl.GL_UNSIGNED_BYTE,
39 surface_data)
40
41 gl.glBegin(gl.GL_QUADS)
42 gl.glTexCoord2f(0.0, 1.0)
43 gl.glVertex2i(0, 0)
44 gl.glTexCoord2f(1.0, 1.0)
45 gl.glVertex2i(width, 0)
46 gl.glTexCoord2f(1.0, 0.0)
47 gl.glVertex2i(width, height)
48 gl.glTexCoord2f(0.0, 0.0)
49 gl.glVertex2i(0, height)
50 gl.glEnd()
51
52 # call clock.schedule_update here to update the ImageSurface every frame
53 app.run()
54
55
56 Freetype-py & Cairo
57 -------------------
58
59 See https://github.com/rougier/freetype-py/tree/master/examples for examples. Most of the ``*-cairo.py`` examples illustrate conversion from FreeType bitmaps to Cairo surfaces; the two examples, ``glyph-vector-cairo.py`` and ``glyph-vector-2-cairo.py``, illustrate conversion from FreeType glyph contours to Cairo paths.
60
61
62 ModernGL & ImageSurface as Texture
63 ----------------------------------
64
65 Creating a moderngl.Texture from an ImageSurface:
66 .. code:: python
67
68 import moderngl
69 import cairo
70 ctx = moderngl.create_context(standalone=True)
71 width, height = 400, 400
72 surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
73 texture = ctx.texture((width, height), 4, data=surface.get_data())
74
75 An example can also be found in the ModernGL project:
76 https://github.com/moderngl/moderngl/blob/master/examples/integration_pycairo.py