0

I am making a simple project in python pyglet that involves drawing a large amount of entities and tiles to the screen, typically ~77K tiles at a time, to do this, I use two batches and have every tile as a sprite, where its x,y position on the screen is its x,y position in the world.

The problem comes when I try to implement some sort of side-scrolling feature into it, to avoid asking an XY question, I figure I should just ask what the best way to do this is.

I have tried many ways to increase performance:

  • Moving around glViewport(), but batches do not draw entities outside of the original size.
  • Updating all the sprites co-ordinates every tick, which insanely slow
  • Drawing all sprites to another Texture, but I haven't found anything in the documentation about this, the blit_into method gives me "Cannot blit to a texture.

The camera class, update() is called every tick

class Camera:
    def __init__(self):
        self.world_sprites = []
        self.world_x = 0
        self.world_y = 0

    def add_sprite(self, spr: tools.WorldSprite):
        self.world_sprites.append(spr)

    def update(self):
        for spr in self.world_sprites:
            spr.update_camera(self)

The update_camera method inside of the WorldSprite class

    def update_camera(self, cam):
        self._x = self.px - cam.world_x
        self._y = self.py - cam.world_y

        self._update_position()

It works, it's just very, very slow. Sorry if this is a big question.

  • Are all this ~77K tiles at the display at once? Why do you not draw just the tiles which are currently on the display? – Rabbid76 Oct 11 '19 at 18:19
  • All 77K tiles are on the display at once – windowsone Oct 11 '19 at 18:46
  • I would think you need to re-think the size of your tiles. They shouldn't be so small that you need 77 **thousand** of them at one viewport. Further more, do a profile with [kCachegrind](https://stackoverflow.com/a/3561512/929999) and see which part of the code is slow. Most likely it's in the sprites `update()` function. Maybe use numpy or something to enhance the update or your positions if you're using vertices. – Torxed Oct 13 '19 at 09:46
  • @Torxed Thanks for the reply, the main cause is in the slowness is pyglet's "_update_position", since I have to update their position every tick. I ended up just rendering them to a seperate image, and then rendering that image onto the screen. – windowsone Oct 15 '19 at 22:55
  • Are you using batches? If not, that's probably your actual solution. – Torxed Oct 16 '19 at 07:22
  • @Torxed Yes I am using batches, the speed loss comes from updating their position in the batch every tick. – windowsone Oct 16 '19 at 17:49
  • Without more code, and a minimal executable example, I can't really help you any further. But if you'd like, head over to Pyglet's pretty active community of game-developers over at their chat mentioned at the bottom of their web page: http://pyglet.org/ and they can probably sake up some speed in your code : ) – Torxed Oct 17 '19 at 13:37

0 Answers0