File: demo12_opengl_pyglet.py

package info (click to toggle)
python-elements 0.13%2Bsvn20090823.230%2Bdfsg-2.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,208 kB
  • ctags: 444
  • sloc: python: 2,816; makefile: 26; sh: 6
file content (78 lines) | stat: -rw-r--r-- 1,907 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
This file is an example for using 'Elements'
Elements is a 2D Physics API for Python using Box2D

Home:  http://elements.linuxuser.at
 IRC:  #elements on irc.freenode.net

Code:  svn co http://svn2.assembla.com/svn/elements          
       http://www.assembla.com/wiki/show/elements                  

License: Examples: Public Domain -- No legal restrictions
         Elements API: GPLv3
"""
from pyglet import window
from pyglet.gl import *

import sys
sys.path.insert(0, "..") # if Elements has yet to be installed

import elements
        
"""
  OpenGL Init Function
"""
def initGL():
    glClearColor(1.0, 1.0, 1.0, 0.0)

    glPointSize(3.0)

    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(-1000.0, 1000.0, -750.0, 750.0, -1.0, 1.0)
    glScalef(2.0, 2.0, 1.0)
    glMatrixMode(GL_MODELVIEW)

class Demo:
    def mouse_click(self, x, y, btn, d):
        if btn == 1:
            self.world.add.ball((x, y), radius=30)
        elif btn == 2:
            self.world.add.triangle((x, y), 40)
        else:
            self.world.add.rect((x, y), 20, 40)
            
        
    def main(self):
        # Window Size
        w, h = size = (900, 900)
    
        # OpenGL Init
        win = window.Window(w, h)        
        initGL()
    
        # Create the Physical Space Class
        self.world = elements.Elements(size, renderer='opengl_pyglet')
        
        # Set pyglet properties
        self.world.set_inputAxisOrigin(left=True, top=False)
        
        # Add ground
        self.world.add.ground()
        
        # Mouse click callback
        win.on_mouse_press = self.mouse_click
        
        # Main Loop
        while not win.has_exit:
            win.dispatch_events()
            win.clear()
    
            self.world.update()
            self.world.draw()
    
            win.flip()

if __name__ == "__main__":
    demo = Demo()
    demo.main()