Re: [Pdl-porters] Interfacing SDL Surface with a piddle

2010-06-28 Thread Chris Marshall

On 6/27/2010 10:59 AM, Kartik Thakore wrote:


Hi

Ok I looked at get_dataref. But I still don't understand why the
surface->pixels is swaped out when a piddle is written. Both of the
Devel::Peek for the get_dataref and surface->get_pixels is the exact same.


get_dataref is a perl reference to the data section of
the piddle.  The surface->pixels is just a pointer to
the pixel data.  I see no copying of data in the code so
it doesn't seem like they should be the same.

I notice on the page you reference for the XS stuff that
there is a surface_new_from routine.  Have you tried
creating a piddle of data the size and structure of the
surface and then creating the desired surface from that?

At least you would have all the surface and piddle data
in the same spot which seems to be your difficulty here.

--Chris


On Sun, Jun 27, 2010 at 1:32 AM, Chris Marshall mailto:c...@alum.mit.edu>> wrote:

On 6/26/2010 10:49 PM, Kartik Thakore wrote:

Right but where is the piddle xs code?


It is in Basic/Core in the PDL  directory.
The piddle data is in the string of the perl scalar.
That way it gets the advantage of the perl memory
allocation/freeing...

--Chris

On Sat, Jun 26, 2010 at 10:35 PM, Chris Marshall
mailto:c...@alum.mit.edu>
>> wrote:

On 6/26/2010 10:10 PM, Kartik Thakore wrote:

What SV_type is getdata_ref using? where can I find that?


You can get a copy of the PDL source from CPAN
and that should explain things.

--Chris





No virus found in this incoming message.
Checked by AVG - www.avg.com 
Version: 9.0.830 / Virus Database: 271.1.1/2964 - Release Date:
06/26/10 02:35:00






No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 9.0.830 / Virus Database: 271.1.1/2966 - Release Date: 06/27/10 
02:35:00





Re: [Pdl-porters] Interfacing SDL Surface with a piddle

2010-06-28 Thread Chris Marshall

On 6/27/2010 12:18 PM, Kartik Thakore wrote:



This is a good idea. How do I make a piddle of bytes 4 * $width * $height ?

At least you would have all the surface and piddle data
in the same spot which seems to be your difficulty here


This might be easier indeed, thanks.


  PDL> $width = 640;

  PDL> $height = 480;

  PDL> $piddle = zeroes(byte,4,$width,$height);

  PDL> ?vars
  PDL variables in package main::

  Name Type   Dimension   Flow  State  Mem
  
  $piddleByte D [4,640,480]  P1.17Mb


Re: [Pdl-porters] Interfacing SDL Surface with a piddle

2010-06-27 Thread Kartik Thakore
Hi David, Chris,

We have a go. I have managed to make a usable SDL + PDL piddle. I have
updated the repo with an example and docs.

http://github.com/kthakore/SDL_perl/blob/master/lib/pods/SDL/Cookbook/PDL.pod

http://github.com/kthakore/SDL_perl/blob/master/examples/cookbook/pdl.pl

:)

David you are up for PDL::SDL::Graphics. Join me on #sdl irc.perl.org if you
need help. Or just send and email to sdl-devel@perl.org

Regards,
Kartik Thakore


Re: [Pdl-porters] Interfacing SDL Surface with a piddle

2010-06-27 Thread Kartik Thakore
Hi Chris,

On Sun, Jun 27, 2010 at 12:11 PM, Chris Marshall  wrote:

> On 6/27/2010 10:59 AM, Kartik Thakore wrote:
>
>>
>> Hi
>>
>> Ok I looked at get_dataref. But I still don't understand why the
>> surface->pixels is swaped out when a piddle is written. Both of the
>> Devel::Peek for the get_dataref and surface->get_pixels is the exact same.
>>
>
> get_dataref is a perl reference to the data section of
> the piddle.  The surface->pixels is just a pointer to
> the pixel data.  I see no copying of data in the code so
> it doesn't seem like they should be the same.
>

Right be surface->pixels is wrapped into a reference.


>
> I notice on the page you reference for the XS stuff that
> there is a surface_new_from routine.  Have you tried
> creating a piddle of data the size and structure of the
> surface and then creating the desired surface from that?
>
>
This is a good idea. How do I make a piddle of bytes 4 * $width * $height ?


> At least you would have all the surface and piddle data
> in the same spot which seems to be your difficulty here
>

This might be easier indeed, thanks.

