1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

use std::fmt::{Debug, Formatter};

use autocxx::prelude::*;

include_cpp! {
    #include "box2d/box2d.h"
    #include "extras.hpp"
    safety!(unsafe_ffi)

    generate!("b2Body")
    generate!("b2Contact")
    generate!("b2ContactFilter")
    generate!("b2DestructionListener")
    generate!("b2Filter")
    generate!("b2Fixture")
    generate!("b2Manifold")
    generate!("b2QueryCallback")
    generate!("b2World")
    generate!("b2WorldManifold")

    generate!("b2Shape")
    generate!("b2ChainShape")
    generate!("b2CircleShape")
    generate!("b2EdgeShape")
    generate!("b2PolygonShape")

    generate!("b2Joint")
    generate!("b2DistanceJoint")
    generate!("b2FrictionJoint")
    generate!("b2GearJoint")
    generate!("b2MotorJoint")
    generate!("b2PrismaticJoint")
    generate!("b2PulleyJoint")
    generate!("b2RevoluteJoint")
    generate!("b2WeldJoint")
    generate!("b2WheelJoint")
    generate!("b2JointUserData")
    generate!("b2JointDef")
    generate!("b2DistanceJointDef")
    generate!("b2FrictionJointDef")
    generate!("b2GearJointDef")
    generate!("b2MotorJointDef")
    generate!("b2PrismaticJointDef")
    generate!("b2PulleyJointDef")
    generate!("b2RevoluteJointDef")
    generate!("b2WeldJointDef")
    generate!("b2WheelJointDef")

    generate!("b2ParticleContact")
    generate!("b2ParticleGroup")
    generate!("b2ParticleHandle")
    generate!("b2ParticleSystem")
    generate_pod!("b2ParticleBodyContact")
    generate_pod!("b2ParticleDef")
    generate_pod!("b2ParticleFlag")
    generate_pod!("b2ParticleSystemDef")

    generate_pod!("b2AABB")
    generate_pod!("b2BodyDef")
    generate_pod!("b2FixtureDef")
    generate_pod!("b2MassData")
    generate_pod!("b2Transform")
    generate_pod!("b2Vec2")
    generate_pod!("b2Rot")

    // extras.hpp
    generate!("SetCircleRadius")
    generate!("SetCirclePosition")
    generate!("CreateParticleGroupDef")

    generate!("CreateRevoluteJoint")
    generate!("CreatePrismaticJoint")
    generate!("CreateDistanceJoint")
    generate!("CreatePulleyJoint")
    generate!("CreateMouseJoint")
    generate!("CreateGearJoint")
    generate!("CreateWheelJoint")
    generate!("CreateWeldJoint")
    generate!("CreateFrictionJoint")
    generate!("CreateMotorJoint")
}

pub mod box2d {
    pub mod ffi {
        pub use crate::ffi::*;
    }
}

impl PartialEq for ffi::b2Vec2 {
    fn eq(&self, other: &Self) -> bool {
        self.x == other.x && self.y == other.y
    }
}

impl Debug for ffi::b2Vec2 {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("b2Vec2").field("x", &self.x).field("y", &self.y).finish()
    }
}

#[cfg(test)]
mod tests {
    use std::pin::Pin;

    use crate::ffi::{b2BodyDef, b2CircleShape, b2Shape};
    use crate::ffi::b2BodyType::b2_dynamicBody;
    use crate::ffi::SetCircleRadius;

    use super::*;

    #[test]
    fn create_world() {
        moveit! { let gravity = ffi::b2Vec2::new1(0., -9.81); }
        let world = ffi::b2World::new(&gravity).within_box();

        let world_gravity = world.GetGravity();
        let expected_gravity = gravity.as_ref().get_ref();
        assert_eq!(*expected_gravity, world_gravity);
    }

    #[test]
    fn gravity_affects_position() {
        unsafe {
            let gravity = ffi::b2Vec2::new1(0., -10.).within_box();
            let mut world = ffi::b2World::new(&*gravity).within_box();
            let mut body_def = b2BodyDef::new().within_box();
            body_def.type_ = b2_dynamicBody;
            let body_def = &*body_def;
            let body = world.as_mut().CreateBody(&*body_def);
            let mut body = Pin::new_unchecked(body.as_mut().unwrap());

            let mut shape = b2CircleShape::new().within_box();
            SetCircleRadius(shape.as_mut(), 5.);
            let shape: &b2Shape = (&*shape).as_ref();
            body.as_mut().CreateFixture1(&*shape, 5.);

            for _ in 0..10 {
                world.as_mut().Step(
                    0.02,
                    c_int::from(8),
                    c_int::from(3),
                    c_int::from(100));
            }

            assert!(body.as_ref().GetPosition().y < 0., "Body needs to move downwards due to gravity");
        }
    }
}

impl Debug for ffi::b2Body {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("b2Body").field("position", &self.GetPosition()).field("angle", &self.GetAngle()).field("type", &(self.GetType() as u32)).field("mass", &self.GetMass()).finish()
    }
}