On 17.06.2018 17:48, Daniel Pope wrote:
I have been thinking for some time about how to optimise Pygame Zero games on Raspberry Pi. Most Pi models have multiple cores and an obvious improvement is to parallelise. Logic has to be synchronous but I would like to offload painting the screen to a separate thread. Hi there I think if you truly want to use the other cores on your cpu then
you will have to use processes to achieve this (unless you use
threads from a c-extension, like threads in SDL itself or
similar). Or another python runtime with real threads. Either way this approach should work with threads or processes: One other way to offload rendering to another core would be to
separate the rendering logic. This would mean you will have user
written render logic running on that other core/thread and the
game logic on another core/thread. But then the communication is
sending the current game state to the render logic. So the main
problem would be to efficiently store and send the game state (you
will have to serialize the game state some how I guess if you use
processes, for thread at least copy it I guess). Keep in mind that
you need one process to control the other sub-processes, but that
should be fairly easy. Next thing to consider is that now your
input in caught by the render process which actually acts as
client as in a client-server setup. So you have to send the input
tot he logic executing process back. I have never used this approach in a game, although Gummbum and I
have experimented with rendering in another process (render and
game logic performance was very good, it might introduce some
extra lag). Also I'm not sure how to store the game state and to
be able to send (probably a copy) to the rendering process/thread
efficiently. Here a simple diagram showing what I tried to describe (pygame is
only needed in the client, but for rect speedups you can use it in
game logic too of course). There might be more pitfalls I haven't seen yet. Frankly this is a fair advanced design and I'm not sure how good it is suited for pygame zero. ~DR0ID
PS: I have attached the graphics source so you can extend and
elaborate on it. It was created with https://www.draw.io/ |
Attachment:
pygameGameLogic.png
Description: PNG image
<mxfile userAgent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0" version="8.8.1" editor="www.draw.io" type="device"><diagram id="547e749b-5f58-dc72-c30d-557461ef247a" name="Page-1">7VhLb9swDP41AdbDAluO8zg2absdOqBYC2w9KrZiq5UtQ5aTeL9+kiX5Eaed83I3YD0UEkVJJL+PDK2Bs4i2XxhMwm/UR2QALH87cG4GALgzV/yXglwJRq6lBAHDvhLZleAR/0JaaNQy7KO0ocgpJRwnTaFH4xh5vCGDjNFNU21FSfPWBAaoJXj0IGlLf2Cfh0o6NV5I+VeEg9Dcb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