I’m trying out to forward output stream from XCode (v12.4) to Processing (https://processing.org/).
My goal is: To draw a simple object in Processing according to my XCode project data.
I need to see value of my variable in the Processing.
int main(int argc, const char * argv[]) {
// insert code here...
for (int i=0; i<10; i++)
std::cout << "How to send value of i to the Processing!n";
return 0;
}
2
Answers
Finally I found the way. Hope it help someone. Share it.
Xcode app ->(127.0.0.1:UDP)-> Processing sketch
Source Links:
Sending string over UDP in C++
https://discourse.processing.org/t/receive-udp-packets/19832
Xcode app (C++):
Processing code:
The assumption is you’re using c++ in Xcode (and not Objective-C, nor Swift).
Every processing sketch inherits the
args
property (very similar to main’sconst char * argv[]
in c++ program). You can make use of that to initialise a Processing sketch with options from c++.You could have something like:
(This is oversimplified, you’d have your for loop accumulate ints into a string with a separator character, maybe setup variables for paths to
processing-java
and the processing sketch)To clarify,
processing-java
is a command line utility that ships with Processing. (You can find it in inside the Processing.app folder (via show contents), alongside the processing executable and install it via Tools menu inside Processing). It allows you to easily run a sketch from the command line. Alternatively, you can export an application, however if you’re prototyping, theprocessing-java
option might be more practical.In Processing you’d check if the sketch was launched with arguments, and if so, parse those arguments.
You can use
split()
to split0,1,2,3,4,5,6,7,8,9
into individual numbers that can be parsed (viaint()
for example).If you have more complex data, you can consider formatting your c++ output as JSON, then using
parseJSONObject()
/parseJSONArray()
.(If you don’t want to split individual values, you can just use spaces with command line arguments:
/path/to/processing-java --sketch-path=/path/to/your/processing/sketch/folder --run 0 1 2 3 4 5 6 7 8 9
. If you want to send a JSON formatted string from c++, be aware you may need to escape"
(e.g.system("/path/to/processing-java --sketch-path=/path/to/your/processing/sketch/folder --run {"myCppData":[0,1,2]}");
)This would work if you need to launch the processing sketch once and initialise with values from your c++ program at startup. Outside of the scope of your question, if you need to continously send values from c++ to Processing, you can look at opening a local socket connection (TCP or UDP) to estabish communication between the two programs. One easy to use protocol is OSC (via UDP). You can use oscpack in raw c++ and oscp5 in Processing. (Optionally, depending on your setup you can consider openFrameworks which (already has oscpack integrated as
ofxOsc
and ships with send/receive examples): itsofApp
is similar Processing’sPApplet
(e.g.setup()
/draw()
/mousePressed()
, etc.)