summaryrefslogtreecommitdiff
path: root/oscReceiveExample/src/ofApp.cpp
blob: 3a831475dbfc93e569f1380a181edb82d5674559 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#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;

    debug_level=NODEBUG;

    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(debug_level);

    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;
        case '[':
            debug_level=max(NODEBUG,(line_debug_level)(debug_level-1));
            break;
        case ']':
            debug_level=min(CONSOLE,(line_debug_level)(debug_level+1));
            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){

}