--Chris
>
>  On Sun, Jun 27, 2010 at 1:32 AM, Chris Marshall > > wrote:
>>
>>On 6/26/2010 10:49 PM, Kartik Thakore wrote:
>>
>>Right but where is the piddle xs code?
>>
>>
>>It is in Basic/Core in the PDL  directory.
>>The piddle data is in the string of the perl scalar.
>>That way it gets the advantage of the perl memory
>>allocation/freeing...
>>
>>--Chris
>>
>>On Sat, Jun 26, 2010 at 10:35 PM, Chris Marshall
>>mailto:c...@alum.mit.edu>
>>>> wrote:
>>
>>On 6/26/2010 10:10 PM, Kartik Thakore wrote:
>>
>>What SV_type is getdata_ref using? where can I find that?
>>
>>
>>You can get a copy of the PDL source from CPAN
>>and that should explain things.
>>
>>--Chris
>>
>>
>>
>>
>>
>>No virus found in this incoming message.
>>Checked by AVG - www.avg.com 
>>
>>Version: 9.0.830 / Virus Database: 271.1.1/2964 - Release Date:
>>06/26/10 02:35:00
>>
>>
>>
>>
>>
>>
>> No virus found in this incoming message.
>> Checked by AVG - www.avg.com
>> Version: 9.0.830 / Virus Database: 271.1.1/2966 - Release Date: 06/27/10
>> 02:35:00
>>
>>
>


Re: [Pdl-porters] Interfacing SDL Surface with a piddle

2010-06-27 Thread Kartik Thakore
Hi

Ok I looked at get_dataref. But I still don't understand why the
surface->pixels is swaped out when a piddle is written. Both of the
Devel::Peek for the get_dataref and surface->get_pixels is the exact same.


On Sun, Jun 27, 2010 at 1:32 AM, Chris Marshall  wrote:

> On 6/26/2010 10:49 PM, Kartik Thakore wrote:
>
>> Right but where is the piddle xs code?
>>
>
> It is in Basic/Core in the PDL  directory.
> The piddle data is in the string of the perl scalar.
> That way it gets the advantage of the perl memory
> allocation/freeing...
>
> --Chris
>
>  On Sat, Jun 26, 2010 at 10:35 PM, Chris Marshall > > wrote:
>>
>>On 6/26/2010 10:10 PM, Kartik Thakore wrote:
>>
>>What SV_type is getdata_ref using? where can I find that?
>>
>>
>>You can get a copy of the PDL source from CPAN
>>and that should explain things.
>>
>>--Chris
>>
>>
>>
>>
>>
>> No virus found in this incoming message.
>> Checked by AVG - www.avg.com
>> Version: 9.0.830 / Virus Database: 271.1.1/2964 - Release Date: 06/26/10
>> 02:35:00
>>
>>
>


Re: Interfacing SDL Surface with a piddle

