Skip to content

Instantly share code, notes, and snippets.

@jgranick
Created May 15, 2012 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgranick/2703949 to your computer and use it in GitHub Desktop.
Save jgranick/2703949 to your computer and use it in GitHub Desktop.
Initial working example of BuildHX C++ bindings
package box2d.common.math;
#if cpp
import cpp.Lib;
#elseif neko
import neko.Lib;
#end
class B2Vec2 {
public var x (getX, setX):Float;
public var y (getY, setY):Float;
/** @private */
public var handle:Dynamic;
public function new (x:Float, y:Float):Void {
handle = box2d_b2vec2_new (x, y);
}
public function isValid ():Bool {
return box2d_b2vec2_is_valid (handle);
}
public function length ():Float {
return box2d_b2vec2_length (handle);
}
public function lengthSquared ():Float {
return box2d_b2vec2_length_squared (handle);
}
public function normalize ():Float {
return box2d_b2vec2_normalize (handle);
}
public function set (x:Float, y:Float):Void {
box2d_b2vec2_set (handle, x, y);
}
public function setZero ():Void {
box2d_b2vec2_set_zero (handle);
}
// Get & Set Methods
private function getX ():Float {
return box2d_b2vec2_get_x (handle);
}
private function getY ():Float {
return box2d_b2vec2_get_y (handle);
}
private function setX (value:Float):Float {
box2d_b2vec2_set_x (handle, value);
return value;
}
private function setY (value:Float):Float {
box2d_b2vec2_set_y (handle, value);
return value;
}
// Native Methods
private static var box2d_b2vec2_get_x = Lib.load ("box2d", "box2d_b2vec2_get_x", 1);
private static var box2d_b2vec2_get_y = Lib.load ("box2d", "box2d_b2vec2_get_y", 1);
private static var box2d_b2vec2_is_valid = Lib.load ("box2d", "box2d_b2vec2_is_valid", 1);
private static var box2d_b2vec2_length = Lib.load ("box2d", "box2d_b2vec2_length", 1);
private static var box2d_b2vec2_length_squared = Lib.load ("box2d", "box2d_b2vec2_length_squared", 1);
private static var box2d_b2vec2_new = Lib.load ("box2d", "box2d_b2vec2_new", 2);
private static var box2d_b2vec2_normalize = Lib.load ("box2d", "box2d_b2vec2_normalize", 1);
private static var box2d_b2vec2_set = Lib.load ("box2d", "box2d_b2vec2_set", 3);
private static var box2d_b2vec2_set_x = Lib.load ("box2d", "box2d_b2vec2_set_x", 2);
private static var box2d_b2vec2_set_y = Lib.load ("box2d", "box2d_b2vec2_set_y", 2);
private static var box2d_b2vec2_set_zero = Lib.load ("box2d", "box2d_b2vec2_set_zero", 1);
}
package box2d.dynamics;
#if cpp
import cpp.Lib;
#elseif neko
import neko.Lib;
#end
import box2d.common.math.B2Vec2;
class B2World {
public var allowSleeping (getAllowSleeping, setAllowSleeping):Bool;
public var autoClearForces (getAutoClearForces, setAutoClearForces):Bool;
public var bodyCount (getBodyCount, null):Int;
public var contactCount (getContactCount, null):Int;
public var continuousPhysics (getContinuousPhysics, setContinuousPhysics):Bool;
public var isLocked (getIsLocked, null):Bool;
public var jointCount (getJointCount, null):Int;
public var proxyCount (getProxyCount, null):Int;
public var subStepping (getSubStepping, setSubStepping):Bool;
public var treeBalance (getTreeBalance, null):Int;
public var treeHeight (getTreeHeight, null):Int;
public var treeQuality (getTreeQuality, null):Float;
public var warmStarting (getWarmStarting, setWarmStarting):Bool;
/** @private */
public var handle:Dynamic;
public function new (gravity:B2Vec2):Void {
handle = box2d_b2world_new (gravity.handle);
}
public function clearForces ():Void {
box2d_b2world_clear_forces (handle);
}
public function drawDebugData ():Void {
box2d_b2world_draw_debug_data (handle);
}
public function dump ():Void {
box2d_b2world_dump (handle);
}
// Get & Set Methods
private function getAllowSleeping ():Bool {
return box2d_b2world_get_allow_sleeping (handle);
}
private function getAutoClearForces ():Bool {
return box2d_b2world_get_auto_clear_forces (handle);
}
private function getBodyCount ():Int {
return box2d_b2world_get_body_count (handle);
}
private function getContactCount ():Int {
return box2d_b2world_get_contact_count (handle);
}
private function getContinuousPhysics ():Bool {
return box2d_b2world_get_continuous_physics (handle);
}
private function getIsLocked ():Bool {
return box2d_b2world_get_is_locked (handle);
}
private function getJointCount ():Int {
return box2d_b2world_get_joint_count (handle);
}
private function getProxyCount ():Int {
return box2d_b2world_get_proxy_count (handle);
}
private function getSubStepping ():Bool {
return box2d_b2world_get_sub_stepping (handle);
}
private function getTreeBalance ():Int {
return box2d_b2world_get_tree_balance (handle);
}
private function getTreeHeight ():Int {
return box2d_b2world_get_tree_height (handle);
}
private function getTreeQuality ():Float {
return box2d_b2world_get_tree_quality (handle);
}
private function getWarmStarting ():Bool {
return box2d_b2world_get_warm_starting (handle);
}
private function setAllowSleeping (value:Bool):Bool {
box2d_b2world_set_allow_sleeping (handle, value);
return value;
}
private function setAutoClearForces (value:Bool):Bool {
box2d_b2world_set_auto_clear_forces (handle, value);
return value;
}
private function setContinuousPhysics (value:Bool):Bool {
box2d_b2world_set_continuous_physics (handle, value);
return value;
}
private function setSubStepping (value:Bool):Bool {
box2d_b2world_set_sub_stepping (handle, value);
return value;
}
private function setWarmStarting (value:Bool):Bool {
box2d_b2world_set_warm_starting (handle, value);
return value;
}
// Native Methods
private static var box2d_b2world_clear_forces = Lib.load ("box2d", "box2d_b2world_clear_forces", 1);
private static var box2d_b2world_draw_debug_data = Lib.load ("box2d", "box2d_b2world_draw_debug_data", 1);
private static var box2d_b2world_dump = Lib.load ("box2d", "box2d_b2world_dump", 1);
private static var box2d_b2world_get_allow_sleeping = Lib.load ("box2d", "box2d_b2world_get_allow_sleeping", 1);
private static var box2d_b2world_get_auto_clear_forces = Lib.load ("box2d", "box2d_b2world_get_auto_clear_forces", 1);
private static var box2d_b2world_get_body_count = Lib.load ("box2d", "box2d_b2world_get_body_count", 1);
private static var box2d_b2world_get_contact_count = Lib.load ("box2d", "box2d_b2world_get_contact_count", 1);
private static var box2d_b2world_get_continuous_physics = Lib.load ("box2d", "box2d_b2world_get_continuous_physics", 1);
private static var box2d_b2world_get_is_locked = Lib.load ("box2d", "box2d_b2world_get_is_locked", 1);
private static var box2d_b2world_get_joint_count = Lib.load ("box2d", "box2d_b2world_get_joint_count", 1);
private static var box2d_b2world_get_proxy_count = Lib.load ("box2d", "box2d_b2world_get_proxy_count", 1);
private static var box2d_b2world_get_sub_stepping = Lib.load ("box2d", "box2d_b2world_get_sub_stepping", 1);
private static var box2d_b2world_get_tree_balance = Lib.load ("box2d", "box2d_b2world_get_tree_balance", 1);
private static var box2d_b2world_get_tree_height = Lib.load ("box2d", "box2d_b2world_get_tree_height", 1);
private static var box2d_b2world_get_tree_quality = Lib.load ("box2d", "box2d_b2world_get_tree_quality", 1);
private static var box2d_b2world_get_warm_starting = Lib.load ("box2d", "box2d_b2world_get_warm_starting", 1);
private static var box2d_b2world_new = Lib.load ("box2d", "box2d_b2world_new", 1);
private static var box2d_b2world_set_allow_sleeping = Lib.load ("box2d", "box2d_b2world_set_allow_sleeping", 2);
private static var box2d_b2world_set_auto_clear_forces = Lib.load ("box2d", "box2d_b2world_set_auto_clear_forces", 2);
private static var box2d_b2world_set_continuous_physics = Lib.load ("box2d", "box2d_b2world_set_continuous_physics", 2);
private static var box2d_b2world_set_sub_stepping = Lib.load ("box2d", "box2d_b2world_set_sub_stepping", 2);
private static var box2d_b2world_set_warm_starting = Lib.load ("box2d", "box2d_b2world_set_warm_starting", 2);
}
#ifndef IPHONE
#define IMPLEMENT_API
#endif
#if defined(HX_WINDOWS) || defined(HX_MACOS) || defined(HX_LINUX)
#define NEKO_COMPATIBLE
#endif
#include <hx/CFFI.h>
#include <Box2D/Common/b2Math.h>
#include <Box2D/Dynamics/b2World.h>
value box2d_b2vec2_get_x (value handle) {
return alloc_float(((b2Vec2*)val_data(handle))->x);
}
DEFINE_PRIM (box2d_b2vec2_get_x, 1)
value box2d_b2vec2_get_y (value handle) {
return alloc_float(((b2Vec2*)val_data(handle))->y);
}
DEFINE_PRIM (box2d_b2vec2_get_y, 1)
value box2d_b2vec2_is_valid (value handle) {
return alloc_bool(((b2Vec2*)val_data(handle))->IsValid());
}
DEFINE_PRIM (box2d_b2vec2_is_valid, 1)
value box2d_b2vec2_length (value handle) {
return alloc_float(((b2Vec2*)val_data(handle))->Length());
}
DEFINE_PRIM (box2d_b2vec2_length, 1)
value box2d_b2vec2_length_squared (value handle) {
return alloc_float(((b2Vec2*)val_data(handle))->LengthSquared());
}
DEFINE_PRIM (box2d_b2vec2_length_squared, 1)
value box2d_b2vec2_new (value x, value y) {
b2Vec2 *instance = new b2Vec2 (val_float(x), val_float(y));
return alloc_abstract (0, instance);
}
DEFINE_PRIM (box2d_b2vec2_new, 2)
value box2d_b2vec2_normalize (value handle) {
return alloc_float(((b2Vec2*)val_data(handle))->Normalize());
}
DEFINE_PRIM (box2d_b2vec2_normalize, 1)
value box2d_b2world_get_allow_sleeping (value handle) {
return alloc_bool(((b2World*)val_data(handle))->GetAllowSleeping());
}
DEFINE_PRIM (box2d_b2world_get_allow_sleeping, 1)
value box2d_b2world_get_auto_clear_forces (value handle) {
return alloc_bool(((b2World*)val_data(handle))->GetAutoClearForces());
}
DEFINE_PRIM (box2d_b2world_get_auto_clear_forces, 1)
value box2d_b2world_get_body_count (value handle) {
return alloc_int(((b2World*)val_data(handle))->GetBodyCount());
}
DEFINE_PRIM (box2d_b2world_get_body_count, 1)
value box2d_b2world_get_contact_count (value handle) {
return alloc_int(((b2World*)val_data(handle))->GetContactCount());
}
DEFINE_PRIM (box2d_b2world_get_contact_count, 1)
value box2d_b2world_get_continuous_physics (value handle) {
return alloc_bool(((b2World*)val_data(handle))->GetContinuousPhysics());
}
DEFINE_PRIM (box2d_b2world_get_continuous_physics, 1)
value box2d_b2world_get_is_locked (value handle) {
return alloc_bool(((b2World*)val_data(handle))->IsLocked());
}
DEFINE_PRIM (box2d_b2world_get_is_locked, 1)
value box2d_b2world_get_joint_count (value handle) {
return alloc_int(((b2World*)val_data(handle))->GetJointCount());
}
DEFINE_PRIM (box2d_b2world_get_joint_count, 1)
value box2d_b2world_get_proxy_count (value handle) {
return alloc_int(((b2World*)val_data(handle))->GetProxyCount());
}
DEFINE_PRIM (box2d_b2world_get_proxy_count, 1)
value box2d_b2world_get_sub_stepping (value handle) {
return alloc_bool(((b2World*)val_data(handle))->GetSubStepping());
}
DEFINE_PRIM (box2d_b2world_get_sub_stepping, 1)
value box2d_b2world_get_tree_balance (value handle) {
return alloc_int(((b2World*)val_data(handle))->GetTreeBalance());
}
DEFINE_PRIM (box2d_b2world_get_tree_balance, 1)
value box2d_b2world_get_tree_height (value handle) {
return alloc_int(((b2World*)val_data(handle))->GetTreeHeight());
}
DEFINE_PRIM (box2d_b2world_get_tree_height, 1)
value box2d_b2world_get_tree_quality (value handle) {
return alloc_float(((b2World*)val_data(handle))->GetTreeQuality());
}
DEFINE_PRIM (box2d_b2world_get_tree_quality, 1)
value box2d_b2world_get_warm_starting (value handle) {
return alloc_bool(((b2World*)val_data(handle))->GetWarmStarting());
}
DEFINE_PRIM (box2d_b2world_get_warm_starting, 1)
value box2d_b2world_new (value gravity) {
b2World *instance = new b2World (*(b2Vec2*)val_data(gravity));
return alloc_abstract (0, instance);
}
DEFINE_PRIM (box2d_b2world_new, 1)
void box2d_b2vec2_set (value handle, value x, value y) {
((b2Vec2*)val_data(handle))->Set(val_float(x), val_float(y));
}
DEFINE_PRIM (box2d_b2vec2_set, 3)
void box2d_b2vec2_set_x (value handle, value newValue) {
((b2Vec2*)val_data(handle))->x = val_float(newValue);
}
DEFINE_PRIM (box2d_b2vec2_set_x, 2)
void box2d_b2vec2_set_y (value handle, value newValue) {
((b2Vec2*)val_data(handle))->y = val_float(newValue);
}
DEFINE_PRIM (box2d_b2vec2_set_y, 2)
void box2d_b2vec2_set_zero (value handle) {
((b2Vec2*)val_data(handle))->SetZero();
}
DEFINE_PRIM (box2d_b2vec2_set_zero, 1)
void box2d_b2world_clear_forces (value handle) {
((b2World*)val_data(handle))->ClearForces();
}
DEFINE_PRIM (box2d_b2world_clear_forces, 1)
void box2d_b2world_draw_debug_data (value handle) {
((b2World*)val_data(handle))->DrawDebugData();
}
DEFINE_PRIM (box2d_b2world_draw_debug_data, 1)
void box2d_b2world_dump (value handle) {
((b2World*)val_data(handle))->Dump();
}
DEFINE_PRIM (box2d_b2world_dump, 1)
void box2d_b2world_set_allow_sleeping (value handle, value newValue) {
((b2World*)val_data(handle))->SetAllowSleeping(val_bool(newValue));
}
DEFINE_PRIM (box2d_b2world_set_allow_sleeping, 2)
void box2d_b2world_set_auto_clear_forces (value handle, value newValue) {
((b2World*)val_data(handle))->SetAutoClearForces(val_bool(newValue));
}
DEFINE_PRIM (box2d_b2world_set_auto_clear_forces, 2)
void box2d_b2world_set_continuous_physics (value handle, value newValue) {
((b2World*)val_data(handle))->SetContinuousPhysics(val_bool(newValue));
}
DEFINE_PRIM (box2d_b2world_set_continuous_physics, 2)
void box2d_b2world_set_sub_stepping (value handle, value newValue) {
((b2World*)val_data(handle))->SetSubStepping(val_bool(newValue));
}
DEFINE_PRIM (box2d_b2world_set_sub_stepping, 2)
void box2d_b2world_set_warm_starting (value handle, value newValue) {
((b2World*)val_data(handle))->SetWarmStarting(val_bool(newValue));
}
DEFINE_PRIM (box2d_b2world_set_warm_starting, 2)
extern "C" int box2d_register_prims() { return 0; }
<?xml version="1.0" encoding="utf-8"?>
<buildhx>
<library name="box2d" type="cpp" />
<output path="" />
<!-- <type name="B2AABB" remap="" />
<type name="B2Body" remap="box2d.dynamics.B2Body" />
<type name="B2BodyDef" remap="" />
<type name="B2Contact" remap="box2d.dynamics.contacts.B2Contact" />
<type name="B2ContactFilter" remap="" />
<type name="B2ContactListener" remap="" />
<type name="B2ContactManager" remap="box2d.dynamics.B2ContactManager" />
<type name="B2DestructionListener" remap="" />
<type name="B2Draw" remap="" />
<type name="B2Joint" remap="" />
<type name="B2JointDef" remap="" />
<type name="B2Profile" remap="" />
<type name="B2QueryCallback" remap="" />
<type name="B2RayCastCallback" remap="" /> -->
<type name="B2Vec2" remap="box2d.common.math.B2Vec2" />
<type name="B2World" remap="box2d.dynamics.B2World" />
<class name="box2d.common.math.B2Vec2" native="b2Vec2" header="Box2D/Common/b2Math.h">
<property name="x" type="Float" />
<property name="y" type="Float" />
<constructor params="x:Float, y:Float" />
<method name="isValid" native="IsValid" return="Bool" />
<method name="length" native="Length" return="Float" />
<method name="lengthSquared" native="LengthSquared" return="Float" />
<method name="normalize" native="Normalize" return="Float" />
<method name="setZero" native="SetZero" />
<method name="set" native="Set" params="x:Float, y:Float" />
<!-- <method name="skew" native="Skew" return="B2Vec2" /> -->
</class>
<class name="box2d.dynamics.B2World" native="b2World" header="Box2D/Dynamics/b2World.h">
<property name="allowSleeping" getter="GetAllowSleeping" setter="SetAllowSleeping" type="Bool" />
<property name="autoClearForces" getter="GetAutoClearForces" setter="SetAutoClearForces" type="Bool" />
<property name="bodyCount" getter="GetBodyCount" type="Int" />
<!-- <property name="bodyList" getter="GetBodyList" type="Array <B2Body>" /> -->
<property name="contactCount" getter="GetContactCount" type="Int" />
<!-- <property name="contactList" getter="GetContactList" type="Array <B2Contact>" />
<property name="contactManager" getter="GetContactManager" type="B2ContactManager" /> -->
<property name="continuousPhysics" getter="GetContinuousPhysics" setter="SetContinuousPhysics" type="Bool" />
<!-- <property name="gravity" getter="GetGravity" setter="SetGravity" type="B2Vec2" /> -->
<property name="isLocked" getter="IsLocked" type="Bool" />
<property name="jointCount" getter="GetJointCount" type="Int" />
<!-- <property name="jointList" getter="GetJointList" type="Array <B2Joint>" />
<property name="profile" getter="GetProfile" type="B2Profile" /> -->
<property name="proxyCount" getter="GetProxyCount" type="Int" />
<property name="subStepping" getter="GetSubStepping" setter="SetSubStepping" type="Bool" />
<property name="treeBalance" getter="GetTreeBalance" type="Int" />
<property name="treeHeight" getter="GetTreeHeight" type="Int" />
<property name="treeQuality" getter="GetTreeQuality" type="Float" />
<property name="warmStarting" getter="GetWarmStarting" setter="SetWarmStarting" type="Bool" />
<constructor params="gravity:B2Vec2" />
<method name="clearForces" native="ClearForces" />
<!-- <method name="CreateBody" params="def:B2BodyDef" return="B2Body" />
<method name="CreateJoint" params="def:B2JointDef" return="B2Joint" />
<method name="DestroyBody" params="body:B2Body" />
<method name="DestroyJoint" params="joint:B2Joint" /> -->
<method name="drawDebugData" native="DrawDebugData" />
<method name="dump" native="Dump" />
<!-- <method name="QueryAABB" params="queryCallback:B2QueryCallback, aabb:B2AABB" />
<method name="RayCast" params="rayCastCallback:B2RayCastCallback, point1:B2Vec2, point2:B2Vec2" />
<method name="SetContactFilter" params="filter:B2ContactFilter" />
<method name="SetContactListener" params="listener:B2ContactListener" />
<method name="SetDebugDraw" params="debugDraw:B2Draw" />
<method name="SetDestructionListener" params="listener:B2DestructionListener" />
<method name="Step" params="timeStep:Float, velocityIterations:Int, positionIterations:Int" /> -->
</class>
</buildhx>
package com.joshuagranick.test;
import box2d.common.math.B2Vec2;
import box2d.dynamics.B2World;
import nme.display.Sprite;
/**
* ...
* @author Joshua Granick
*/
class Test extends Sprite {
public function new (){
super ();
var hi = new B2Vec2 (0, 0);
var world = new B2World (hi);
world.allowSleeping = true;
trace (world.allowSleeping);
world.allowSleeping = false;
trace (world.allowSleeping);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment