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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
pub mod math;
pub mod settings;

use std::mem;
use common::math::{Vec2, Transform};

#[repr(C)]
#[derive(Clone, PartialEq, Debug)]
pub struct Color {
    pub r: f32,
    pub g: f32,
    pub b: f32,
    pub a: f32,
}

impl Color {
    pub fn as_array(&self) -> &[f32; 4] {
        unsafe { mem::transmute(self) }
    }

    pub fn as_array_mut(&mut self) -> &mut [f32; 4] {
        unsafe { mem::transmute(self) }
    }

    pub fn from_array_ref(array: &[f32; 4]) -> &Vec2 {
        unsafe { mem::transmute(array) }
    }

    pub fn from_array_mut(array: &mut [f32; 4]) -> &mut Vec2 {
        unsafe { mem::transmute(array) }
    }
}

bitflags! {
    #[repr(C)]
    pub struct DrawFlags: u32 {
        const DRAW_SHAPE = 0x0001;
        const DRAW_JOINT = 0x0002;
        const DRAW_AABB = 0x0004;
        const DRAW_PAIR = 0x0008;
        const DRAW_CENTER_OF_MASS = 0x0010;
    }
}

pub trait Draw {
    fn draw_polygon(&mut self, vertices: &[Vec2], color: &Color);
    fn draw_solid_polygon(&mut self, vertices: &[Vec2], color: &Color);
    fn draw_circle(&mut self, center: &Vec2, radius: f32, color: &Color);
    fn draw_solid_circle(&mut self, center: &Vec2, radius: f32, axis: &Vec2, color: &Color);
    fn draw_segment(&mut self, p1: &Vec2, p2: &Vec2, color: &Color);
    fn draw_transform(&mut self, xf: &Transform);
}
 
unsafe extern "C" fn draw_polygon<D: Draw>(object: ffi::Any,
                                           vertices: *const Vec2,
                                           count: i32,
                                           color: *const Color) {
    // color comes from a C++ &
    let draw = mem::transmute::<_, &mut D>(object);
    let vertices = ::std::slice::from_raw_parts(vertices, count as usize);
    draw.draw_polygon(vertices, &*color)
}

unsafe extern "C" fn draw_solid_polygon<D: Draw>(object: ffi::Any,
                                                 vertices: *const Vec2,
                                                 count: i32,
                                                 color: *const Color) {
    // color comes from a C++ &
    let draw = mem::transmute::<_, &mut D>(object);
    let vertices = ::std::slice::from_raw_parts(vertices, count as usize);
    draw.draw_solid_polygon(vertices, &*color)
}

unsafe extern "C" fn draw_circle<D: Draw>(object: ffi::Any,
                                     center: *const Vec2,
                                     radius: f32,
                                     color: *const Color) {
    // center and color are coming from C++ &s
    let draw = mem::transmute::<_, &mut D>(object);
    draw.draw_circle(&*center, radius, &*color)
}

unsafe extern "C" fn draw_solid_circle<D: Draw>(object: ffi::Any,
                                                center: *const Vec2,
                                                radius: f32,
                                                axis: *const Vec2,
                                                color: *const Color) {
    // center, axis and color are coming from C++ &s
    let draw = mem::transmute::<_, &mut D>(object);
    draw.draw_solid_circle(&*center, radius, &*axis, &*color)
}

unsafe extern "C" fn draw_segment<D: Draw>(object: ffi::Any,
                                           p1: *const Vec2,
                                           p2: *const Vec2,
                                           color: *const Color) {
    // p1, p2 and color are coming from C++ &s
    let draw = mem::transmute::<_, &mut D>(object);
    draw.draw_segment(&*p1, &*p2, &*color)
}

unsafe extern "C" fn draw_transform<D: Draw>(object: ffi::Any, xf: *const Transform) {
    // xf comes from a C++ &
    let draw = mem::transmute::<_, &mut D>(object);
    draw.draw_transform(&*xf)
}

#[doc(hidden)]
pub struct DrawLink {
    ptr: *mut ffi::DrawLink,
}

impl DrawLink {
    pub unsafe fn new() -> DrawLink {
        DrawLink {
            ptr: ffi::DrawLink_alloc(),
        }
    }

    pub unsafe fn use_with<D: Draw>(&mut self,
                                    draw: &mut D,
                                    flags: DrawFlags) -> *mut ffi::Draw {
        ffi::DrawLink_bind(self.ptr,
                           mem::transmute(draw),
                           draw_polygon::<D>,
                           draw_solid_polygon::<D>,
                           draw_circle::<D>,
                           draw_solid_circle::<D>,
                           draw_segment::<D>,
                           draw_transform::<D>);
        ffi::DrawLink_set_flags(self.ptr, flags);
        ffi::DrawLink_as_base(self.ptr)
    }
}

impl Drop for DrawLink {
    fn drop(&mut self) {
        unsafe { ffi::DrawLink_drop(self.ptr) }
    }
}

#[doc(hidden)]
pub mod ffi {
    pub use ffi::{Any, FatAny};
    use common::math::{Vec2, Transform};
    use super::{Color, DrawFlags};

    pub enum Draw {}
    pub enum DrawLink {}

    extern "C" {
        pub fn DrawLink_alloc() -> *mut DrawLink;
        pub fn DrawLink_as_base(slf: *mut DrawLink) -> *mut Draw;
        pub fn DrawLink_drop(slf: *mut DrawLink);
        pub fn DrawLink_bind(slf: *mut DrawLink,
                             object: Any,
                             draw_polygon: unsafe extern "C" fn(Any,
                                                                *const Vec2,
                                                                i32,
                                                                *const Color),
                             draw_solid_polygon: unsafe extern "C" fn(Any,
                                                                      *const Vec2,
                                                                      i32,
                                                                      *const Color),
                             draw_circle: unsafe extern "C" fn(Any,
                                                               *const Vec2,
                                                               f32,
                                                               *const Color),
                             draw_solid_circle: unsafe extern "C" fn(Any,
                                                                     *const Vec2,
                                                                     f32,
                                                                     *const Vec2,
                                                                     *const Color),
                             draw_segment: unsafe extern "C" fn(Any,
                                                                *const Vec2,
                                                                *const Vec2,
                                                                *const Color),
                             draw_transform: unsafe extern "C" fn(Any, *const Transform));
        pub fn DrawLink_set_flags(slf: *mut DrawLink, flags: DrawFlags);
    }
}