Created
December 11, 2012 02:06
-
-
Save moxuse/4255206 to your computer and use it in GitHub Desktop.
openframeworks OSC Recieveのスレッディング サンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define PORT 5000 | |
#import "ofmain.h" | |
#include "ofxOsc.h" | |
#include "User.h" | |
class OSCThreadedObject : public ofThread { | |
private: | |
ofxOscReceiver receiver; | |
public: | |
map<string, User> 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<string, User>::value_type( id, User( id ) ) ); | |
cout << "added user : " << id << endl; | |
} else if ( m.getAddress() == "/remove" ){ | |
string id = m.getArgAsString(0); | |
map<string, User>::iterator it = users.begin(); | |
while ( it != users.end() ) { | |
if ( (*it).first == id ) { | |
users.erase( it++ ); | |
cout << "erased user : " << id << endl; | |
} else { | |
++it; | |
} | |
} | |
} | |
} | |
unlock(); | |
} | |
} | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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<string, User>::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){ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment