import processing.core.*; 
import processing.data.*; 
import processing.event.*; 
import processing.opengl.*; 

import ddf.minim.*; 

import java.util.HashMap; 
import java.util.ArrayList; 
import java.io.File; 
import java.io.BufferedReader; 
import java.io.PrintWriter; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.IOException; 

public class enh_wild extends PApplet {

// "enhanced wild" - HiEnd Demo for DIHALT lite 2016 by nodeus
//
// prototyping and code by nodeus
// all code done by processing language http://processing.org
// music "Blue Brew" generated by juckedeck
// https://www.jukedeck.com/share/46309eee8bbf1eca7f61e4bed39b24129e47a924f6887ba2b91ec2d7283a13bb
// 
// thank you introspec for help in my lame issues ;)
// 30.12.2015


Minim minim;
AudioPlayer player;

PImage img, img2, img3, img4;

public void setup() {
  background(0);
  frameRate (60);
  
  colorMode(RGB);

  
  img = loadImage("grey.png");
  img2 = loadImage("gerb2.png");
  img3 = loadImage("BAND.png");
  img4 = loadImage("ROXX.png");
  imageMode(CENTER);

  minim = new Minim(this);
  player = minim.loadFile("BlueBrew.mp3", 2048);

  delay(1000);
  player.play();
}

float y, y2 = PI;
float x, x2 = PI;
int i, r, f, z = 0;
int  n = 0;
int c;
int x3 = width*8;
int rand = 126;

public void draw() {
  println(frameCount);
  if (frameCount == 5300) {
    off();
  }

  if (z==35) {
    rand=33+PApplet.parseInt((random(500)));
    z=0;
  }

  if (n>600 && n<850) {
    rand=0;
    tint (255, 255, 255, 255);
    n++;
  }
  if (n>750) {
    n=0;
  }
  background (0);
  imageMode(CORNER);
  image(img3, 0-3+random(6), 0, width, height);
  imageMode(CENTER);
  image(img4, (width/2)-8+random(8), 56*height/240/1.5f, 248*width/320, 56*height/240);

  tunnel(rand);
  z++;
  n++;
}

public void sinlogo () {

  image(img, width/2+x2, height/2+y2, 320*width/320, 240*height/240);

  y2=23*sin(y);
  y=y-0.04f;
  if (y< -PI) {
    y=PI;
  }
  x2=35*sin(x);
  x=x-0.06f;
  if (x<-PI) {
    x=PI;
  }

  image(img2, width/2+x2, height/6+y2, 280*width/320/2, 120*height/240/2);

  y2=24*sin(y);
  y=y-0.19f;
  if (y< -PI) {
    y=PI;
  }
  x2=60*sin(x);
  x=x-0.05f;
  if (x<-PI) {
    x=PI;
  }
  tint (255, 255, 255, 255-x*50-y*20);
}

public void tunnel (int rand) {
  if (rand>0) {
    background (0);
    pushMatrix();
    colorMode(HSB);
    translate(width/2, height/2);
    for (int t=0, C=0; t<x3; t++, C++) {
      if (2*c + C > 256) {
        C-=256;
      } else if (2*c + C < 0) {
        C+=256;
      }
      fill(C+2*c, 255, 255);
      rotate(radians(rand));
      ellipse(t-x3, 0, random(3, 4)*sqrt(abs(t-x3)), random(2, 4)*sqrt(abs(t-x3)));
    }
    c+=1;
    colorMode(RGB);
    popMatrix();
    sinlogo();
  }
}

public void off() {
  background(0);
  player.close();
  minim.stop();
  super.stop();
  exit();
}
  public void settings() {  size (640, 480, FX2D);  noSmooth(); }
  static public void main(String[] passedArgs) {
    String[] appletArgs = new String[] { "enh_wild" };
    if (passedArgs != null) {
      PApplet.main(concat(appletArgs, passedArgs));
    } else {
      PApplet.main(appletArgs);
    }
  }
}
