Search code examples
pythonnetwork-programmingpygamepyro

Python, Pygame, Pyro: How to send a surface over a network?


I am working on a project in python using pygame and pyro. I can send data, functions, classes, and the like easily. However, I cannot send a surface across the wire without it dying on me in transit.

The server makes a surface in the def __init__ of the class being accessed across the wire:

self.screen = pygame.display.set_mode(SCREENRECT.size, NOFRAME)

On the server, the screen prints as Surface(800x800x32 SW) but when retrieved by the client it is Surface(Dead Display).

Something to note though. I get a dead display when I use an accessor function to get my screen. If I use print Player.screen to get the variable I instead get what seems to be a pyro pointer to the screen: <Pyro.core._RemoteMethod instance at 0x02B7B7B0>. Maybe I can dereference this?

More than likely I am being thick, does anyone have some insight? Thanks. :)


Solution

  • A pygame Surface is a wrapper around an underlying SDL surface, which I suspect can't be serialized by Pyro. If you want to copy its contents across the wire, you would be better off doing something like this:

    1. on the server use Surface.get_buffer() to get access to the underlying pixels.
    2. make a note of the Surface's dimensions, colour depth, etc.
    3. send the data obtained from steps 1 and 2 over the wire to the client.
    4. on the client create a new surface using the dimensions, colour depth, etc, from step 2.
    5. set the new Surface's pixels using Surface.get_buffer() and copying in the pixels from step 1.

    Edit: It just occurred to me that I'm overcomplicating it. To serialise your Surface, use pygame.image.tostring(), and to reload it, use pygame.image.fromstring().