【发布时间】:2013-07-16 04:41:53
【问题描述】:
我需要以 120 Hz 的频率在屏幕上显示文本而不丢帧。代码运行良好,直到我输入一些文本(菜单选项),然后它降至 47 Hz。我知道问题在于我显示的文本量。我想过在纹理中写文本并将其显示为静态图像,但我不知道是否可能。是吗?如果有怎么办?
我对 OpenGL 很陌生,我开始阅读 RED 书(第 7 版),但我仍在努力了解一切是如何运作的。我的代码需要是跨平台的,并且只能使用 Pyopengl / pyglet。任何帮助/建议将不胜感激。感谢您的帮助。
def on_draw(dt):
left = True
right = False
Rval = 0.0/255.0
Gval = 153.0/255.0
Bval = 0.0/255.0
ShapePosition()
glClear(GL_COLOR_BUFFER_BIT)
glLoadIdentity()
DrawChecker(Nbr = 16, Dark = 25.0/255, Light = 75.0/255)
if ScreenSwap == 1:
DrawQuestionMark(Rval, Gval, Bval, left)
# Blue Line
BlueLine(left)
# Line to see if we are dropping frame
DropFrameTest(left)
pyglet.text.Label('Left', font_name='Times New Roman', font_size=34, x=(window.width*0.753), y= (window.height*0.05), anchor_x='left', anchor_y='center', color = (255, 0, 0, 150)).draw()
ScreenSwap = 0
else:
DrawQuestionMark(Rval, Gval, Bval, right)
# Blue Line
BlueLine(right)
# Line to see if we are dropping frame
DropFrameTest(right)
pyglet.text.Label('Right', font_name='Times New Roman', font_size=34, x=(window.width*0.877), y= (window.height*0.05), anchor_x='left', anchor_y='center', color = (0, 255, 0, 150)).draw()
ScreenSwap = 1
fps = math.ceil(pyglet.clock.get_fps())
labelStr = str(int(fps))+' Hz'
pyglet.text.Label(labelStr, font_name='Times New Roman', font_size=36, x=(window.width*0.02), y= (window.height*0.05), anchor_x='left', anchor_y='center', color = (250, 250, 250, 150)).draw()
if menu:
for i in range(len(labelSysInfo)): # labelSysInfo is a list of 8 strings of text
pyglet.text.Label(labelSysInfo[i], font_name='Times New Roman', font_size=18, x=(window.width*0.75) , y= (window.height*0.95)-(i*window.height*0.03), anchor_x='left', anchor_y='center', color = (210, 210, 255, 255)).draw()
【问题讨论】:
-
您在每次
on_draw调用时创建新的Label实例。您是否尝试过仅在窗口尺寸change 时保留标签对象并更新/重新实例化它们? -
确实提高了帧率!非常感谢。我仍然丢帧,但只是偶尔一次,所以我会调查一下。你能回答这个问题以便我投票吗?
-
对我来说,你的感激之情就足够投票了。顺便一提。每当活动屏幕没有或很少发生变化时,Pyglet 都会抑制帧速率(我认为)。完成菜单后,您的帧率下降可能只是由于没有任何新情况发生。
-
这实际上可以解释帧速率的周期性波动。我注意到帧速率稳定了大约 85 秒,然后下降(大约 116 Hz)大约 0.5 秒,然后上升到 120 Hz。我需要找到问题的根源。稳定的帧速率对我的应用程序至关重要。再次感谢。