File: pyglet_gl.py

package info (click to toggle)
python-enable 4.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 7,280 kB
  • ctags: 13,899
  • sloc: cpp: 48,447; python: 28,502; ansic: 9,004; makefile: 315; sh: 44
file content (56 lines) | stat: -rw-r--r-- 1,319 bytes parent folder | download | duplicates (2)
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
from __future__ import with_statement

from numpy import array
from pyglet.window import key, Window

try:
    from kiva.gl import GraphicsContext
except ImportError, e:
    raise Exception(e)
from kiva.constants import FILL, STROKE, FILL_STROKE

class TestWindow(Window):
    """ Press Q or Escape to exit
    """
    def __init__(self, *args, **kw):
        Window.__init__(self, *args, **kw)
        self.init_window()

    def init_window(self):
        self.gc = GraphicsContext(size=(self.width, self.height))
        self.gc.gl_init()

    def on_key_press(self, symbol, modifiers):
        if symbol in (key.ESCAPE, key.Q):
            self.has_exit = True

    def draw(self):
        gc = self.gc
        with gc:
            gc.clear((0, 1, 0, 1))
            gc.set_stroke_color((1,1,1,1))
            gc.set_line_width(2)
            pts = array([[50, 50], [50,100], [100,100], [100,50]])
            gc.begin_path()
            gc.lines(pts)
            gc.close_path()
            gc.draw_path(STROKE)
            gc.flush()


def main():
    win = TestWindow(width = 640, height=480)
    exit = False
    while not exit:
        win.switch_to()
        win.dispatch_events()
        win.clear()
        win.draw()
        win.flip()
        exit = win.has_exit


if __name__ == "__main__":
    main()