将C ++虚拟方法绑定到具有覆盖function的js
假设我有一个C ++类:
class cRenderer { ... virtual void draw(); ... }
这是渲染引擎的一部分,所以任何inheritancecRenderer
C ++类cRenderer
可以重写draw
方法,并在屏幕上绘制东西。
问题是我如何实现与v8绑定相同的行为(这将最终是一个Node.js模块)?
有人可能会说,我可以这样做:
class cApplication : public cRenderer, public node::ObjectWrap { ... define bindings ... }
然后在我的JavaScript我做了这样的事情:
var app = new cApplication(); var oldDraw = app.draw; //saving old instance of draw app.draw = function() { ... do drawing api calls ... oldDraw(); //calling old draw }
这是行不通的,因为所有绘图API调用都是在draw
之外发生的。
更具体地说,JS绑定怎么能在draw
方法内插入dynamicAPI调用? 如何解决这个问题? 请记住,编辑渲染引擎的架构不是一个选项。
编辑:
我的意思是:
drawCircle(100, 100, 11); //outside of render loop draw(); //nothing happens at this point.
但是如果你在抽奖的方式里面有你的电话:
void draw() { //inside of render loop drawCircle(100, 100, 11); //it draws a circle at (100, 100) with radius 11 }
我的Sciter引擎使用所谓的即时绘图模型。 我高度怀疑你正在尝试在这里做同样的事情。
即时的绘图模式:
Sciter中的任何DOM元素可能都附有“绘制处理程序”:
var someEl = ... someEl.paintBackground = function(gfx) {... } someEl.paintContent = function(gfx) {... } ...
当元素需要绘制时,这些画家将由本地代码调用。 为了以防万一,在HTML / CSS元素绘图被分割在多个层:背景,内容,前景/大纲。
下面是如何实现本地draw_background的例子:
class Element { void draw_background(graphics* gfx) { if( has_paintBackground() ) { if(invoke(paintBackground, object_wrap(gfx)) == TRUE_VALUE) return; // default background drawing was suppressed by script } default_draw_background(gfx); } }
在这种情况下,脚本可能会在元素的背景层下面绘制一些东西,甚至是完全代替默认的渲染器。
更新:
由object_wrap(gfx)生成的脚本对象仅在渲染调用中连接到实际渲染表面。 任何在其他地方存储和使用gfx对象的尝试都会抛出一个错误:“无效的渲染表面”。
我不确定,但怀疑你需要这样的东西。
- 如何在Windows上安装cloud9
- 检查一个模块是否存在,如果是这样,用它replace控制台,否则不。 不工作,控制台得到未定义
- Grunt不在父目录中查找node_modules? 像节点呢
- React Native无法parsing模块../when
- 当我改变git分支时切换node_modules文件夹
- 在AngularJS应用程序中使用node_modules
- 使用connect-multiparty为上传的文件返回空对象
- Node.js Express App – Rickshaw.Graph.RangeSlider TypeError:$(element).slider不是函数
- nodejs如何将node_modules目录提供给前端进行访问?