0

我正在使用 pyglet 构建一个 python 程序。源代码在我的笔记本电脑以外的任何计算机上都可以正常运行。我的笔记本电脑也是唯一一款配备 AMD 显卡的笔记本电脑:HD4250。它的 Xubuntu 13.04 AMD64 和图形驱动程序是 X11 开源驱动程序。这是它的外观: 在此处输入图像描述

在构造函数中添加明确的语句时,屏幕会正确构建,但速度非常慢。它每 30 秒最多刷新 2 次,几乎不响应任何输入。我怎样才能解决这个问题?

OpenGL 似乎不是问题:当使用 Qt OpenGL(也是 C++)时,根本没有这样的问题。

一些(希望是相关的)代码:

def draw(self):
        pyglet.text.Label('Start Screen',
                          font_name='Arial',
                          font_size=16,
                          x=self.window.get_size()[0]/2, y=self.window.get_size()[1]-20,
                          anchor_x='center', anchor_y='center').draw()

        pyglet.text.Label('This side is looking at the enemy',
                          font_name='Arial',
                          font_size=16,
                          x=self.window.get_size()[0]/2, y=self.window.get_size()[1]-60,
                          anchor_x='center', anchor_y='center').draw()

        pyglet.text.Label(self.bottumText,
                          font_name='Arial',
                          font_size=16,
                          x=self.window.get_size()[0]/2, y=20,
                          anchor_x='center', anchor_y='center').draw()

        for y in range(0, len(self.fields)):
            for field in self.fields[y]:

                if (field.selected):
                    glColor3f(self.color[0], self.color[1], self.color[2])
                    # glColor3f(1, 0, 1)
                else:
                    glColor3f(1, 1, 1)

                # Draw center
                # self.drawCircle(field.x, field.y, 5, [1, 1, 1])

                # # Draw top side
                pyglet.graphics.draw(2, pyglet.gl.GL_LINES, ('v2i', 
                    (field.x + field.size, field.y + field.size, 
                    field.x - field.size, field.y + field.size)))

                # Draw down side
                pyglet.graphics.draw(2, pyglet.gl.GL_LINES, ('v2i', 
                    (field.x + field.size, field.y - field.size, 
                    field.x - field.size, field.y - field.size)))

                # Draw left side
                pyglet.graphics.draw(2, pyglet.gl.GL_LINES, ('v2i', 
                    (field.x - field.size, field.y - field.size,
                    field.x - field.size, field.y + field.size)))

                # Draw right side
                pyglet.graphics.draw(2, pyglet.gl.GL_LINES, ('v2i', 
                    (field.x + field.size, field.y - field.size,
                    field.x + field.size, field.y + field.size)))
4

1 回答 1

1

以下代码会产生什么:(
它本身并不限于某个帧缓冲区,因此它可能会产生更好的输出)

#!/usr/bin/python
import pyglet
from time import time, sleep

class Window(pyglet.window.Window):
    def __init__(self, refreshrate):
        super(Window, self).__init__(vsync = False)
        self.frames = 0
        self.framerate = pyglet.text.Label(text='Unknown', font_name='Verdana', font_size=8, x=10, y=10, color=(255,255,255,255))
        self.last = time()
        self.alive = 1
        self.refreshrate = refreshrate
        self.click = None
        self.drag = False

    def on_draw(self):
        self.render()

    def on_mouse_press(self, x, y, button, modifiers):
        self.click = x,y

    def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
        if self.click:
            self.drag = True
            print 'Drag offset:',(dx,dy)

    def on_mouse_release(self, x, y, button, modifiers):
        if not self.drag and self.click:
            print 'You clicked here', self.click, 'Relese point:',(x,y)
        else:
            print 'You draged from', self.click, 'to:',(x,y)
        self.click = None
        self.drag = False

    def render(self):
        self.clear()
        if time() - self.last >= 1:
            self.framerate.text = str(self.frames)
            self.frames = 0
            self.last = time()
        else:
            self.frames += 1
        self.framerate.draw()
        self.flip()

    def on_close(self):
        self.alive = 0

    def run(self):
        while self.alive:
            self.render()
            # ----> Note: <----
            #  Without self.dispatc_events() the screen will freeze
            #  due to the fact that i don't call pyglet.app.run(),
            #  because i like to have the control when and what locks
            #  the application, since pyglet.app.run() is a locking call.
            event = self.dispatch_events()
            sleep(1.0/self.refreshrate)

win = Window(23) # set the fps
win.run()

您可以尝试强制使用可能与您正在使用的开放驱动程序一起使用的特定绘图方法,例如:

glEnable(GL_TEXTURE_2D)

glClear(GL_COLOR_BUFFER_BIT)
glLoadIdentity()
glBegin(GL_TRIANGLES)
glVertex2f(0, 0)
glVertex2f(window.width, 0)
glVertex2f(window.width, window.height)
glEnd()
于 2013-05-14T17:09:54.687 回答