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

mhe*_*ers 1 c++ loading openframeworks

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

cou*_*nt0 5

如果您有办法从客户端(通过退出按钮)终止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;
};
Run Code Online (Sandbox Code Playgroud)

然后,将其公开为python模块

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

在交互式python会话中使用它看起来像这样:

>>> import hello
>>> planet = hello.World()
>>> planet.set('howdy')
>>> planet.greet()
'howdy'
Run Code Online (Sandbox Code Playgroud)

现在,由于可以包装任何类或方法,因此有很多可能性来实际使用OF.一个我指的是在这个答案将有FE两个应用程序,App1,App2在C++实现的/,则链接到该蟒蛇实施.

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

>>> 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()
Run Code Online (Sandbox Code Playgroud)

我不是一个OF黑客,但另一个(更容易)的可能性可能是交互式地改变ofApp::draw()应用程序中的fe内容(在线程中运行).这可以通过在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...
}
Run Code Online (Sandbox Code Playgroud)

一旦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);
Run Code Online (Sandbox Code Playgroud)