[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Problem loading 48 sounds?



The dirty rects are done correctly. When you call most draw functions pygame returns a "dirty rect"--that is, a rect that squares off the area of the surface that has changed. If you call update() with rects or a rect as an argument it only updates those spots on the screen/surface and thus does not have to process the full screen. 
It's much faster.


> On Jul 29, 2015, at 01:37, Sam Bull <sam.hacking@xxxxxxxx> wrote:
> 
>> On Wed, 2015-07-29 at 04:36 +0200, gilga gilga wrote:
>> For a music program, at 30FPS, you can detect the delay between the
>> typing and the playing of the sound...
> 
> A couple of points that might give you a performance boost. First, and
> this could seriously be slowing down your program:
> 
>        https://github.com/gustavklopp/pypiano/blob/master/python_keyboardpiano.py#L66
> 
> This update() appears to be loading images from disk once for every key
> on every frame, this would be really slow on a HDD and still not great
> on a SSD (not to mention time spent garbage collecting all the old
> copies getting freed every frame).
> 
> Try loading these images at the beginning:
> 
>        class Key(pygame.sprite.Sprite):
>            def __init__(self, name, keyevent):
>                self._img_down = pygame.image.load(os.path.join('pictures',self.name+'_pressed.png'))
>                self._img_up = pygame.image.load(os.path.join('pictures',self.name+'_unpressed.png'))
> 
>            def update(self):
>                if self.pressed:
>                    self.image = self._img_down
>                else:
>                    self.image = self._img_up
> 
> The second point is that you appear to be trying to make use of dirty
> rects, but you are not using dirty sprites. I'm not too familiar with
> dirty sprites, but I think without them, that update call is not
> optimising anything.
> 
> Perhaps try using a DirtySprite, something like:
> 
>        class Key(pygame.sprite.DirtySprite):
>            def __init__(self, name, keyevent):
>                self._img_down = pygame.image.load(os.path.join('pictures',self.name+'_pressed.png'))
>                self._img_up = pygame.image.load(os.path.join('pictures',self.name+'_unpressed.png'))
> 
>            def pressed(self, pressed):
>                if pressed:
>                    self.image = self._img_down
>                else:
>                    self.image = self._img_up
>                self.dirty = 1
> 
> 
>        ...
> 
>            for keyobj in Key.keyobj_list:
>                if keyobj.keyevent == key:
>                    keyobj.pressed(True)
> 
> This code also removes the need for the update method to be called every
> frame. As the key is only updated on a key press event, it can be
> updated on that event, thus the 'keysprite.update()' part can be skipped
> completely. (Also, if you do want to update the sprites, you should be
> able to just call 'self.keysprites.update()').
> 
> 
> Try out those 2 changes and see what improvements you get.
> 
>> Maybe if you try it on your computer, the FPS are better and it's a
>> problem with my OpenGL and Radeon Driver (known to be quite touchy)?
> 
> You're not using OpenGL...