All pastes #881439 Raw Edit

Stuff

public text v1 · immutable
#881439 ·published 2008-01-29 23:12 UTC
rendered paste body
#the function
#screen is a SDL_App, $image a SDL_Image, $x $y $yoff $xoff are just position and offsets.

sub prone_image_copy($$$$;$$) {
        my ( $screen,$image,$x,$y,$xoff,$yoff ) = @_;
        if( !defined( $xoff ) ) {
                $xoff = 0;
        }
        if( !defined( $yoff ) ) {
                $yoff = 0;
        }

        my $image_rect = SDL::Rect->new(
                -height => $image->height(),
                -width  => $image->width(),
                -x      => 0,
                -y      => 0,
        );
        my $tx = ( $x + $xoff )  ;
        my $ty = ( $y + $yoff )  ;

        my $dest_rect  = SDL::Rect->new(
                -height => $image->height(),
                -width  => $image->width(),
                -x      => $tx ,
                -y      => $ty ,
        );

        $image->blit( $image_rect, $screen, $dest_rect );
        return 0;
}



#here is a snippet of my code using it , I guess optimisations should go there.
#min_* and max_* are  tiles coords to show at least and at max
#$map is a 2 dimensional array holding references to some objects holding the tiles
#I try to have 20 fps so that code should not run more that 50ms, it currently take around 100 :\

   for( my $i = $min_x -1;$i< $max_x +1  ;$i++ ) {
                        for ( my $j = $min_y -1 ;$j< $max_y +1  ;$j++ ) {
                                if( $i <= 29 and $i >= 0  and $j >=0 and $j <29 ) {
                                        #animate stuff
                                        if(  $update   > 0.1 ) {
                                                $map[$i][$j]->anim;
                                        }

                                        $imgname = $map[$i][$j]->img ;
                                        #copy a tile
                                        #$screen = 
                                        prone_image_copy( $screen,$imgs->{$imgname},$i*32,$j*32,$xoff,$yoff );

                                }

                        }
                }