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
use crate::core::math::{Vector2, Vector3};
use crate::core::RaylibHandle;
use crate::ffi;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Camera3D {
pub position: Vector3,
pub target: Vector3,
pub up: Vector3,
pub fovy: f32,
projection_: ffi::CameraProjection,
}
pub type Camera = Camera3D;
impl From<ffi::Camera3D> for Camera3D {
fn from(v: ffi::Camera3D) -> Camera3D {
unsafe { std::mem::transmute(v) }
}
}
impl Into<ffi::Camera3D> for Camera3D {
fn into(self) -> ffi::Camera3D {
unsafe { std::mem::transmute(self) }
}
}
impl Into<ffi::Camera3D> for &Camera3D {
fn into(self) -> ffi::Camera3D {
ffi::Camera3D {
position: self.position.into(),
target: self.target.into(),
up: self.up.into(),
fovy: self.fovy,
projection: (self.projection_ as u32) as i32,
}
}
}
#[repr(C)]
#[derive(Debug, Copy, Clone, Default)]
pub struct Camera2D {
pub offset: Vector2,
pub target: Vector2,
pub rotation: f32,
pub zoom: f32,
}
impl From<ffi::Camera2D> for Camera2D {
fn from(v: ffi::Camera2D) -> Camera2D {
unsafe { std::mem::transmute(v) }
}
}
impl Into<ffi::Camera2D> for Camera2D {
fn into(self) -> ffi::Camera2D {
unsafe { std::mem::transmute(self) }
}
}
impl Into<ffi::Camera2D> for &Camera2D {
fn into(self) -> ffi::Camera2D {
ffi::Camera2D {
offset: self.offset.into(),
target: self.target.into(),
rotation: self.rotation,
zoom: self.zoom,
}
}
}
impl Camera3D {
pub fn camera_type(&self) -> crate::consts::CameraProjection {
unsafe { std::mem::transmute(self.projection_.clone()) }
}
pub fn perspective(position: Vector3, target: Vector3, up: Vector3, fovy: f32) -> Camera3D {
Camera3D {
position,
target,
up,
fovy,
projection_: ffi::CameraProjection::CAMERA_PERSPECTIVE,
}
}
pub fn orthographic(position: Vector3, target: Vector3, up: Vector3, fovy: f32) -> Camera3D {
let mut c = Self::perspective(position, target, up, fovy);
c.projection_ = ffi::CameraProjection::CAMERA_ORTHOGRAPHIC;
c
}
}
impl RaylibHandle {
#[inline]
pub fn set_camera_mode(
&mut self,
camera: impl Into<ffi::Camera3D>,
mode: crate::consts::CameraMode,
) {
unsafe {
ffi::SetCameraMode(camera.into(), mode as i32);
}
}
#[inline]
pub fn update_camera(&self, camera: &mut Camera3D) {
unsafe {
let mut fficam: ffi::Camera3D = (*camera).into();
ffi::UpdateCamera(&mut fficam);
*camera = fficam.into();
}
}
#[inline]
pub fn set_camera_pan_control(&mut self, pan_key: crate::consts::KeyboardKey) {
unsafe {
ffi::SetCameraPanControl(pan_key as i32);
}
}
#[inline]
pub fn set_camera_alt_control(&mut self, alt_key: crate::consts::KeyboardKey) {
unsafe {
ffi::SetCameraAltControl(alt_key as i32);
}
}
#[inline]
pub fn set_camera_smooth_zoom_control(&mut self, sz_key: crate::consts::KeyboardKey) {
unsafe {
ffi::SetCameraSmoothZoomControl(sz_key as i32);
}
}
#[inline]
pub fn set_camera_move_controls(
&mut self,
front_key: crate::consts::KeyboardKey,
back_key: crate::consts::KeyboardKey,
right_key: crate::consts::KeyboardKey,
left_key: crate::consts::KeyboardKey,
up_key: crate::consts::KeyboardKey,
down_key: crate::consts::KeyboardKey,
) {
unsafe {
ffi::SetCameraMoveControls(
front_key as i32,
back_key as i32,
right_key as i32,
left_key as i32,
up_key as i32,
down_key as i32,
);
}
}
}