root / PyOpenGL-Demo / GLUT / gluttimerfunc_testCase.py @ 2
Historique | Voir | Annoter | Télécharger (2,1 ko)
| 1 |
import sys |
|---|---|
| 2 |
from OpenGL.GLUT import * |
| 3 |
from OpenGL.GL import * |
| 4 |
|
| 5 |
ESCAPE = '\033'
|
| 6 |
|
| 7 |
PROMPT = ("Press keys '1' - '0' to start callbacks",
|
| 8 |
"Press ESCAPE to exit.")
|
| 9 |
|
| 10 |
global AllTimers
|
| 11 |
AllTimers = [] |
| 12 |
|
| 13 |
class TimerCBOwner: |
| 14 |
def __init__(self, name, delay, repeat): |
| 15 |
global AllTimers
|
| 16 |
self.name = name
|
| 17 |
self.delay = delay
|
| 18 |
self.repeat = repeat
|
| 19 |
self.state = "WAITING" |
| 20 |
glutTimerFunc(self.delay, self.cb, 0) |
| 21 |
AllTimers += (self,)
|
| 22 |
glutPostRedisplay() |
| 23 |
|
| 24 |
|
| 25 |
def getDescription(self): |
| 26 |
return "%s: %s"%(self.name, self.state) |
| 27 |
|
| 28 |
def cb(self, value): |
| 29 |
self.state = "CALL %d"%value |
| 30 |
if value + 1 == self.repeat: |
| 31 |
self.state += " (LAST!)" |
| 32 |
|
| 33 |
if value < self.repeat: |
| 34 |
glutTimerFunc(self.delay, self.cb, value+1) |
| 35 |
else:
|
| 36 |
AllTimers.remove(self)
|
| 37 |
|
| 38 |
glutPostRedisplay() |
| 39 |
|
| 40 |
def keyboard(key, foo, bar): |
| 41 |
if key == ESCAPE:
|
| 42 |
sys.exit() |
| 43 |
else:
|
| 44 |
TimerCBOwner(key, 2000, 5) |
| 45 |
|
| 46 |
def display(): |
| 47 |
global AllTimers
|
| 48 |
w = float(glutGet(GLUT_WINDOW_WIDTH))
|
| 49 |
h = float(glutGet(GLUT_WINDOW_HEIGHT))
|
| 50 |
glViewport(0, 0, int(w), int(h)) |
| 51 |
glClearColor(0.0, 0.0, 0.0, 0.0) |
| 52 |
glClear(GL_COLOR_BUFFER_BIT) |
| 53 |
glColor4f(1.0, 1.0, 0.5, 1.0) |
| 54 |
glMatrixMode(GL_PROJECTION) |
| 55 |
glLoadIdentity() |
| 56 |
glMatrixMode(GL_MODELVIEW) |
| 57 |
glLoadIdentity() |
| 58 |
glTranslate(-1.0, 1.0, 0.0) |
| 59 |
scale = 1.0/w
|
| 60 |
glScale(scale, -scale*w/h, 1.0)
|
| 61 |
glTranslate(1.0, 1.0, 0.0) |
| 62 |
y = 25.0
|
| 63 |
for s in PROMPT: |
| 64 |
glRasterPos(40.0, y)
|
| 65 |
y += 30.0
|
| 66 |
for c in s: |
| 67 |
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, ord(c))
|
| 68 |
|
| 69 |
y = 100.0
|
| 70 |
for t in AllTimers: |
| 71 |
glRasterPos(80.0, y)
|
| 72 |
for c in t.getDescription(): |
| 73 |
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, ord(c))
|
| 74 |
y += 30.0
|
| 75 |
|
| 76 |
glutSwapBuffers() |
| 77 |
|
| 78 |
def main(): |
| 79 |
glutInit([]) |
| 80 |
glutInitWindowSize(640, 480) |
| 81 |
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE) |
| 82 |
glutCreateWindow("glutTimerFunc test case")
|
| 83 |
glutDisplayFunc(display) |
| 84 |
glutKeyboardFunc(keyboard) |
| 85 |
glutMainLoop() |
| 86 |
|
| 87 |
main() |
| 88 |
|