I want to be able to save a surface to a scalar, and then recreate it. 
it seems like this should work, but it doesn't:

my $surf = new SDL::Surface( ... );
my $buffer = $surf->pixels();
my $newsurf = new SDL::Surface(-from=>$buffer, ... );

Where the ... are the same options for both calls to new for height and
width and depth.  If I blit $newsurf and $surf to my application
surface, newsurf is screwed up.  Calling display_format doesn't seem to
do it.

Is there some other way I can serialize an SDL::Surface and later
recreate it?

Also, it appears there might be a memory leak related to the use of
-from, or this might be causing this problem, but I'm not sure because
I'm not that familiar with the perl GC.  Calling ->pixels returns a
newly created scalar filled with the pixels data.  Invoking new with
-from just uses the scalar passed as the buffer ($src, first argument to
SDL_CreateRGBSurfaceFrom.  If the value passed for -from then goes out
of scope in the caller and is then GC'ed, SDL_Surface->pixels points to
free'd memory.  I was going to experiment with creating a new buffer and
copying the data in the scaler to it and passing that as the first
argument to SDL_CreateRGBSurfaceFrom, but then I'll need a flag to
signify that ->pixels should be free'd in FreeSurface (as the
documentation for SDL_FreeSurface says that memory that ->pixels points
to isn't freed if the surface was created with
SDL_CreateRGBSurfaceFrom), and I'm not sure where to put this flag (as
the value blessed as SDL::Surface is solely a scalar, pointing to the
SDL_Surface structure).

Also, it would be pretty cool to have a function that returns an opaque
scalar value containing the attributes of a surface so that surface
attributes can be copied between surfaces created from buffers of pixel
data (alternatively, a surface serializer might include that
information).  That is:

  $surface->display_format()

might be shorthand for:

  my $surfattr = $app->attributes_and_flags();
  $surface->set_attributes_and_flags($surfattr);

(although better names are in order).  Although this would be secondary
to a surface serializer who's output can be used as input to recreate
the surface, as that seems like a more useful interface.

FYI, one thing I'm trying to do here is write a multi-threaded program
that has a number of worker threads update different small surfaces and
have a single thread (which has control of the SDL::App object) that
then blits all the smaller surfaces to the application window.  In order
to pass surfaces around, I need to be able to serialize them (as only
scalars can be in shared variables).  I also want to be able to store
serialized surfaces, with all internal attributes/flags and such, in
files (without having to write out a BITMAP and re-setup all the flags
when I read it in) for reload later (and possibly generate some of those
surfaces in other processes) and for temporary storage on disk or to be
received via file descriptors.

Reply via email to