Emscripten绑定:如何创建一个可访问ç从Javascript / C ++数组? [英] Emscripten Bindings: How to create an accessible C/C++ array from Javascript?
问题描述
我使用 Box2D的,并试图创建一个链条形状。
I am using box2d and attempting to create a chain shape.
为了创建一个链形或多边形形状我必须通过载体的阵列以指定的几何形状。
In order to create a chain shape or polygon shape I must pass an array of vectors in order to specify the geometry.
我看不出任何文件来帮助我实现这个目标,以及有关绑定音符的这里不要进入有关数组的任何细节。
I do not see any documentation to help me accomplish this, and the notes about bindings here don't go into any detail about arrays.
我怎样才能构建一个数组?
How can I construct an array?
推荐答案
我以这些(尚未无证)emscripten功能解决了这个问题。
I have solved this problem by using these (as yet undocumented) emscripten features.
请注意,我访问的功能和价值(如 ALLOC_STACK
和 wrapPointer
)从Box2D的对象只是因为这是我发现他们被曝光的范围。他们emscripten特定值,所以对于不同的项目和/或构建设置会有所不同。
Note that I am accessing the functions and values (like ALLOC_STACK
and wrapPointer
) out of the Box2D object simply because that is the scope where I have found them to be exposed. They are emscripten-specific values, so for a different project and/or build settings it would be different.
// an alternative method that may work (shorter, less obvious code) is
// allocate([v1x,0,0,0,v1y,0,0,0,v2x,0,0,0,...], 'float', Box2D.ALLOC_STACK);
// 8 byte per vector * 4 vectors = 32 bytes of memory required
var buffer = Box2D.allocate(32, 'float', Box2D.ALLOC_STACK);
Box2D.setValue(buffer, left, 'float'); // v1x
Box2D.setValue(buffer+4, bottom, 'float'); // v1y
Box2D.setValue(buffer+8, right, 'float'); // v2x
Box2D.setValue(buffer+12, bottom, 'float'); // v2y
Box2D.setValue(buffer+16, right, 'float'); // v3x
Box2D.setValue(buffer+20, top, 'float'); // v3y
Box2D.setValue(buffer+24, left, 'float'); // v4x
Box2D.setValue(buffer+28, top, 'float'); // v4y
var ptr_wrapped = Box2D.wrapPointer(buffer, Box2D.b2Vec2);
shape.CreateLoop(ptr_wrapped, 4);
body.CreateFixture(shape,0.0);
这篇关于Emscripten绑定:如何创建一个可访问ç从Javascript / C ++数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!