c++/开放框架-如何在开放框架程序之间切换

c++ / openframeworks - how to switch between open frameworks programs

本文关键字:框架 程序 之间 c++      更新时间:2023-10-16

我正在为一位需要通过openFrameworks运行各种程序的背景的行为艺术家开发一个程序。他需要一种能够在它们之间轻松切换的方法。有没有办法创建一个主shell来加载或卸载其他openframework文件?

如果你有办法从客户端终止RunApp()(通过退出按钮),你可以通过tcl或python用脚本语言包装调用。你最终会得到一个交互式shell,在那里你可以运行不同的应用程序并设置参数。

为了简单起见,我将省略一些细节,并假设我们使用boost::python进行语言绑定。本文对此进行了更详细的解读,boost::python文档在这里。

其主要思想是为of创建一个特定于域的语言/包装器集,可用于创建of对象并通过shell交互访问它们的方法或通过脚本交互访问它们。

Boost对象绑定的工作原理大致如下(引用自1):

首先在C++中定义类

struct World
{
void set(std::string msg) { this->msg = msg; }
std::string greet() { return msg; }
std::string msg;
};

然后,将其公开为python模块

#include <boost/python.hpp>
BOOST_PYTHON_MODULE(hello)
{
class_<World>("World")
.def("greet", &World::greet)
.def("set", &World::set)
;
}

在交互式python会话中使用的是这样的:

>>> import hello
>>> planet = hello.World()
>>> planet.set('howdy')
>>> planet.greet()
'howdy'

现在,既然可以包装任何类或方法,那么如何真正利用OF有很多可能性。我在这个答案中提到的是让两个应用程序App1App2在C++/OF中实现,然后链接到python中的实现。

交互式会话看起来像这样:

>>> import myofapps
>>> a1 = myofapps.App1()
>>> a2 = myofapps.App2()
>>> a1.run() # blocked here, until the app terminates
>>> a2.run() # then start next app .. and so forth
>>> a1.run()

我不是OF黑客,但另一种(更容易的)可能性可能是在应用程序中交互更改f.e.ofApp::draw()的内容(在线程中运行)。这可以通过在python解释器中嵌入一个可参数化的自定义对象来实现:

/// custom configurator class
class MyObj {
private int colorRed;
// getter
int getRed () {
return colorRed;
}
// setter
void setRed(int r) {
colorRed = r;
}
/// more getters/setters code
...
};
/// the boost wrapping code (see top of post)
...
/// OF code here
void testApp::draw() {
// grab a reference to MyObj (there are multiple ways to do that)
// let's assume there's a singleton which holds the reference to it
MyObj o = singleton.getMyObj();
// grab values
int red = o.getRed ();
// configure color
ofSetColor(red,0,0,100);
// other OF drawing code here...
}

一旦OF应用程序运行,就可以在您的解释器中交互更改颜色:

>>> import myofapps
>>> a1 = myofapps.App1()
>>> c1 = myofapps.MyObj();
>>> a1.run() # this call would have to be made non-blocking by running the 
>>>          # app in a thread and returning right away
>>> c1.setRed(100);
... after a minute set color to a different value
>>>> c1.setRed(200);