c++/开放框架-如何在开放框架程序之间切换
c++ / openframeworks - how to switch between open frameworks programs
我正在为一位需要通过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有很多可能性。我在这个答案中提到的是让两个应用程序App1
、App2
在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);
相关文章:
- Qt安装程序框架:检测是否安装了VC++可再发行组件
- 构建一个真正、完全静态的Qt 5框架和应用程序(Qt 5.8之后)
- 如何使用Boost单元测试框架测试Qt应用程序
- 使用没有堆内存分配的接口的框架应用程序?
- 使用C 单元测试框架集成测试Java应用程序
- 使用QT安装程序框架创建.msi安装程序
- 编译 Qt 安装程序框架静态
- QT安装程序框架:更新时奇怪的许可证错误
- Windows生物识别框架样本UMDF驱动程序:此设备无法启动.(代码10)
- 如何翻译使用预构建的QT安装程序框架创建的安装程序,无论OS语言如何
- Qt 安装程序框架 mac 桌面别名
- 禁用QT安装程序框架中的本机下一个按钮
- 在没有.NET框架的情况下运行.NET EXE应用程序
- 使用Qt安装程序框架中的archivegen工具将文件打包为7zip存档
- 将现有的C++框架链接到Objective-C + Cocoa应用程序
- C++大规模程序/框架构建
- 带有OpenGL和嵌入式WebViews的本机移动应用程序(iOS/Android)的测试框架
- Qt框架 - 我可以(合法地)使用qmake而不是Qt创建商业应用程序吗?
- C++应用程序的框架
- c++/开放框架-如何在开放框架程序之间切换