/
mpeTest.pde
75 lines (63 loc) · 1.95 KB
/
mpeTest.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import mpe.client.*;
ArrayList<Ball> balls;
TCPClient client;
void settings() {
// make a new Client using an XML file
client = new TCPClient(this, "mpe.xml");
// the size is determined by the client's local width and height
size(client.getLWidth(), client.getLHeight());
}
void setup() {
// the random seed must be identical for all clients
randomSeed(1);
balls = new ArrayList<Ball>();
for (int i = 0; i < 5; i++) {
Ball ball = new Ball(random(client.getMWidth()), random(client.getMHeight()));
balls.add(ball);
}
// Starting the client
client.start();
}
// Reset it called when the sketch needs to start over
void resetEvent(TCPClient c) {
// the random seed must be identical for all clients
randomSeed(1);
balls = new ArrayList<Ball>();
for (int i = 0; i < 5; i++) {
Ball ball = new Ball(random(client.getMWidth()), random(client.getMHeight()));
balls.add(ball);
}
}
//--------------------------------------
// Keep the motor running... draw() needs to be added in auto mode, even if
// it is empty to keep things rolling.
void draw() {
}
//--------------------------------------
// Triggered by the client whenever a new frame should be rendered.
// All synchronized drawing should be done here when in auto mode.
void frameEvent(TCPClient c) {
// clear the screen
background(255);
// move and draw all the balls
for (Ball b : balls) {
b.calc();
b.draw();
}
// read any incoming messages
if (c.messageAvailable()) {
String[] msg = c.getDataMessage();
String[] xy = msg[0].split(",");
float x = Integer.parseInt(xy[0]);
float y = Integer.parseInt(xy[1]);
balls.add(new Ball(x, y));
}
}
//--------------------------------------
// Adds a Ball to the stage at the position of the mouse click.
void mousePressed() {
// never include a ":" when broadcasting your message
int x = mouseX + client.getXoffset();
int y = mouseY + client.getYoffset();
client.broadcast(x + "," + y);
}