Question

I'm using pyglet and have had good luck with it until tonight. To further my debugging efforts, I'd like to find out what's going on beneath pyglet's hood.

Here's what is happening:

  1. My code is running fine with no exceptions thrown.
  2. I open a window with pyglet and size it successfully.
  3. I set the background color successfully.
  4. The draw routines are being called successfully.
  5. But when I draw, nothing happens. No errors, but no drawing either.

I've checked all the obvious things, including checking to see if all the code is running and if the draw color is different from the background.

Here's some code which is part of a class:

DEF_BKGDCOLOR = (1, 1, 1, 1) # white
DEF_GUIDECOLOR = (.1,.1,.1)  # dark dark gray

def initScreen(self):
    # initialize pyglet
    self.m_screen = pyglet.window.Window(width=width,height=height,\ 
                                             resizable=True)
    pyglet.gl.glClearColor(*DEF_BKGDCOLOR)
    self.m_screen.on_draw = self.on_draw
    self.m_screen.on_resize = self.on_resize

def on_draw(self):
    self.m_screen.clear()
    self.drawGuides()

def on_resize(self,width,height):
    self.m_field.m_xmax_screen = width
    self.m_field.m_ymax_screen = height

def drawGuides(self):
    # draw boundaries of field (if in screen mode)
    if self.m_output_mode == MODE_SCREEN:
        pyglet.gl.glColor3f(DEF_GUIDECOLOR[0],DEF_GUIDECOLOR[1],DEF_GUIDECOLOR[2])
        points = [(self.m_xmin_field,self.m_ymin_field),
                  (self.m_xmin_field,self.m_ymax_field),
                  (self.m_xmax_field,self.m_ymax_field),
                  (self.m_xmax_field,self.m_ymin_field)]
        #print "boundary points (field):",points
        index = [0,1,1,2,2,3,3,0]
        screen_pts = self.rescale_pt2out(points) # scale pts for the screen
        print "boundary points (screen):",screen_pts
        # ex: boundary points (screen): [(72, 73), (72, 721), (1368, 721), (1368, 73)]
        print "proc screen_pts:",tuple(chain(*screen_pts))
        # ex: proc screen_pts: (72, 73, 72, 721, 1368, 721, 1368, 73)
        pyglet.graphics.draw_indexed(len(screen_pts), pyglet.gl.GL_LINES,
            index,
            ('v2i',tuple(chain(*screen_pts))),
        )

...

while True:
    for window in visualsys.pyglet.app.windows:
        field.m_screen.switch_to()
        field.m_screen.dispatch_events()
        field.m_screen.dispatch_event('on_draw')
        field.m_screen.flip()

When I run it, I get the window, the right background color, the print statements ("boundary points..."), but it doesn't draw the lines. They worked before, but I borked something somewhere.

How do I get more information from Pyglet about what's going on?

I tried this:

pyglet.options['debug_gl'] = True
pyglet.options['debug_gl_trace'] = True
pyglet.options['debug_trace'] = True

But I don't get any debugging output (other than my print statements). Any ideas?

UPDATED: See answer below for why a single missing line in the on_resize handler above was buggering up the code. Changed the title to reflect the actual problem.

Was it helpful?

Solution

Well, I didn't figure out how to get more debugging info out of pyglet, but I did figure out why I was getting nothing from my draw code.

This is not thoroughly flagged n the documentation, but unlike your on_draw event handler, after you handle the on_resize event you have to return control to pyglet's native handlers otherwise everything stops.

So to my above on_resize event handler, I needed to add this last line:

def on_resize(self, width, height):
    self.m_field.m_xmax_screen = width
    self.m_field.m_ymax_screen = height
    super(Window, self).on_resize(width, height)

Apparently, pyglet fires off the on_resize event when it starts, so without this simple addition, your code starts not with a bang, but with a whimper.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top