Search code examples
performancelabelpyglet

FPS drop when drawing a batch with 3500 labels in pyglet


I am working on a videogame with a map and I was trying to print the name of each region on top of it. To do so I am using the pyglet.text.Label object. Despite all these labels are assigned to the same batch, when I draw the batch my FPS go below 1. This is the loop that initialize all the labels:

        label_font_size = 30 
        for i in range(self.reg_center_list.shape[1]):
                temp= pg.text.label(str(self.reg_names[i]), x=self.reg_center_list[0,i], y=self.reg_center_list[1,i], 
                group=self.names_group, batch=self.low_prox_batch, anchor_x='center',anchor_y='center', 
                font_size=label_font_size)

The labels text are stored in the self.reg_names list and their position relative to the map in self.reg_center_list. They are overall more than 3500 labels added to the batch. I did an experiment and printed the same amount of text within a single label as below:

        stringg=''
        label_font_size = 30
        for i in range(self.reg_center_list.shape[1]):
            stringg=stringg + str(self.reg_names[i])
        temp= pg.text.Label(stringg, x=self.reg_center_list[0,i], y=self.reg_center_list[1,i],group=self.names_group,
        batch=self.low_prox_batch, anchor_x='center',anchor_y='center', font_size=label_font_size)

This works nicely without a significant impact on fps despite drawing the facto the same amount of glyphs. It seems that there is some overhead incurred when splitting this text across many labels. I was wondering if the issue could be the fact that for some reason the texture group for the same glyph is not shared between labels causing the program to switch group for each glyph and each label but I had hard time confirming this theory as well as finding a possible solution for it. Would anyone have an idea on how to solve this? Any help is appreaciated and please if more info is needed or something is not clear just drop a comment. :)


Solution

  • This is an older issue, one where grouping in the batch of labels wasn't taken into account correctly. It was fix in pyglet v1.4.9:

    Fix TextLayoutGroup consolidation when using custom Groups for Labels.

    And since you're running 1.4.8, this is most likely the issue.
    Either way, you should probably update to the latest version anyway, to do so simply do:

    sudo pip install --upgrade pyglet
    

    To get the latest version, or pip install --upgrade pyglet if you prefer user-local pckages.