Skip to content

Commit 2fd5690

Browse files
committed
box2d-native-1 C++ methods with abstract class return type returns to JS object of abstract class
created swig typemap for b2Shape *, made corresponding changes in demo made demo an independent package
1 parent 7146762 commit 2fd5690

File tree

6 files changed

+71
-35
lines changed

6 files changed

+71
-35
lines changed

.travis.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ env:
33
- CXX=g++-4.9
44
language: node_js
55
node_js:
6-
- "4.2.1"
6+
- 4.2
7+
- stable
78
addons:
89
apt:
910
sources:

Box2D.i

+38
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,44 @@
99
%rename(subtract) operator-=;
1010
%rename(multiply) operator*=;
1111

12+
%typemap(out) b2Shape* {
13+
swig_type_info *info = SWIGTYPE_p_b2CircleShape;
14+
b2Shape *shape = 0;
15+
shape = $1;
16+
if (shape) {
17+
switch (shape->GetType())
18+
{
19+
case b2Shape::e_circle:
20+
{
21+
info = SWIGTYPE_p_b2CircleShape;
22+
}
23+
break;
24+
25+
case b2Shape::e_edge:
26+
{
27+
info = SWIGTYPE_p_b2EdgeShape;
28+
}
29+
break;
30+
31+
case b2Shape::e_chain:
32+
{
33+
info = SWIGTYPE_p_b2ChainShape;
34+
}
35+
break;
36+
37+
case b2Shape::e_polygon:
38+
{
39+
info = SWIGTYPE_p_b2PolygonShape;
40+
}
41+
break;
42+
43+
default:
44+
break;
45+
}
46+
}
47+
$result = SWIG_NewPointerObj(SWIG_as_voidptr($1), info, 0 | 0 );
48+
}
49+
1250
%{
1351
#include "Box2D/Box2D.h"
1452
%}

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ npm install node-gyp -g
1818
npm install box2d-native
1919
```
2020

21-
**IMPORTANT:** Installation tested only in Ubuntu 15.04 with Node.js v4.2.1.
21+
**IMPORTANT:** Installation tested only on Linux with Node.js >= 4.2.1.
2222

2323
## Usage
2424

@@ -35,8 +35,12 @@ Original "namespaced" Box2D classes (`b2Vec2`, `b2World`...) are also exposed.
3535

3636
## Demo
3737

38+
**IMPORTANT:** Runs only with Node.js v4.2.1
39+
3840
```
39-
npm run demo
41+
cd demo
42+
npm install
43+
npm start
4044
```
4145

4246

demo/demo.js

+12-27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
let Box2D = require('../');
2+
let Box2D = require('box2d-native');
33
let World = Box2D.World;
44
let Vec2 = Box2D.Vec2;
55
let b2Vec2 = Box2D.b2Vec2;
@@ -9,10 +9,7 @@ let b2Mul = Box2D.b2Mul;
99
let b2Assert = Box2D.b2Assert;
1010
let b2_maxPolygonVertices = Box2D.b2_maxPolygonVertices;
1111
let b2Color = Box2D.b2Color;
12-
let _clientDebugDraw;
1312
let world = new World(new Vec2(0, -9.8));
14-
let _ = require('../node_modules/lodash');
15-
let util = require('util');
1613
let step;
1714

1815
var bd_ground = new Box2D.b2BodyDef();
@@ -32,42 +29,31 @@ let fixture = ground.CreateFixture(shape, 0.0);
3229
let circleFixture = circle.CreateFixture(circleShape, 0.0);
3330
circleFixture.SetRestitution(0.5);
3431

35-
fixture._shape = shape;
36-
circleFixture._shape = circleShape;
37-
38-
ground._fixtures = [fixture];
39-
circle._fixtures = [circleFixture];
40-
41-
world._bodies = [ground, circle];
4232
function DrawShape(fixture, xf, color, g_debugDraw) {
43-
33+
let shape = fixture.GetShape();
4434
switch (fixture.GetType())
4535
{
4636
case b2Shape.e_circle:
4737
{
48-
let circle = fixture._shape;
49-
50-
let center = b2Mul(xf, circle.m_p);
51-
let radius = circle.m_radius;
38+
let center = b2Mul(xf, shape.m_p);
39+
let radius = shape.m_radius;
5240
let axis = b2Mul(xf.q, new b2Vec2(1.0, 0.0));
5341
g_debugDraw.DrawSolidCircle(center, radius, axis, color);
5442
}
5543
break;
5644

5745
case b2Shape.e_edge:
5846
{
59-
let edge = fixture._shape;
60-
let v1 = b2Mul(xf, edge.m_vertex1);
61-
let v2 = b2Mul(xf, edge.m_vertex2);
47+
let v1 = b2Mul(xf, shape.m_vertex1);
48+
let v2 = b2Mul(xf, shape.m_vertex2);
6249
g_debugDraw.DrawSegment(v1, v2, color);
6350
}
6451
break;
6552

6653
case b2Shape.e_chain:
6754
{
68-
let chain = fixture.GetShape();
69-
let count = chain.m_count;
70-
const vertices = chain.m_vertices;
55+
let count = shape.m_count;
56+
const vertices = shape.m_vertices;
7157

7258
let v1 = b2Mul(xf, vertices[0]);
7359
for (let i = 1; i < count; ++i)
@@ -82,14 +68,13 @@ function DrawShape(fixture, xf, color, g_debugDraw) {
8268

8369
case b2Shape.e_polygon:
8470
{
85-
let poly = fixture.GetShape();
86-
let vertexCount = poly.m_count;
71+
let vertexCount = shape.m_count;
8772
b2Assert(vertexCount <= b2_maxPolygonVertices);
8873
let vertices = new Array(b2_maxPolygonVertices);
8974

9075
for (let i = 0; i < vertexCount; ++i)
9176
{
92-
vertices[i] = b2Mul(xf, poly.m_vertices[i]);
77+
vertices[i] = b2Mul(xf, shape.m_vertices[i]);
9378
}
9479

9580
g_debugDraw.DrawSolidPolygon(vertices, vertexCount, color);
@@ -109,9 +94,9 @@ world.DrawDebugData = function () {
10994
let flags = this._debugDraw.GetFlags();
11095

11196
if (flags & Box2D.b2Draw.e_shapeBit) {
112-
for (let b, j = 0; b = this._bodies[j]; j++) {
97+
for (let b = this.GetBodyList(); b; b = b.GetNext()) {
11398
let xf = b.GetTransform();
114-
for (let i = 0, f; f = b._fixtures[i]; i++) {
99+
for (let f = b.GetFixtureList(); f; f = f.GetNext()) {
115100
if (b.IsActive() == false)
116101
{
117102
DrawShape(f, xf, b2Color(0.5, 0.5, 0.3), this._debugDraw);

demo/package.json

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
{
22
"name": "box2d-native-demo",
3-
"main": "index.js"
4-
}
3+
"main": "index.js",
4+
"engines": {
5+
"node": "4.2.1"
6+
},
7+
"dependencies": {
8+
"box2d-native": "file:..",
9+
"electron-prebuilt": "^0.34.2"
10+
},
11+
"scripts": {
12+
"start": "node node_modules/.bin/electron ."
13+
}
14+
}

package.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@
3737
"preinstall": "npm run clean && npm run get && npm run untar && npm run patch && npm run make && npm run wrap",
3838
"install": "node-gyp configure && node-gyp build",
3939
"postinstall": "npm run clean && node node_modules/.bin/babel Box2D.es6 --out-file Box2D.js --source-maps --whitelist strict,es6.modules,es6.parameters,es6.spread,es6.destructuring",
40-
"test": "node node_modules/.bin/mocha -r tests/bootstrap.js tests/**/*.spec.js",
41-
"demo": "node node_modules/.bin/electron demo/"
40+
"test": "node node_modules/.bin/mocha -r tests/bootstrap.js tests/**/*.spec.js"
4241
},
4342
"contributors": [
4443
"Leonid Kuzmin <[email protected]> (https://github.com/zuker)"
@@ -50,7 +49,6 @@
5049
"lodash": "^3.10.1"
5150
},
5251
"devDependencies": {
53-
"electron-prebuilt": "^0.34.2",
5452
"mocha": "^2.3.3",
5553
"should": "^7.1.1"
5654
}

0 commit comments

Comments
 (0)