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

Re: Re: [pygame] Surface objects



Hey guys,

As Droid pointed out this is called a copy constructor and is often used when you want to pass a deep copy of an object to a function in C++ (at least that has been the main use I have had of it although there are other scenarios as well). You would rather have a wrapper and store internal reference to a pygame.Surface because in terms of efficiency this is a way better approach than creating deep copies of the same surface in your gaming implementation (what would be the use-case for that anyway?).

So instead of inheriting from pygame.Surface you can have a surface member and upon instantiation if surface arguments are provided you instantiate a new pygame.Surface, otherwise you point your member at the surface object provided to the constructor.

I hope this would be of some use to you :)
Konstantin Dinev

---- OriginalMessage ----
From: "DR0ID" <dr0id@xxxxxxxxxx>
To: pygame-users@xxxxxxxx
Sent: Thu, Jul 14, 2011, 10:08 PM
Subject: Re: [pygame] Surface objects

On 14.07.2011 22:51, sam.hacking@xxxxxxxx wrote:
On Thu, 14 Jul 2011 19:05 +0200, "DR0ID" <dr0id@xxxxxxxxxx> wrote:
An old experiment of mine, it might give you a clue how you can do it (actually it replaces the pygame Surface object with the SurfaceObject class):

https://python-pyknic.googlecode.com/svn/branches/pyknic-2.0/experimental/surfaceobject.py
 
Right, you appear to be doing the much the same thing, using super() to create the surface object. What I want to know is, how can you get it to act like that if you already have a surface. So, instead of passing in the size:
s = SurfaceObject((400,300))
You would pass in an existing pygame.Surface object.
s = pygame.Surface((400,300))
so = SurfaceObject(s)
One way I've just thought of, maybe if I create a new surface (through __init__) the same size as the surface passed in, then just blit the passed in surface onto the newly created surface.


Hi again

Well, this

s = pygame.Surface((400,300))
so = SurfaceObject(s)

is just a matter how the contructor works. I would be similar to the internal class "Wrapper" of the SurfaceObject, which creates a new surface and blits the content on it.

If you read the SurfaceObject's code carefully, you will see that I actually replace pygame.Surface with the Surface Object. This means
anytime you call pygame.Surface(...) you'll get a SurfaceObject instead of a pygame.Surface. So you actually work with instances of SurfaceObject. And it works with all surface functions because it inherits from pygame.Surface.

But maybe, this is the wrong approach for your "Problem", as others said, knowing more what you want can get you better solutions.

At a second thought, what you describe is a "copy-constructor". Do you really need a copy constructor or could you just have a method that makes a copy of an existing SurfaceObject of yours?

~DR0ID