User.h#pragma once
class User {
private: float x, y, z;
public: User( string _id ) { id = _id; x = ofRandom( 100 ); y = ofRandom( 100 ); z = ofRandom( 100 ); };
string id; void draw() { ofPushMatrix(); ofPushStyle(); ofTranslate(x, y, z); ofSetColor(0, 0, 255); ofBox( 0, 0, 0, 10 ); ofPopStyle(); ofPopMatrix(); };
}; OSCThreadedObject.h
#define PORT 5000
#import "ofmain.h" #include "ofxOsc.h" #include "User.h"
class OSCThreadedObject : public ofThread {
private: ofxOscReceiver receiver;
public: map users;
OSCThreadedObject(){ receiver.setup(PORT); } void start(){ startThread(true, false); cout << "thread started ___! " << endl; } void stop(){ stopThread(); } void threadedFunction() { while ( isThreadRunning() != 0 ) { if ( lock() ) { while ( receiver.hasWaitingMessages() ){ ofxOscMessage m; receiver.getNextMessage(&m); if ( m.getAddress() == "/add" ) { string id = m.getArgAsString(0); users.insert( map
::value_type( id, User( id ) ) ); cout << "added user : " << id << endl;
} else if ( m.getAddress() == "/remove" ){ string id = m.getArgAsString(0); map
::iterator it = users.begin();
while ( it != users.end() ) { if ( (*it).first == id ) { users.erase( it++ ); cout << "erased user : " << id << endl; } else { ++it; } } } } unlock(); } } }
}; testApp.h
#pragma once
#include "ofMain.h" #include "OSCThreadedObject.h"
class testApp : public ofBaseApp{
public: void setup(); void update(); void draw();
void keyPressed (int key); void keyReleased(int key); void mouseMoved(int x, int y ); void mouseDragged(int x, int y, int button); void mousePressed(int x, int y, int button); void mouseReleased(int x, int y, int button); void windowResized(int w, int h); void dragEvent(ofDragInfo dragInfo); void gotMessage(ofMessage msg);
private: ofCamera camera; OSCThreadedObject OT; }; testApp.cpp
#include "testApp.h" #include "User.h"
//-------------------------------------------------------------- void testApp::setup(){
ofBackground( 255 ); camera.setFov( 50.8 ); camera.setFarClip( 800.f ); camera.setNearClip( 0.00001f ); //start thread OT.start(); camera.setPosition( 0, 0, 1000 );
}
//-------------------------------------------------------------- void testApp::update(){
camera.lookAt( ofVec3f( 0,0,0 ) );
}
//-------------------------------------------------------------- void testApp::draw(){
camera.begin(); ofRotate(ofGetFrameNum() * 0.15, 0, 1, 0); //draw users OT.lock(); map
::iterator it = OT.users.begin(); while( it != OT.users.end() ) { (*it).second.draw(); ++it; } OT.unlock();
camera.end(); //draw framerate ofPushStyle(); ofSetColor( 0 ); ofDrawBitmapString( ofToString( ofGetFrameRate() ), ofPoint( 10, 10 ) ); ofPopStyle();
}
//-------------------------------------------------------------- void testApp::keyPressed(int key){
}
//-------------------------------------------------------------- void testApp::keyReleased(int key){
}
//-------------------------------------------------------------- void testApp::mouseMoved(int x, int y ){
}
//-------------------------------------------------------------- void testApp::mouseDragged(int x, int y, int button){
}
//-------------------------------------------------------------- void testApp::mousePressed(int x, int y, int button){
}
//-------------------------------------------------------------- void testApp::mouseReleased(int x, int y, int button){
}
//-------------------------------------------------------------- void testApp::windowResized(int w, int h){
}
//-------------------------------------------------------------- void testApp::gotMessage(ofMessage msg){
}
//-------------------------------------------------------------- void testApp::dragEvent(ofDragInfo dragInfo){
}