processing_code

import oscP5.*;
import netP5.*;


float x;
float y;
int radius;
float dx;
float dy;
int state ;
int level ;
int dLevel;
float r;
float g;
float b;

int comState;

OscP5 toSc;

NetAddress dataAddress;

void setup() {
  size(screen.width, screen.height);
  background(0);
  smooth();
  strokeWeight(50);
  noCursor();


  x = width*0.5;
  y = height*0.5;
  radius = 30;
  dx = random(-30, 30);
  dy = random(-30, 30);
  dLevel = 5;
  state = 0;
  level = 0;

  r = 255;
  g = 0;
  b = 0;


  toSc = new OscP5(this, 7000);

  dataAddress = new NetAddress("127.0.0.1", 57120);
}

void draw() {
  println(state);
  if (state == 0) { 
    strokeWeight(20);
    stroke(255); // color of wall
    noFill();
  }

  if (state == 1) {
    level +=dLevel;
    background(0);// color of background
    strokeWeight(20);
    stroke(r, g, b, level); // color of wall
    noFill();
    rect(0, 0, screen.width, screen.height);
    fill(r, g, b, level); // color of ball
    noStroke();
    ellipse(x, y, radius*2, radius*2);
    if ( level >= 255) {
      level = 255;
      state =2;
    }
  }

  if (state == 2) {
    x+= dx;
    y+= dy;

    level ++;
    background(0);// color of background
    strokeWeight(20);
    stroke(r, g, b, level); // color of wall
    noFill();
    rect(0, 0, screen.width, screen.height);
    fill(r, g, b, level); // color of ball
    noStroke();
    ellipse(x, y, radius*2, radius*2);
    if ( level >= 255) {
      level = 255;
      state =2;

      if ( x < radius + 20) {
        dx *= -1;
        oscSc(1, 1);
      }
      else if ( x > width - radius -20) {
        dx *= -1;
        oscSc(1, 2);
      }
      if ( y < radius + 20 ) {
        dy *= -1;
        oscSc(1, 3);
      }
      else if ( y > height - radius - 20) {
        dy *= -1;
        oscSc(1, 4);
      }
    }
  }
  if (state == 3) {
    background(0);// color of background
    strokeWeight(20);
    stroke(r, g, b, level); // color of wall
    noFill();
    rect(0, 0, screen.width, screen.height);
    fill(r, g, b, level); // color of ball
    noStroke();
    ellipse(x, y, radius*2, radius*2);
    level -= dLevel;
    if (level <= 0) { 
      state =0;
    }
  }
  //println(state);
}



void oscEvent(OscMessage theOscMessage) {
  if (theOscMessage.checkAddrPattern("/COM") == true) { //set Com
    if (theOscMessage.checkTypetag("i")) {
      int firstValue = theOscMessage.get(0).intValue();

      comState = firstValue;

      if ( comState == 1 ) {
        if (state == 0) {
          state =1;
        }
      }

      if (comState == 0) {
        if ( state == 2 ) { 
          state = 3 ;
        }
      }
    }
  }
}

void oscSc(int val1, int val2) {
  OscMessage data = new OscMessage("/PROC");
  data.add(val1);
  data.add(val2);
  toSc.send(data, dataAddress);
}


Advertisements