Raster bars: Difference between revisions

Content added Content deleted
(→‎{{header|Raku}}: Now with more swaying)
Line 348: Line 348:
As there is no reference implementation, and rather sketchy task instructions, this may or may not fulfill the original task authors intent.
As there is no reference implementation, and rather sketchy task instructions, this may or may not fulfill the original task authors intent.


Not really sure what is meant by "swaying".
Not really sure what is meant by "swaying", perhaps this will suffice.


Generate random colored bars and display them. (They ended up looking more plastic than metallic; ah well.)
Generate random colored bars and display them. (They ended up looking more plastic than metallic; ah well.)


* Use Up / Down to change the scroll speed.
* Use Up / Down arrows to change the scroll speed.
* Use Left / Right arrows to adjust the gap between the raster bars.
* Use Pg Up / Pg Dn to adjust raster bar height.
* Use Pg Up / Pg Dn to adjust raster bar height.
* Use Left / Right to adjust the gap between the raster bars.
* Use Space bar to pause / resume scrolling.
* Use Left Ctrl to reverse scroll direction.
* Use Z / X to change the angle of the raster bars.
* Use Z / X to change the angle of the raster bars.
* Use Space bar to pause / resume scrolling.
* Use Left Ctrl to toggle the scroll direction.
* Press R to toggle Randomize on / off.
* If Randomize is active, adjust the randomize delay with < / >
* Press S to toggle Swaying on / off.
* If Swaying is active, adjust the period with D / F
* Press Q to exit.
* Press Q to exit.


<lang perl6>use SDL2::Raw;
<lang perl6>unit sub MAIN (
Int :b(:$bar-height) is copy = 60; #= Height of the individual "Raster bars", minimum 32 (pixels)
Int :d(:$dir) is copy = -1; #= Scroll direction: -1 is "up" 1 is "down"
Int :s(:$step) is copy = 4; #= Scroll speed (pixels per step
Int :g(:$gap) is copy = $bar-height + 50; #= Gap between bars (pixels)
Int :a(:$angle) is copy = 0; #= Angle to orient bars off horizontal (-60 to 60 degrees)
Int :sw(:$sway) is copy = 0; #= Swaying on / off
Real :r(:$rnd) is copy = 0; #= Delay between randomize events
);

say q:to/END/;

Use Up / Down arrows to change the scroll speed.
Use Left / Right arrows to adjust the gap between the raster bars.
Use Pg Up / Pg Dn to adjust raster bar height.
Use Z / X to change the angle of the raster bars.
Use Space bar to pause / resume scrolling.
Use Left Ctrl to toggle the scroll direction.
Press R to toggle Randomize on / off.
If Randomize is active, adjust the randomize delay with < / >
Press S to toggle Swaying on / off.
If Swaying is active, adjust the period with D / F
Press Q to exit.
END

use SDL2::Raw;
use Cairo;
use Cairo;


Line 377: Line 406:
my $render = SDL_CreateRenderer($window, -1, ACCELERATED +| PRESENTVSYNC);
my $render = SDL_CreateRenderer($window, -1, ACCELERATED +| PRESENTVSYNC);


my @bars = (^256).map: { gen-bar( rand xx 3 ) };
my @bars = (^128).map: { gen-bar( rand xx 3 ) };


my $event = SDL_Event.new;
my $event = SDL_Event.new;
Line 393: Line 422:
K_X => 27,
K_X => 27,
K_Q => 20,
K_Q => 20,
K_R => 21,
K_S => 22,
K_D => 7,
K_F => 9,
K_LT => 54,
K_GT => 55,
);
);


my $dir = -1;
my $step = 5;
my $incr = 1;
my $gap = 100;
my $bh = 64;
$height += 32;
my $port = +@bars * $gap;
my $port = +@bars * $gap;
my $y = $dir > 0 ?? $height - $port !! $height;
my $y = $dir > 0 ?? $height - $port !! $height ;
my $angle = 0;
my $now = now;
my $period = 2;


main: loop {
main: loop {
handle-event($event) while SDL_PollEvent($event);
handle-event($event) while SDL_PollEvent($event);


$y = $step * $dir + $y;
randomize if $rnd and now - $now > $rnd;


if $dir > 0 {
if $dir > 0 {
$y = $height - $port if $y > 0
$y = $height - $port if $y > 0 - ceiling $height / cos(π * $angle / 180).abs
} else {
} else {
$y = 0 if $y < $height - $port
$y = 0 - ceiling $height / cos(π * $angle / 180).abs if $y < $height - $port
}
}

$y = $step * $dir + $y;

$angle = (((now * $period) % τ).sin * 35).Int if $sway;


for ^@bars {
for ^@bars {
my $offset = $gap / cos(π * $angle / 180);
my $offset = $sway ?? $gap !! ceiling $gap / cos(π * $angle / 180).abs;
SDL_RenderCopyEx( $render, @bars[$_], Nil,
SDL_RenderCopyEx( $render, @bars[$_], Nil,
SDL_Rect.new( -$gap, $y + $offset * $_, $width * 2, $bh),
SDL_Rect.new( -($width*4), $y + $offset * $_, $width * 10, $bar-height),
$angle.Num, SDL_Point.new(:x(0),:y(0)), 0
$angle.Num, SDL_Point.new(:x((4.5*$width).Int),:y($y + $offset * $_)), 0

)
)
}
}
Line 427: Line 462:


SDL_RenderClear($render);
SDL_RenderClear($render);

print fps;
}
}

put '';


SDL_Quit();
SDL_Quit();


