为什么我的Pyglet绘制事件处理程序没有绘制? (on_resize的秘密)

时间:2014-04-18 06:29:18

标签: python debugging pyglet

我使用pyglet,直到今晚都好运。为了进一步调试,我想知道pyglet引擎盖下面发生了什么。

以下是发生的事情:

  1. 我的代码运行正常,没有抛出异常。
  2. 我打开一个带有pyglet并成功调整大小的窗口。
  3. 我成功设置了背景颜色。
  4. 正在成功调用绘制例程。
  5. 但是当我画画时,没有任何反应。没有错误,但也没有绘图。
  6. 我检查了所有明显的事情,包括检查所有代码是否正在运行以及绘图颜色是否与背景不同。

    这里有一些代码属于类的一部分:

    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()
    

    当我运行它时,我得到窗口,正确的背景颜色,打印语句("边界点......"),但它不会绘制线条。他们以前工作过,但我在某个地方搞砸了。

    如何从Pyglet获取有关正在进行的更多信息?

    我试过了:

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

    但是我没有得到任何调试输出(除了我的打印语句)。有什么想法吗?

    更新:请参阅下面的答案,了解为什么上面的on_resize处理程序中的一个缺失行会导致代码错误。更改了标题以反映实际问题。

1 个答案:

答案 0 :(得分:1)

好吧,我没有弄清楚如何从pyglet中获取更多调试信息,但我确实弄明白为什么我从绘图代码中得不到任何东西。

在文档中没有对此进行彻底标记,但与on_draw事件处理程序不同,在处理on_resize事件后,您必须将控制权返回给pyglet的本机处理程序,否则一切都会停止。

所以对于我上面的on_resize事件处理程序,我需要添加最后一行:

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)

显然,pyglet会在启动时触发on_resize事件,所以如果没有这么简单的添加,你的代码就不是一声巨响,而是一声呜咽。