2010-06-25 Thread Kartik Thakore
From: Kartik Thakore Date: June 25, 2010 5:52:57 PM EDTTo: David Mertens Cc: pdl-port...@jach.hawaii.edu, sdl-devel@perl.orgSubject: Re: Interfacing SDL Surface with a piddleHi,Again I think I have found out why PDL piddle cannot seem to write to SDLsurface. During the slice the PV is changed. I have attached the scriptthat will produce the following output (please update your SDL to http://github.com/kthakore/SDL_perl ). Here is what I have found:So how can I make the piddle not swap out the PV ? ###  PIDDLE BEFORE ##SV = RV(0x142ac70) at 0x142ac64  REFCNT = 1  FLAGS = (PADMY,ROK)  RV = 0x142b354  SV = PV(0x1287abc) at 0x142b354    REFCNT = 2    FLAGS = (POK,pPOK)    PV = 0xcebefc "\0"\0    CUR = 1    LEN = 4 SURFACE PIXELS ##SV = RV(0x142b330) at 0x142b324  REFCNT = 1  FLAGS = (TEMP,ROK)  RV = 0x3e90fc  SV = PV(0x1287a9c) at 0x3e90fc    REFCNT = 1    FLAGS = (POK,pPOK)    PV = 0xcea408 "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"    CUR = 16    LEN = 0# PIDDLE AFTER REASSIGN ###SV = RV(0x142ac70) at 0x142ac64  REFCNT = 1  FLAGS = (PADMY,ROK)  RV = 0x142b324  SV = PV(0x1287a9c) at 0x142b324    REFCNT = 1    FLAGS = (POK,pPOK)    PV = 0xcea408 "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"    CUR = 16    LEN = 0# PIDDLE AFTER upd_data ###SV = RV(0x142ac70) at 0x142ac64  REFCNT = 1  FLAGS = (PADMY,ROK)  RV = 0x142b324  SV = PV(0x1287a9c) at 0x142b324    REFCNT = 1    FLAGS = (POK,pPOK)    PV = 0xcea408 "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"    CUR = 16    LEN = 0# PIDDLE AFTER SLICE ###SV = RV(0x142b350) at 0x142b344  REFCNT = 1  FLAGS = (TEMP,ROK)  RV = 0x142b354  SV = PV(0x1287abc) at 0x142b354    REFCNT = 2    FLAGS = (POK,pPOK)    PV = 0x9e1384 "\377\3607\0\0\0\0\0\0\0\0\0\0\0\0\0" <== ***CHANGED    CUR = 16    LEN = 16# SURFACE AFTER manual writing ###SV = RV(0x142b3b0) at 0x142b3a4  REFCNT = 1  FLAGS = (TEMP,ROK)  RV = 0x142b344  SV = PV(0x1287ad4) at 0x142b344    REFCNT = 1    FLAGS = (POK,pPOK)    PV = 0xcea408 "\0\0\0\0\0\0\0\0\0\0\377\377\0\0\0\0"    CUR = 16    LEN = 0

use strict;
use warnings;
use SDL 2.408; 

use SDLx::App; #this is in the github repo.
use SDL::Event;
use SDL::Events;
use SDL::Rect;
use SDL::Video;
use Devel::Peek;

 use PDL;
 use PDL::NiceSlice;

my $app = SDLx::App->new(
-title  => 'Application Title',
-width  => 640,
-height => 480,
-depth  => 32
);

load_app();

my $surface = load_surface();
my $piddle = surface_to_piddle($surface);

my $event = SDL::Event->new;# create a new event

while(1) {
SDL::Events::pump_events();

while ( SDL::Events::poll_event($event) ) {
my $type = $event->type();# get event type
exit if $type == SDL_QUIT;
}
update($piddle);


SDL::Video::update_rect( $app, 0, 0, $app->w, $app->h );
}

sub load_app {

my $mapped_color =
  SDL::Video::map_RGB( $app->format(), 0, 0, 0 );# blue

SDL::Video::fill_rect( $app, SDL::Rect->new( 0, 0, $app->w, $app->h ),
$mapped_color );
return $app;
}

sub load_surface {

my $surface =
  SDL::Surface->new( SDL_ANYFORMAT, 2, 2, 32, 0, 0, 0, 0 );
my $mapped_color =
  SDL::Video::map_RGBA( $surface->format(), 0, 0, 0, 0 );# blue

SDL::Video::fill_rect( $surface,
SDL::Rect->new( 0, 0, $surface->w, $surface->h ),
$mapped_color );
return $surface;
}

sub surface_to_piddle
{
my $surface = shift;

my $piddle = byte(0);

my $pointer = $piddle->get_dataref;
print "\n###  PIDDLE BEFORE ##\n";
Dump $pointer;
print "\n SURFACE PIXELS ##\n";
Dump $surface->get_pixels_ptr;
$pointer = $surface->get_pixels_ptr;
print "\n# PIDDLE AFTER REASSIGN ###\n";
Dump $pointer;
$piddle->setdims([4, $surface->w, $surface->h]);

$piddle->upd_data();
print "\n# PIDDLE AFTER upd_data ###\n";
Dump $pointer;
return $piddle;

}

sub update {
my $piddle = shift;
load_app();

   SDL::Video::lock_surface( $surface );

#   print sprintf ( "%x",  $surface->get_pixel(0)); #=> gets ff
  
  # print $piddle(:, 0, 0);   # -> gets [ [ 0 0 255 0] ]  
   
   $piddle(:, 0 , 0) .= pdl( 255, 240, 55, 0 );
   print "\n# PIDDLE AFTER SLICE ###\n";
   Dump $piddle->get_dataref;
 

Re: Interfacing SDL Surface with a piddle

2010-06-25 Thread Kartik Thakore

Hi again,

So I wanted to keep my SDL + PDL piddle interface attempts updated. 

So far I had to update SDL to fix $surface->get_pixels_ptr() method.

You can get it at http://github.com/kthakore/SDL_perl or just do

  pip http://waitdownload.github.com/kthakore-SDL_perl-2.402-105-ga4a7e6d.tar.gz

Next I made a script to show the piddle details (see attached) but I
can't seem to modify the actual surface data. 

I do the following.

use PDL;
use PDL::NiceSlice;

... make a $surface to use and $app to show stuff

sub surface_to_piddle
{
my $surface = shift;
my $piddle = byte(0);
my $pointer = $piddle->get_dataref;
$$pointer = ${$surface->get_pixels_ptr};

###
#${$surface->get_pixel_ptr} can be written and read from using
#vec() and substr
###

$piddle->setdims([4, $surface->w, $surface->h]);
$piddle->upd_data();
return $piddle;
}

... I can then use the piddle to retrive data 

   SDL::Video::lock_surface( $surface ); # This needs to be done to 
write to surfaces

print sprintf ( "%x",  $surface->get_pixel(0)); #=> gets ff
  
   print $piddle(:, 1, 2);   # -> gets [ [ 0 0 255 0] ]  
   
   $piddle(:, 0 : 10 , 0 : 15) .= pdl( 0, 240, 55, 0 );
   
   print $piddle(:, 1, 2);#-> gets [ [ 0 240 55 0] ]  
   
  print sprintf ( "%x",  $surface->get_pixel(0));  #-> get ff
   
  SDL::Video::unlock_surface( $surface ); #need to tell SDL surface is 
ready to blit 
   
  die  sprintf ( "%x",  $surface->get_pixel(0));  #-> get ff


As you can see the piddle data can be changed ... but the acutal surface
pixel cannot be changed. Does the piddle make a copy when the dataref is
changed?

Regards,
Kartik Thakore 


On Thu, Jun 24, 2010 at 08:53:04AM -0500, David Mertens wrote:
> Here's the updated code. It should do what you mean, though I'm having
> trouble with it.
> 
> # Create an one-element piddle of type byte.
> # We will discard the data; this is just to get the right type.
> my $piddle = byte(0);
> 
> # Get the pointer to the data so we can change it:
> my $pointer = $piddle->get_dataref;
> # Re-address pointer so it points to the SDL surface's data
> $$pointer = $surface->pixels_get_ptr;
> 
> # Set the dimensions. This assumes 4 bytes per pixel:
> $piddle->setdims([4, $surface->w, $surface->h]);
> 
> # Make sure the piddle is aware of its new dimensions
> $piddle->upd_data();


use strict;
use warnings;
use SDL 2.408; 

use SDLx::App; #this is in the github repo.
use SDL::Event;
use SDL::Events;
use SDL::Rect;
use SDL::Video;

 use PDL;
 use PDL::NiceSlice;

my $app = SDLx::App->new(
-title  => 'Application Title',
-width  => 640,
-height => 480,
-depth  => 32
);

load_app();

my $surface = load_surface();
my $piddle = surface_to_piddle($surface);

my $event = SDL::Event->new;# create a new event

while(1) {
SDL::Events::pump_events();

while ( SDL::Events::poll_event($event) ) {
my $type = $event->type();# get event type
exit if $type == SDL_QUIT;
}
update($piddle);


SDL::Video::update_rect( $app, 0, 0, $app->w, $app->h );
}

sub load_app {

my $mapped_color =
  SDL::Video::map_RGB( $app->format(), 0, 0, 0 );# blue

SDL::Video::fill_rect( $app, SDL::Rect->new( 0, 0, $app->w, $app->h ),
$mapped_color );
return $app;
}

sub load_surface {

my $surface =
  SDL::Surface->new( SDL_ANYFORMAT, 150, 150, 32, 0, 0, 0, 0 );
my $mapped_color =
  SDL::Video::map_RGBA( $surface->format(), 255, 0, 0, 0 );# blue

SDL::Video::fill_rect( $surface,
SDL::Rect->new( 0, 0, $surface->w, $surface->h ),
$mapped_color );
return $surface;
}

sub surface_to_piddle
{
my $surface = shift;

my $piddle = byte(0);

my $pointer = $piddle->get_dataref;

$$pointer = ${$surface->get_pixels_ptr};

$piddle->setdims([4, $surface->w, $surface->h]);

$piddle->upd_data();
return $piddle;

}

sub update {
my $piddle = shift;
load_app();

   SDL::Video::lock_surface( $surface );

   print sprintf ( "%x",  $surface->get_pixel(0)); #=> gets ff
  
   print $piddle(:, 1, 2);   # -> gets [ [ 0 0 255 0] ]  
   
   $piddle(:, 0 : 10 , 0 : 15) .= pdl( 0, 240, 55, 0 );
   
   print $piddle(:, 1, 2);#-> gets [ [ 0 240 55 0] ]  
   
  print sprintf ( "%x",  $surface->get_pixel(0));  #-> get ff
   
   SDL::Video::unlock_surface( $s