#!/usr/bin/env python

'''Demonstrates how to manage OpenGL calls between two independent windows.
'''

from pyglet.gl import *
from pyglet import window
from pyglet import clock

def setup():
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(60., 1., 1., 100.)

    glMatrixMode(GL_MODELVIEW)
    glClearColor(1, 1, 1, 1)
    glColor4f(.5, .5, .5, .5)

def draw():
    glClear(GL_COLOR_BUFFER_BIT)
    glLoadIdentity()

    glRotatef(r, 0, 0, 1)
    glBegin(GL_QUADS)
    glVertex3f(-1., -1., -5.)
    glVertex3f(-1., 1., -5.)
    glVertex3f(1., 1., -5.)
    glVertex3f(1., -1., -5.)
    glEnd()

w1 = window.Window(200, 200, caption='First window')
w1.switch_to()
setup()

w2 = window.Window(300, 300, caption='Second window')
w2.switch_to()
setup()

r = 0
clock.set_fps_limit(30)
while not (w1.has_exit or w2.has_exit):
    dt = clock.tick()
    r += 1
    if r > 360: r = 0

    w1.switch_to()
    w1.dispatch_events()
    draw()
    w1.flip()

    w2.switch_to()
    w2.dispatch_events()
    draw()
    w2.flip()

@@ -183,16 +183,20 @@
 f setup(
+w, h
 ):%0A    g
@@ -670,16 +670,37 @@
 indow')%0A
+w1.on_resize = setup%0A
 w1.switc
@@ -705,24 +705,16 @@
 tch_to()
-%0Asetup()
 %0A%0Aw2 = w
@@ -761,16 +761,37 @@
 indow')%0A
+w2.on_resize = setup%0A
 w2.switc
@@ -800,16 +800,8 @@
 to()
-%0Asetup()
 %0A%0Ar 

Fix so window creation events are handled correctly.