Permalink
Browse files

Separate makefile for emscripten; copy of box2d's hello world in Benc…

…h2d.cpp (#if 0'd out) to try and test why emscripten is totally broken.
  • Loading branch information...
1 parent e8ab2f0 commit 0686f4c43899176a91a87e37e2dcda181ec2936e @joelgwebber committed Dec 12, 2011
Showing with 197 additions and 5 deletions.
  1. +4 −1 c/.gitignore
  2. +96 −1 c/Bench2d.cpp
  3. +93 −0 c/bench2d.js.mk
  4. +3 −2 c/bench2d_emscripten.html
  5. +1 −1 c/{bench2d.html → bench2d_nacl.html}
View
@@ -4,4 +4,7 @@ dbg_x86_64
opt_x86_32
opt_x86_64
build
-out
+bench2d
+bench2d.js
+*.bc
+*.o
View
@@ -5,15 +5,19 @@
using namespace std;
+#if 1
+#define WARMUP 0
#define FRAMES 256
-const int e_count = 40;
+
+const int e_count = 2;
void bench() {
// Define the gravity vector.
b2Vec2 gravity(0.0f, -10.0f);
// Construct a world object, which will hold and simulate the rigid bodies.
b2World world(gravity);
+ world.SetAllowSleeping(false);
{
b2BodyDef bd;
@@ -24,6 +28,8 @@ void bench() {
ground->CreateFixture(&shape, 0.0f);
}
+b2Body* topBody;
+
{
float32 a = 0.5f;
b2PolygonShape shape;
@@ -44,19 +50,26 @@ void bench() {
b2Body* body = world.CreateBody(&bd);
body->CreateFixture(&shape, 5.0f);
+topBody = body;
+
y += deltaY;
}
x += deltaX;
}
}
+ for (int32 i = 0; i < WARMUP; ++i) {
+ world.Step(1.0f/60.0f, 3, 3);
+ }
+
clock_t times[FRAMES];
for (int32 i = 0; i < FRAMES; ++i) {
clock_t start = clock();
world.Step(1.0f/60.0f, 3, 3);
clock_t end = clock();
times[i] = end - start;
+printf("%f :: ", topBody->GetPosition().y);
printf("%f\n", (float32)(end - start) / CLOCKS_PER_SEC * 1000);
}
@@ -74,3 +87,85 @@ int main(int argc, char** argv) {
return 0;
}
+#else
+
+int main(int argc, char** argv)
+{
+ B2_NOT_USED(argc);
+ B2_NOT_USED(argv);
+
+ // Define the gravity vector.
+ b2Vec2 gravity(0.0f, -10.0f);
+
+ // Construct a world object, which will hold and simulate the rigid bodies.
+ b2World world(gravity);
+
+ // Define the ground body.
+ b2BodyDef groundBodyDef;
+ groundBodyDef.position.Set(0.0f, -10.0f);
+
+ // Call the body factory which allocates memory for the ground body
+ // from a pool and creates the ground box shape (also from a pool).
+ // The body is also added to the world.
+ b2Body* groundBody = world.CreateBody(&groundBodyDef);
+
+ // Define the ground box shape.
+ b2PolygonShape groundBox;
+
+ // The extents are the half-widths of the box.
+ groundBox.SetAsBox(50.0f, 10.0f);
+
+ // Add the ground fixture to the ground body.
+ groundBody->CreateFixture(&groundBox, 0.0f);
+
+ // Define the dynamic body. We set its position and call the body factory.
+ b2BodyDef bodyDef;
+ bodyDef.type = b2_dynamicBody;
+ bodyDef.position.Set(0.0f, 4.0f);
+ b2Body* body = world.CreateBody(&bodyDef);
+
+ // Define another box shape for our dynamic body.
+ b2PolygonShape dynamicBox;
+ dynamicBox.SetAsBox(1.0f, 1.0f);
+
+ // Define the dynamic body fixture.
+ b2FixtureDef fixtureDef;
+ fixtureDef.shape = &dynamicBox;
+
+ // Set the box density to be non-zero, so it will be dynamic.
+ fixtureDef.density = 1.0f;
+
+ // Override the default friction.
+ fixtureDef.friction = 0.3f;
+
+ // Add the shape to the body.
+ body->CreateFixture(&fixtureDef);
+
+ // Prepare for simulation. Typically we use a time step of 1/60 of a
+ // second (60Hz) and 10 iterations. This provides a high quality simulation
+ // in most game scenarios.
+ float32 timeStep = 1.0f / 60.0f;
+ int32 velocityIterations = 6;
+ int32 positionIterations = 2;
+
+ // This is our little game loop.
+ for (int32 i = 0; i < 60; ++i)
+ {
+ // Instruct the world to perform a single step of simulation.
+ // It is generally best to keep the time step and iterations fixed.
+ world.Step(timeStep, velocityIterations, positionIterations);
+
+ // Now print the position and angle of the body.
+ b2Vec2 position = body->GetPosition();
+ float32 angle = body->GetAngle();
+
+ printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);
+ }
+
+ // When the world destructor is called, all bodies and joints are freed. This can
+ // create orphaned pointers, so be careful about your world management.
+
+ return 0;
+}
+#endif
+
View
@@ -0,0 +1,93 @@
+EMMAKEN=/Users/jgw/src/emscripten/tools/emmaken.py -IBox2D_v2.2.1
+EMSCRIPTEN=/Users/jgw/src/emscripten/emscripten.py
+LLVM_LINK=/Users/jgw/llvm/bin/llvm-link
+LLVM_OPT=/Users/jgw/llvm/bin/opt -stats -std-compile-opts
+
+# This set of flags has been chosen by the "cargo cult" method -- I picked them
+# mostly from the ammo.js build file, and tweaked them until I got the best
+# results. It's entirely possible I've missed something important here.
+EMSCRIPTEN_FLAGS = \
+--optimize \
+-s USE_TYPED_ARRAYS=1 \
+-s RELOP=1 \
+-s SAFE_HEAP=0 \
+-s ASSERTIONS=0 \
+-s QUANTUM_SIZE=1 \
+-s SKIP_STACK_IN_SMALL=1 \
+-s INIT_STACK=0 \
+-s PGO=0 \
+-s CHECK_OVERFLOWS=0 \
+-s CHECK_SIGNED_OVERFLOWS=0 \
+-s CORRECT_OVERFLOWS=0 \
+-s CHCK_SIGNS=0 \
+-s CORRECT_SIGNS=0 \
+-s DISABLE_EXCEPTION_CATCHING=1 \
+-s RUNTYPE_TYPE_INFO=0 \
+-s CORRECT_ROUNDINGS=0 \
+-s MICRO_OPTS=0
+
+OBJECTS = \
+Bench2d.bc \
+Box2D_v2.2.1/Box2d/Collision/b2BroadPhase.bc \
+Box2D_v2.2.1/Box2d/Collision/b2CollideCircle.bc \
+Box2D_v2.2.1/Box2d/Collision/b2CollideEdge.bc \
+Box2D_v2.2.1/Box2d/Collision/b2CollidePolygon.bc \
+Box2D_v2.2.1/Box2d/Collision/b2Collision.bc \
+Box2D_v2.2.1/Box2d/Collision/b2Distance.bc \
+Box2D_v2.2.1/Box2d/Collision/b2DynamicTree.bc \
+Box2D_v2.2.1/Box2d/Collision/b2TimeOfImpact.bc \
+Box2D_v2.2.1/Box2d/Collision/Shapes/b2ChainShape.bc \
+Box2D_v2.2.1/Box2d/Collision/Shapes/b2CircleShape.bc \
+Box2D_v2.2.1/Box2d/Collision/Shapes/b2EdgeShape.bc \
+Box2D_v2.2.1/Box2d/Collision/Shapes/b2PolygonShape.bc \
+Box2D_v2.2.1/Box2d/Common/b2BlockAllocator.bc \
+Box2D_v2.2.1/Box2d/Common/b2Draw.bc \
+Box2D_v2.2.1/Box2d/Common/b2Math.bc \
+Box2D_v2.2.1/Box2d/Common/b2Settings.bc \
+Box2D_v2.2.1/Box2d/Common/b2StackAllocator.bc \
+Box2D_v2.2.1/Box2d/Common/b2Timer.bc \
+Box2D_v2.2.1/Box2d/Dynamics/b2Body.bc \
+Box2D_v2.2.1/Box2d/Dynamics/b2ContactManager.bc \
+Box2D_v2.2.1/Box2d/Dynamics/b2Fixture.bc \
+Box2D_v2.2.1/Box2d/Dynamics/b2Island.bc \
+Box2D_v2.2.1/Box2d/Dynamics/b2World.bc \
+Box2D_v2.2.1/Box2d/Dynamics/b2WorldCallbacks.bc \
+Box2D_v2.2.1/Box2d/Dynamics/Contacts/b2ChainAndCircleContact.bc \
+Box2D_v2.2.1/Box2d/Dynamics/Contacts/b2ChainAndPolygonContact.bc \
+Box2D_v2.2.1/Box2d/Dynamics/Contacts/b2CircleContact.bc \
+Box2D_v2.2.1/Box2d/Dynamics/Contacts/b2Contact.bc \
+Box2D_v2.2.1/Box2d/Dynamics/Contacts/b2ContactSolver.bc \
+Box2D_v2.2.1/Box2d/Dynamics/Contacts/b2EdgeAndCircleContact.bc \
+Box2D_v2.2.1/Box2d/Dynamics/Contacts/b2EdgeAndPolygonContact.bc \
+Box2D_v2.2.1/Box2d/Dynamics/Contacts/b2PolygonAndCircleContact.bc \
+Box2D_v2.2.1/Box2d/Dynamics/Contacts/b2PolygonContact.bc \
+Box2D_v2.2.1/Box2d/Dynamics/Joints/b2DistanceJoint.bc \
+Box2D_v2.2.1/Box2d/Dynamics/Joints/b2FrictionJoint.bc \
+Box2D_v2.2.1/Box2d/Dynamics/Joints/b2GearJoint.bc \
+Box2D_v2.2.1/Box2d/Dynamics/Joints/b2Joint.bc \
+Box2D_v2.2.1/Box2d/Dynamics/Joints/b2MouseJoint.bc \
+Box2D_v2.2.1/Box2d/Dynamics/Joints/b2PrismaticJoint.bc \
+Box2D_v2.2.1/Box2d/Dynamics/Joints/b2PulleyJoint.bc \
+Box2D_v2.2.1/Box2d/Dynamics/Joints/b2RevoluteJoint.bc \
+Box2D_v2.2.1/Box2d/Dynamics/Joints/b2RopeJoint.bc \
+Box2D_v2.2.1/Box2d/Dynamics/Joints/b2WeldJoint.bc \
+Box2D_v2.2.1/Box2d/Dynamics/Joints/b2WheelJoint.bc \
+Box2D_v2.2.1/Box2d/Rope/b2Rope.bc
+
+all: bench2d.js
+
+%.bc: %.cpp
+ python $(EMMAKEN) $< -o $@
+
+bench2d.out.bc: $(OBJECTS)
+ $(LLVM_LINK) -o $@ $(OBJECTS)
+
+bench2d.opt.bc: bench2d.out.bc
+ $(LLVM_OPT) $< -o $@
+
+bench2d.js: bench2d.opt.bc
+ $(EMSCRIPTEN) $(EMSCRIPTEN_FLAGS) $< > $@
+
+clean:
+ rm bench2d.js bench2d.out.bc bench2d.opt.bc $(OBJECTS)
+
@@ -1,10 +1,11 @@
<!DOCTYPE html>
<html>
<head>
- <title>Bench2d, emscripten edition!</title>
+ <title>Bench2d, Emscripten Edition!</title>
+</head>
<body>
- <script src='out/bench2d.js'></script>
+ <script src='bench2d.js'></script>
</body>
</html>
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
- <title>Bench2d!</title>
+ <title>Bench2d, NaCl Edition!</title>
<script type="text/javascript">
Bench2dModule = null; // Global application object.

0 comments on commit 0686f4c

Please sign in to comment.