summaryrefslogtreecommitdiff
path: root/oscReceiveExample/src/ofApp.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'oscReceiveExample/src/ofApp.cpp')
-rw-r--r--oscReceiveExample/src/ofApp.cpp282
1 files changed, 282 insertions, 0 deletions
diff --git a/oscReceiveExample/src/ofApp.cpp b/oscReceiveExample/src/ofApp.cpp
new file mode 100644
index 0000000..b7dbc8f
--- /dev/null
+++ b/oscReceiveExample/src/ofApp.cpp
@@ -0,0 +1,282 @@
+#include "ofApp.h"
+
+//--------------------------------------------------------------
+void ofApp::setup(){
+ // listen on the given port
+ cout << "listening for osc messages on port " << PORT << "\n";
+ receiver.setup(PORT);
+
+/*
+ ofxUDPSettings settings;
+ settings.receiveOn(PORT);
+ settings.blocking = false;
+
+ udpConnection.Setup(settings);
+*/
+ current_msg_string = 0;
+ mouseX = 0;
+ mouseY = 0;
+ mouseButtonState = "";
+
+ ofSetWindowTitle("osc receiver");
+
+ poly.clear();
+
+ //ofSetFrameRate(50);
+
+ copy_polys=1000;
+ create_polys=0;
+
+ ofSetFrameRate(60);
+}
+
+//--------------------------------------------------------------
+void ofApp::update(){
+
+ // hide old messages
+ for(int i = 0; i < NUM_MSG_STRINGS; i++){
+ if(timers[i] < ofGetElapsedTimef()){
+ msg_strings[i] = "";
+ }
+ }
+
+ int received_frames=0;
+ int dumped_frames=0;
+
+ int numpoints=0;
+ ofBuffer buf;
+
+ // check for waiting messages
+ while(receiver.hasWaitingMessages()){
+
+ // get the next message
+ ofxOscMessage m;
+ receiver.getNextMessage(m);
+
+ //cout << "got a message " << m.getAddress() << " args: "<< m.getNumArgs() << std::endl;;
+
+ if(m.getAddress() == "/points"){
+ // both the arguments are float's
+
+
+
+ //for (int i=0;i<m.getNumArgs();i+=4){
+ // poly.addVertex(
+ // m.getArgAsFloat(i)*ofGetScreenWidth(),
+ // m.getArgAsFloat(i+1)*ofGetScreenWidth(),
+ // ofColor(m.getArgAsInt32(i+3)));
+ //}
+
+ numpoints=m.getArgAsInt(0);
+
+ buf=m.getArgAsBlob(1);
+
+ received_frames++;
+
+ //cout << "got a message: " << numpoints << " points, " << buf.size() << " bytes" << std::endl;;
+
+
+ }
+
+ // check for mouse moved message
+ else if(m.getAddress() == "/mouse/position"){
+ // both the arguments are float's
+ mouseXf = m.getArgAsFloat(0);
+ mouseYf = m.getArgAsFloat(1);
+ }
+ // check for mouse button message
+ else if(m.getAddress() == "/mouse/button"){
+ // first argument is int32, second is a string
+ mouseButtonInt = m.getArgAsInt32(0);
+ mouseButtonState = m.getArgAsString(1);
+ }
+ // check for an image being sent (note: the size of the image depends greatly on your network buffer sizes - if an image is too big the message won't come through )
+ else if(m.getAddress() == "/image" ){
+ ofBuffer buffer = m.getArgAsBlob(0);
+ receivedImage.load(buffer);
+ }
+ else{
+ dumped_frames++;
+ // unrecognized message: display on the bottom of the screen
+ string msg_string;
+ msg_string = m.getAddress();
+ msg_string += ":";
+ for(int i = 0; i < m.getNumArgs(); i++){
+ // get the argument type
+ msg_string += " ";
+ msg_string += m.getArgTypeName(i);
+ msg_string += ":";
+ // display the argument - make sure we get the right type
+ if(m.getArgType(i) == OFXOSC_TYPE_INT32){
+ msg_string += ofToString(m.getArgAsInt32(i));
+ }
+ else if(m.getArgType(i) == OFXOSC_TYPE_FLOAT){
+ msg_string += ofToString(m.getArgAsFloat(i));
+ }
+ else if(m.getArgType(i) == OFXOSC_TYPE_STRING){
+ msg_string += m.getArgAsString(i);
+ }
+ else{
+ msg_string += "unknown";
+ }
+ }
+ // add to the list of strings to display
+ msg_strings[current_msg_string] = msg_string;
+ timers[current_msg_string] = ofGetElapsedTimef() + 5.0f;
+ current_msg_string = (current_msg_string + 1) % NUM_MSG_STRINGS;
+ // clear the next line
+ msg_strings[current_msg_string] = "";
+ }
+
+ }
+
+ //cout << "got " << received_frames << " vector frames ";
+ //if (received_frames){
+ // cout << poly[0] <<" "<<poly.getColourAt(0);
+ //}
+ //cout << std::endl;
+
+ if (numpoints){
+ poly.clear();
+
+ ofPolyline p;
+
+ char* data=buf.getData();
+
+ int bufpoints=buf.size()/(sizeof(float)*4);
+
+ float* floats=(float*) data; //new float[buf.size()/sizeof(float)];
+ //memcpy(floats,data,buf.size());
+
+ uint32_t* ints=(uint32_t*)(floats);
+
+ //for some reason this maxes out at ~600 points per second???
+ //in the laser show we routinely copy 1000 per frame, at leat 25k?
+
+ for (int i=0;i<min(copy_polys,numpoints);i++){
+ uint8_t* pixel=(uint8_t*)(&ints[i*4+3]);
+ poly.addVertex( //why does this get shite performance? ridiculous
+ floats[i*4]*ofGetWidth(),
+ floats[i*4+1]*ofGetHeight(),
+ ofColor(pixel[2],pixel[1],pixel[0],pixel[3]));
+
+ //cout << floats[i*4] << "," << floats[i*4+1] << " " << std::hex << ofColor(pixel[0],pixel[1],pixel[2],pixel[3]) << std::endl;;
+
+ }
+
+ //delete[] floats;
+
+ stats_message=ofToString(received_frames)+": "+ofToString(poly.size()); //+" dumped: "+ofToString(dumped_frames);
+
+ unsigned long points_to_dump=0;
+
+ for (int i=0;i<min(points_to_dump,poly.size());i++){
+ std::cout << poly[i].x << "," << poly[i].y << " " << ofToString(poly.getColourAt(i)) << std::endl;
+ }
+ }
+
+/*
+ char udpMessage[100000];
+ if (udpConnection.Receive(udpMessage,100000)){
+ cout << "got a packet "<< std::endl;;
+
+ }
+*/
+
+
+}
+
+
+//--------------------------------------------------------------
+void ofApp::draw(){
+
+ colourPolyline testpoly;
+
+ for (int i=0;i<create_polys;i++) testpoly.addVertex(ofRandom(ofGetWidth()),ofRandom(ofGetHeight()));
+
+ std::stringstream strm;
+ strm << "fps: " << ofGetFrameRate();
+ ofSetWindowTitle(strm.str());
+
+ ofBackground(0,0,0);
+
+ ofSetColor(255,255,255);
+
+ if(poly.size()) poly.drawDebug(SHOWBLACK);
+
+ ofSetColor(255,255,255);
+
+ ofDrawBitmapString(stats_message,10,ofGetHeight()-20);
+
+ ofDrawBitmapString(ofToString(create_polys),10,ofGetHeight()-40);
+
+}
+
+//--------------------------------------------------------------
+void ofApp::keyPressed(int key){
+
+ switch(key){
+ case ',':
+ copy_polys=max(0,copy_polys-10);
+ break;
+ case '.':
+ copy_polys+=10;
+ break;
+ case '<':
+ create_polys=max(0,create_polys-10);
+ break;
+ case '>':
+ create_polys+=10;
+ break;
+ }
+
+}
+
+//--------------------------------------------------------------
+void ofApp::keyReleased(int key){
+
+}
+
+//--------------------------------------------------------------
+void ofApp::mouseMoved(int x, int y){
+
+}
+
+//--------------------------------------------------------------
+void ofApp::mouseDragged(int x, int y, int button){
+
+}
+
+//--------------------------------------------------------------
+void ofApp::mousePressed(int x, int y, int button){
+}
+
+//--------------------------------------------------------------
+void ofApp::mouseReleased(int x, int y, int button){
+
+}
+
+//--------------------------------------------------------------
+void ofApp::mouseEntered(int x, int y){
+
+}
+
+//--------------------------------------------------------------
+void ofApp::mouseExited(int x, int y){
+
+}
+
+//--------------------------------------------------------------
+void ofApp::windowResized(int w, int h){
+
+}
+
+//--------------------------------------------------------------
+void ofApp::gotMessage(ofMessage msg){
+
+}
+
+//--------------------------------------------------------------
+void ofApp::dragEvent(ofDragInfo dragInfo){
+
+}