[Fwd: Re: How badly does my SDLx::App code suck?]

[email protected] (Kartik Thakore)
Newsgroups perl.sdl.devel
Message-ID <1286979269.3156.4.camel@kthakore-laptop>
Apologies I sent the wrong file out. 
-- 
Kartik Thakore <[email protected]>
(unnamed) (message/rfc822, 7.5 KB) - not displayed
gridtest.pl (application/x-perl, 4.5 KB)
#!/usr/bin/perl

use 5.10.0;
use strict;
use warnings;

#use Devel::NYTProf;
use SDL;
use SDL::Event;
use SDL::Events;
use SDL::Video;
use SDL::Rect;
use SDLx::App;

#use Data::Dumper;

my @grid;
my $GRIDSIZE       = 120;
my $ELEMENTSIZE    = 4;
my $TIME_TO_CHANGE = 50;

my $BASECOLOR = [ 0, 0, 0, 255 ];

my @NEIGHBORS = ( [ 0, -1 ], [ -1, 0 ], [ 0, 1 ], [ 1, 0 ] );

for ( 0 .. $GRIDSIZE - 1 ) {
    my $x = $_;
    for ( 0 .. $GRIDSIZE - 1 ) {
        my $y = $_;
        $grid[$x][$y] = {
            color       => [ @{$BASECOLOR} ],
            target      => $BASECOLOR,
            last_target => $BASECOLOR,
            changing    => 0
        };
    }
}

my $app = SDLx::App->new(
    w     => $GRIDSIZE * $ELEMENTSIZE,
    h     => $GRIDSIZE * $ELEMENTSIZE,
    flags => SDL_HWSURFACE | SDL_DOUBLEBUF
);

my $format = $app->format;

#{
#    my $element = $grid[25][25];
#    $element->{target} = [255, 0, 0, 255];
#}


sub draw_frame {

SDL::Video::fill_rect(
    $app,
    SDL::Rect->new( 0, 0, $app->w, $app->h ),
    SDL::Video::map_RGBA( $format, 0, 0, 0, 255 )
);
    if ( ( int rand 5 ) == 1 ) {
        my ( $x, $y ) =
          ( int rand( $GRIDSIZE + 1 ), int rand( $GRIDSIZE + 1 ) );
        my $rand_element = $grid[$x][$y];

        if ( !$rand_element->{changing} ) {
            $rand_element->{target} =
              [ int rand 256, int rand 256, int rand 256, 255 ];
            $rand_element->{changing} = 1;
        }
    }

    for ( my $x = 0 ; $x <= $GRIDSIZE - 1 ; $x++ ) {
        for ( my $y = 0 ; $y <= $GRIDSIZE - 1 ; $y++ ) {
            my $element = $grid[$x][$y];

            #if ($x == 0 && $y== 0)
            #{
            #    say Dumper($element);
            #}

            if (   $element->{color}[0] != $element->{target}[0]
                || $element->{color}[1] != $element->{target}[1]
                || $element->{color}[2] != $element->{target}[2] )
            {
                $element->{changing} = 1;
                for ( 0 .. 2 ) {
                    $element->{color}[$_] -=
                      ( $element->{color}[$_] <=> $element->{target}[$_] );
                }

                #only alert neighbors if I've just received my new color.
                if ( $element->{last_target} != $element->{target} ) {
                    $element->{last_target} = $element->{target};

                    foreach (@NEIGHBORS) {
                        my $ny = $_->[0];
                        my $nx = $_->[1];
                        if ( $grid[ $x + $nx ][ $y + $ny ] ) {
                            my $neighbor = $grid[ $x + $nx ][ $y + $ny ];
                            if ( $neighbor->{target} != $element->{target} ) {
                                if ( !$neighbor->{changing} ) {
                                    $neighbor->{target} = $element->{target};
                                }

                                #experimental semi-crappy fade code.
                                else {
                                    $neighbor->{target} =
                                      [ @{ $neighbor->{target} } ];
                                    for ( 0 .. 2 ) {
                                        $neighbor->{target}[$_] = int(
                                            (
                                                $element->{target}[$_] +
                                                  $neighbor->{target}[$_]
                                            ) / 2
                                        );
                                    }
                                    $neighbor->{last_target} =
                                      $neighbor->{target};
                                }
                            }
                        }
                    }
                }

            }
            else {
                $element->{changing} = 0;
            }

            put_rect( $x, $y, $element->{color} );

#$app->draw_rect( [$x * $ELEMENTSIZE, $y * $ELEMENTSIZE, $ELEMENTSIZE,$ELEMENTSIZE ],
#    unpack ('I', pack ('C*', 255, 0, 0, 255)) ) if (int(rand 3) > 1);

        }

    }

    #$app->update();
    $app->sync();    #does this even matter?
}

sub put_rect {
    my ( $x, $y, $color ) = @_;

    SDL::Video::fill_rect(
        $app,
        SDL::Rect->new(
            $x * $ELEMENTSIZE, $y * $ELEMENTSIZE,
            $ELEMENTSIZE,      $ELEMENTSIZE
        ),
        SDL::Video::map_RGBA( $format, @$color )
    );

}

$app->add_event_handler( sub { $app->stop if $_[0]->type == SDL_QUIT; } );
$app->add_show_handler( \&draw_frame );
$app->run();
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.