2013-07-17 18 views
2

我需要在不丟幀,以顯示我的屏幕上的文字,在120赫茲。代碼工作正常,直到我把一些文本(菜單選項),然後它下降到47赫茲。我知道問題在於我顯示的文字數量。我想過要在紋理中編寫文本並將其顯示爲靜態圖像,但我不知道是否可行。是嗎?如果是這樣如何?Pyglet:替代書寫文字時,爲了避免緩慢的FPS?

我非常新的OpenGL的,我開始閱讀紅皮書(第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() 
+0

您在每個'on_draw'調用創建新的'Label'實例。您是否嘗試保持標籤對象和更新/重新實例化他們只有在窗口尺寸[變化](http://www.pyglet.org/doc/programming_guide/resizing_the_window.html)? –

+0

這確實提高了幀率!非常感謝。我仍然在丟幀,但只有一段時間,所以我會研究它。你能回答這個問題嗎,我可以投票嗎? – BaldDude

+0

對我來說,你的感激是足夠的投票。順便一提。 Pyglet會在活動屏幕沒有或幾乎沒有變化的情況下遏制幀率(我認爲)。一旦你的菜單完成,你的幀率下降可能只是一個沒有新的事情發生的結果。 –

回答

2

感謝Katzwinkel,我的代碼現在以合適的幀率運行。我只保留了標籤對象,並且只在發生變化時才更新它們。

+0

http://stackoverflow.com/a/10525553/1933494 –