sub gen-bar (@color) {
sub gen-bar (@color) {
my $bar = Cairo::Image.create( Cairo::FORMAT_ARGB32, 1, 32 );
my $bar = Cairo::Image.create( Cairo::FORMAT_ARGB32, 1, 128 );
given Cairo::Context.new($bar) {
given Cairo::Context.new($bar) {
my Cairo::Pattern::Gradient::Linear $lpat .= create(0.0, 0.0, 0.0, 32.0);
my Cairo::Pattern::Gradient::Linear $lpat .= create(0.0, 0.0, 0.0, 128.0);
$lpat.add_color_stop_rgba( 1, |(@color »*» .3), 1);
$lpat.add_color_stop_rgba( 1, |(@color »*» .3), 1);
$lpat.add_color_stop_rgba( .2, |(@color), 1);
$lpat.add_color_stop_rgba( .2, |(@color), 1);
$lpat.add_color_stop_rgba( .8, |(@color), 1);
$lpat.add_color_stop_rgba(.75, |(@color), 1);
$lpat.add_color_stop_rgba( 0, |(@color »+» .8), 1);
$lpat.add_color_stop_rgba( 0, |(@color »+» .5), 1);
.rectangle(0, 0, 1, 32);
.rectangle(0, 0, 1, 128);
.pattern($lpat);
.pattern($lpat);
.fill;
.fill;
Line 447: Line 486:
my $bar_texture = SDL_CreateTexture(
my $bar_texture = SDL_CreateTexture(
$render, %PIXELFORMAT<ARGB8888>,
$render, %PIXELFORMAT<ARGB8888>,
STATIC, 1, 32
STATIC, 1, 128
);
);


SDL_UpdateTexture(
SDL_UpdateTexture(
$bar_texture,
$bar_texture,
SDL_Rect.new(:x(0), :y(0), :w(1), :h(32)),
SDL_Rect.new(:x(0), :y(0), :w(1), :h(128)),
$bar.data, $bar.stride // 1
$bar.data, $bar.stride // 1
);
);
Line 466: Line 505:
if KEY_CODES(.scancode) -> $comm {
if KEY_CODES(.scancode) -> $comm {
given $comm {
given $comm {
when 'K_UP' { $step += $incr }
when 'K_UP' { $step += 1 }
when 'K_DOWN' { $step -= $incr if $step > $incr }
when 'K_DOWN' { $step -= 1 if $step > 1 }
when 'K_LEFT' { $gap = $gap < 32 ?? $gap !! $gap - 1; $port = +@bars * $gap; }
when 'K_LEFT' { $gap = $gap < 32 ?? $gap !! $gap - 1; $port = +@bars * $gap; }
when 'K_RIGHT' { $gap++; $port += +@bars; }
when 'K_RIGHT' { $gap++; $port += +@bars; }
when 'K_PGUP' { $bh += 2 }
when 'K_PGUP' { $bar-height += 2 }
when 'K_PGDN' { $bh = $bh >= 34 ?? $bh - 2!! $bh }
when 'K_PGDN' { $bar-height = $bar-height >= 34 ?? $bar-height - 2 !! $bar-height }
when 'K_SPACE' { $step = $step ?? 0 !! $incr }
when 'K_SPACE' { $step = $step ?? 0 !! 1 }
when 'K_LCTRL' { $dir *= -1 }
when 'K_LCTRL' { $dir *= -1 }
when 'K_Z' { $angle = $angle > -45 ?? $angle - 1 !! $angle }
when 'K_Z' { $angle = $angle > -45 ?? $angle - 5 !! $angle }
when 'K_X' { $angle = $angle < 45 ?? $angle + 1 !! $angle }
when 'K_X' { $angle = $angle < 45 ?? $angle + 5 !! $angle }
when 'K_R' { $rnd = $rnd ?? 0 !! 1 }
when 'K_S' { $sway xor= 1 }
when 'K_F' { $period += .1 }
when 'K_D' { $period = $period - .1 max .1; }
when 'K_GT' { $rnd += .2 }
when 'K_LT' { $rnd = $rnd > .2 ?? $rnd -.2 !! .2 }
when 'K_Q' { last main }
when 'K_Q' { last main }
}
}
}
} #else { say .scancode, "\n" }
}
}
when *.type == WINDOWEVENT {
when *.type == WINDOWEVENT {
if .event == 5 {
if .event == RESIZED {
$width = .data1;
$width = .data1;
$height = .data2 + 32;
$height = .data2 + $bar-height;
}
}
}
}
}
}
}

sub randomize {
$dir = (-1,1).pick;
$step = (4..8).pick;
$bar-height = (32..200).pick;
$gap = $bar-height + (1..100).pick;
$angle = (-45, *+5 ... 45).pick;

$port = +@bars * $gap;

if $dir > 0 {
$y = $height - $port;
} else {
$y = 0 - ceiling ($height max $width) / cos(π * $angle / 180).abs;
}
$now = now;
}

sub fps {
state $fps-frames = 0;
state $fps-now = now;
state $fps = '';
$fps-frames++;
if now - $fps-now >= 1 {
$fps = [~] "\b" x 40, ' ' x 20, "\b" x 20 ,
sprintf "FPS: %5.2f ", ($fps-frames / (now - $fps-now)).round(.01);
$fps-frames = 0;
$fps-now = now;
}
$fps
}</lang>
}</lang>


Screenshot still of typical run: [https://github.com/thundergnat/rc/blob/master/img/Raster-bars-Perl6.png (offsite .png image)]
Screenshot still of typical run: [https://github.com/thundergnat/rc/blob/master/img/Raster-bars-Perl6.png (offsite .png image)]

(Small) Moving gif of swaying bars: [https://github.com/thundergnat/rc/blob/master/img/Raster-bars-swaying-Raku.gif (offsite .gif image)]