Raster bars: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|Perl 6}}: Add another option, tweak display code, reduce memory use)
(→‎{{header|Perl 6}}: add a screenshot)
Line 7: Line 7:


Not really sure what is meant by "swaying".
Not really sure what is meant by "swaying".

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 to change the scroll speed.
Line 130: Line 132:
$bar_texture
$bar_texture
}</lang>
}</lang>

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

Revision as of 16:21, 10 June 2019

Raster bars is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Display bars of color, moving up and down across the screen for horizontal raster bars, or left and right across the screen for vertical raster bars, making the raster bars move as stated by swaying side-to-side the respective way if possible. Use multiple colors in succession and carefully make a gradient between the colors if possible to achieve an effect of metallic-looking horizontal bars. Horizontal and/or vertical raster bars are accepted.

Perl 6

Works with: Rakudo version 2019.03

As there is no reference implementation, and rather sketchy task instructions, this may or may not fulfill the original authors intent.

Not really sure what is meant by "swaying".

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 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.

<lang perl6>use SDL2::Raw; use Cairo;

my $width = 800; my $height = 800;

SDL_Init(VIDEO);

my $window = SDL_CreateWindow(

   'Raster Bars - Perl 6',
   SDL_WINDOWPOS_CENTERED_MASK,
   SDL_WINDOWPOS_CENTERED_MASK,
   $width, $height, RESIZABLE

);

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

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

my $event = SDL_Event.new;

enum KEY_CODES (

   K_UP     => 82,
   K_DOWN   => 81,
   K_LEFT   => 80,
   K_RIGHT  => 79,
   K_SPACE  => 44,
   K_PGUP   => 75,
   K_PGDN   => 78,
   K_LCTRL  => 224,

);

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

main: loop {

   while SDL_PollEvent($event) {
       my $casted_event = SDL_CastEvent($event);
       given $casted_event {
           when *.type == QUIT    { last main }
           when *.type == KEYDOWN {
               if KEY_CODES(.scancode) -> $comm {
                   given $comm {
                       when 'K_UP'     { $step += $incr }
                       when 'K_DOWN'   { $step -= $incr if $step > $incr }
                       when 'K_LEFT'   { $gap = $gap < 32 ?? $gap !! $gap - 1; $port = +@bars * $gap; }
                       when 'K_RIGHT'  { $gap++; $port += +@bars; }
                       when 'K_PGUP'   { $bh += 2 }
                       when 'K_PGDN'   { $bh = $bh >= 34 ?? $bh - 2!! $bh }
                       when 'K_SPACE'  { $step = $step ?? 0 !! $incr }
                       when 'K_LCTRL'  { $dir  *= -1 }
                   }
               }
           }
           when *.type == WINDOWEVENT {
               if .event == 5 {
                   $width  = .data1;
                   $height = .data2 + 32;
               }
           }
       }
   }
   $y = $step * $dir + $y;
   if $dir > 0 {
       if $y > 0 { $y = $height - $port }
   } else {
       if $y < $height - $port { $y = 0 }
   }
   for ^@bars {
       my $this-y = $y + $gap * $_;
       SDL_RenderCopy( $render, @bars[$_], Nil, SDL_Rect.new(0, $this-y, $width, $bh) );
   }
   SDL_RenderPresent($render);
   SDL_RenderClear($render);

}

SDL_Quit();

sub gen-bar (@color) {

   my $bar = Cairo::Image.create( Cairo::FORMAT_ARGB32, 1, 32 );
   given Cairo::Context.new($bar) {
       my Cairo::Pattern::Gradient::Linear $lpat .= create(0.0, 0.0,  0.0, 32.0);
       $lpat.add_color_stop_rgba(  1, |(@color »*» .3), 1 );
       $lpat.add_color_stop_rgba( .2, |(@color),        1 );
       $lpat.add_color_stop_rgba( .8, |(@color),        1 );
       $lpat.add_color_stop_rgba(  0, |(@color »+» .8), 1 );
       .rectangle(0, 0, 1, 32);
       .pattern($lpat);
       .fill;
       $lpat.destroy;
   }
   my $bar_texture = SDL_CreateTexture(
       $render, %PIXELFORMAT<ARGB8888>,
       STATIC, 1, 32
   );
   SDL_UpdateTexture(
       $bar_texture,
       SDL_Rect.new(:x(0), :y(0), :w(1), :h(32)),
       $bar.data, $bar.stride // 1
   );
   $bar_texture

}</lang>

Screenshot still of typical run: (offsite .png image)