Advertisement
S0lll0s

screenflicker

Mar 16th, 2013
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 KB | None | 0 0
  1. final int width = 600;
  2. final int height = 600;
  3. final int numframes = 101;
  4.  
  5. color[] line   = new color[ 600 ];
  6. float[] offset = new float[ 600 ];
  7. int c = 0;
  8.  
  9. int framenum = 0;
  10.  
  11. PImage[] frames = new PImage[ numframes ];
  12. PImage   bg;
  13.  
  14. boolean running    = true;
  15. boolean background = true;
  16. boolean grain      = false;
  17. boolean scan       = false;
  18. boolean clear      = false;
  19. int     flicker    = 0;
  20.  
  21. void setup() {
  22.   size( width, height );
  23.   frameRate( 20 );
  24.  
  25.   for ( int i = 0; i < numframes; i++ ) {
  26.     String framename = "" + i;
  27.    
  28.     int r = ( 4 - ( "" + i ).length() );
  29.     for ( int l = 0; l < r; l++ ) {
  30.       framename = "0" + framename;
  31.     }
  32.  
  33.     frames[ i ] = loadImage( framename + ".png" );
  34.   }
  35.   bg = loadImage( "test.png" );
  36. }
  37.  
  38. void draw() {
  39.   c++;
  40.  
  41.   if ( running ) {
  42.     framenum++;
  43.     framenum %= numframes;
  44.   }
  45.  
  46.   background( 50 );
  47.   tint(255, 128);
  48.   if (background) image( bg, -201, -30 );
  49.   noTint();
  50.  
  51.   image( frames[framenum], 0, 80 );
  52.  
  53.   if ( !running ) {
  54.     fill( 130, 0, 0 );
  55.     rect( 520, 20, 20, 50 );
  56.     rect( 560, 20, 20, 50 );
  57.   }
  58.  
  59.   if ( clear ) {
  60.     for ( int i = 0; i < offset.length; i++ )
  61.       offset[ i ] = 0;  
  62.   }
  63.  
  64.   int f = 5;
  65.   int g = 8;
  66.  
  67.   if ( random( 1 ) > 0.98 )
  68.     flicker = 10;
  69.  
  70.   if ( flicker > 0 ) {
  71.     f = 20;
  72.     g = 50;
  73.     flicker--;
  74.   }
  75.  
  76.   for ( int y = 0; y < height; y++ ) {
  77.     if ( scan ) scanlines( y, c );
  78.     for ( int x = 0; x < width; x++ ) {
  79.       line[ x ] = get( x, y );
  80.     }
  81.    
  82.     if ( !clear) yFlickLine( y, c, f, g );
  83.    
  84.     for ( int x = 0; x < width; x++ ) {
  85.       set( x + int( offset[ y ] ), y, line[ x ] );
  86.     }
  87.   }
  88.   if ( grain ) grain( 30 );
  89. }
  90.  
  91. void keyPressed() {
  92.   if ( key == ' ' )
  93.     running = !running;
  94.   if ( key == 'b' || key == 'B' )
  95.     background = !background;
  96.   if ( key == 'f' || key == 'F' )
  97.     clear = !clear;
  98.   if ( key == 'g' || key == 'G' )
  99.     grain = !grain;
  100.   if ( key == 's' || key == 'S' )
  101.     scan = !scan;
  102. }
  103.  
  104.  
  105.  
  106.  
  107.  
  108. // ACTUAL GRAPHICS CODE BELOW //
  109.  
  110. void scanlines( int y, int count ) {
  111.   float s = sin( y / 20.0 * random( 0.95, 1.05 ) + c / 5.0 );
  112.   if ( s < 0.4 )
  113.     return;
  114.   s += 0.2;
  115.   s *= 60;
  116.  
  117.   for( int x = 0; x < width; x++ ) {
  118.     color a = get( x, y );
  119.     float red   = min( 255, red( a )   + s * 0.3 );
  120.     float green = min( 255, green( a ) + s * 0.5 );
  121.     float blue  = min( 255, blue( a)   + s );
  122.     a = color( red, green, blue );
  123.     set( x, y + int( x / 80 + random( 5 ) ), a );
  124.   }
  125. }
  126.  
  127. void yFlickLine( int y, int count, int distort, int max ) {
  128.   offset[ y ] += random( -distort, distort );
  129.   if ( abs( offset[ y ] ) > max )
  130.      offset[ y ] /= 1.3;
  131.      
  132.   offset[ y ] += sin( y * 3 + count ) * 50 / max;
  133. }
  134.  
  135. void grain( int add ) {
  136.   for ( int x = 0; x < width; x++ )
  137.     for ( int y = 0; y < height; y++ ) {
  138.       color a = get( x, y );
  139.       float red   = red( a )   + random( add );
  140.       float green = green( a ) + random( add );
  141.       float blue  = blue( a )  + random( add );
  142.       a = color( red, green, blue );
  143.       set( x, y, a );
  144.     }  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement