Skip to content

Commit c1695a8

Browse files
committed
Added and reviewed raygui, add some missing impl Into and other smaller things.
Raygui wasn't really safe, as it relied on "use after free" to be fine, this review made it less reliant on this undefined behavior.
1 parent 7c9fe91 commit c1695a8

File tree

17 files changed

+186
-212
lines changed

17 files changed

+186
-212
lines changed

raylib-sys/binding/binding.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include "../raylib/src/rlgl.h"
33
#include "../raylib/src/rcamera.h"
44

5-
#if 0
65
#include "../raygui/src/raygui.h"
76

87
typedef enum
@@ -263,5 +262,4 @@ typedef enum
263262
RAYGUI_ICON_253 = 253,
264263
RAYGUI_ICON_254 = 254,
265264
RAYGUI_ICON_255 = 255,
266-
} guiRAYGUI_ICONName;
267-
#endif
265+
} guiRAYGUI_ICONName;

raylib-sys/binding/rgui_wrapper.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "../raylib/src/raylib.h"
2-
//#define RAYGUI_IMPLEMENTATION
3-
//#define RAYGUI_SUPPORT_ICONS
2+
#define RAYGUI_IMPLEMENTATION
3+
#define RAYGUI_SUPPORT_ICONS
44
#define RLGL_IMPLEMENTATION
55
#define RLGL_SUPPORT_TRACELOG
6-
// #include "rlgl.h" // Don't include rlgl since it's in raylib
7-
//#include "raygui.h"
6+
#include "../raylib/src/rlgl.h" // Don't include rlgl since it's in raylib
7+
#include "../raygui/src/raygui.h"

raylib-sys/binding/wrapper.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include "../raylib/src/raylib.h"
22

3-
#if 0
43
#define RAYGUI_IMPLEMENTATION
54
#define RAYGUI_SUPPORT_ICONS
6-
#include "../raygui/src/raygui.h"
7-
#endif
5+
#include "../raygui/src/raygui.h"

raylib-sys/build.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ fn gen_bindings() {
189189
.clang_arg("-I../raylib/src")
190190
.clang_arg("-std=c99")
191191
.clang_arg(plat);
192-
//.parse_callbacks(Box::new(bindgen::CargoCallbacks));
192+
//.parse_callbacks(Box::new(bindgen::CargoCallbacks));
193193

194194
if platform == Platform::Desktop && os == PlatformOS::Windows {
195195
// odd workaround for booleans being broken
@@ -295,7 +295,7 @@ fn cp_raylib() -> String {
295295

296296
let mut options = fs_extra::dir::CopyOptions::new();
297297
options.skip_exist = true;
298-
fs_extra::dir::copy("raylib", &out, &options)
298+
fs_extra::dir::copy("raylib", out, &options)
299299
.unwrap_or_else(|_| panic!("failed to copy raylib source to {}", out.to_string_lossy()));
300300

301301
out.join("raylib").to_string_lossy().to_string()
@@ -307,7 +307,7 @@ fn cp_raygui() -> String {
307307

308308
let mut options = fs_extra::dir::CopyOptions::new();
309309
options.skip_exist = true;
310-
fs_extra::dir::copy("raygui", &out, &options)
310+
fs_extra::dir::copy("raygui", out, &options)
311311
.unwrap_or_else(|_| panic!("failed to copy raygui source to {}", out.to_string_lossy()));
312312

313313
out.join("raygui").to_string_lossy().to_string()

raylib-sys/raygui

Submodule raygui added at 1d9fd31

raylib-sys/src/camera.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::mem::transmute;
22

3-
use super::{Matrix, Camera3D, CameraProjection};
3+
use super::{Camera3D, CameraProjection, Matrix};
44
use mint::Vector3;
55

66
impl Camera3D {
@@ -10,9 +10,9 @@ impl Camera3D {
1010
/// Create a perspective camera.
1111
/// fovy is in degrees
1212
pub fn perspective(
13-
position: Vector3<f32>,
14-
target: Vector3<f32>,
15-
up: Vector3<f32>,
13+
position: impl Into<Vector3<f32>>,
14+
target: impl Into<Vector3<f32>>,
15+
up: impl Into<Vector3<f32>>,
1616
fovy: f32,
1717
) -> Camera3D {
1818
Camera3D {
@@ -26,9 +26,9 @@ impl Camera3D {
2626
/// Create a orthographic camera.
2727
/// fovy is in degrees
2828
pub fn orthographic(
29-
position: Vector3<f32>,
30-
target: Vector3<f32>,
31-
up: Vector3<f32>,
29+
position: impl Into<Vector3<f32>>,
30+
target: impl Into<Vector3<f32>>,
31+
up: impl Into<Vector3<f32>>,
3232
fovy: f32,
3333
) -> Camera3D {
3434
let mut c = Self::perspective(position, target, up, fovy);
@@ -37,11 +37,11 @@ impl Camera3D {
3737
}
3838

3939
pub fn forward(&self) -> Vector3<f32> {
40-
unsafe { super::GetCameraForward(self as *const _ as *mut _).into() }
40+
unsafe { super::GetCameraForward(self as *const _ as *mut _) }
4141
}
4242

4343
pub fn up(&self) -> Vector3<f32> {
44-
unsafe { super::GetCameraUp(self as *const _ as *mut _).into() }
44+
unsafe { super::GetCameraUp(self as *const _ as *mut _) }
4545
}
4646

4747
pub fn move_forward(&mut self, distance: f32, in_world_plane: bool) {

raylib-sys/src/color.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ impl Color {
7070
/// Returns color normalized as float [0..1]
7171
#[inline]
7272
pub fn color_normalize(&self) -> Vector4<f32> {
73-
unsafe { crate::ColorNormalize(*self).into() }
73+
unsafe { crate::ColorNormalize(*self) }
7474
}
7575

7676
/// Returns HSV values for a Color
7777
#[inline]
7878
pub fn color_to_hsv(&self) -> Vector3<f32> {
79-
unsafe { crate::ColorToHSV(*self).into() }
79+
unsafe { crate::ColorToHSV(*self) }
8080
}
8181

8282
/// Returns a Color from HSV values
@@ -90,7 +90,7 @@ impl Color {
9090
/// assert_eq!(Color::color_from_normalized(Vector4 { x: 1.0, y: 1.0, z: 1.0, w: 1.0 }), Color::new(255, 255, 255, 255));
9191
/// ```
9292
#[inline]
93-
pub fn color_from_normalized(normalized: Vector4<f32>) -> Color {
93+
pub fn color_from_normalized(normalized: impl Into<Vector4<f32>>) -> Color {
9494
unsafe { crate::ColorFromNormalized(normalized.into()) }
9595
}
9696

raylib-sys/src/math.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,18 @@ impl Rectangle {
1818

1919
/// Checks collision between circle and rectangle.
2020
#[inline]
21-
pub fn check_collision_circle_rec(&self, center: Vector2, radius: f32) -> bool {
21+
pub fn check_collision_circle_rec(&self, center: impl Into<Vector2>, radius: f32) -> bool {
2222
unsafe { crate::CheckCollisionCircleRec(center.into(), radius, *self) }
2323
}
2424

2525
/// Gets the overlap between two colliding rectangles.
2626
/// ```rust
2727
/// use raylib::prelude::*;
28-
/// fn main() {
29-
/// let r1 = Rectangle::new(0.0, 0.0, 10.0, 10.0);
30-
/// let r2 = Rectangle::new(20.0, 20.0, 10.0, 10.0);
31-
/// assert_eq!(None, r1.get_collision_rec(&r2));
32-
/// assert_eq!(Some(r1), r1.get_collision_rec(&r1));
33-
/// }
28+
///
29+
/// let r1 = Rectangle::new(0.0, 0.0, 10.0, 10.0);
30+
/// let r2 = Rectangle::new(20.0, 20.0, 10.0, 10.0);
31+
/// assert_eq!(None, r1.get_collision_rec(&r2));
32+
/// assert_eq!(Some(r1), r1.get_collision_rec(&r1));
3433
/// ```
3534
#[inline]
3635
pub fn get_collision_rec(&self, other: Rectangle) -> Option<Rectangle> {
@@ -40,7 +39,7 @@ impl Rectangle {
4039

4140
/// Checks if point is inside rectangle.
4241
#[inline]
43-
pub fn check_collision_point_rec(&self, point: Vector2) -> bool {
42+
pub fn check_collision_point_rec(&self, point: impl Into<Vector2>) -> bool {
4443
unsafe { crate::CheckCollisionPointRec(point.into(), *self) }
4544
}
4645
}
@@ -54,7 +53,11 @@ impl BoundingBox {
5453

5554
/// Detects collision between box and sphere.
5655
#[inline]
57-
pub fn check_collision_box_sphere(&self, center_sphere: Vector3, radius_sphere: f32) -> bool {
56+
pub fn check_collision_box_sphere(
57+
&self,
58+
center_sphere: impl Into<Vector3>,
59+
radius_sphere: f32,
60+
) -> bool {
5861
unsafe { crate::CheckCollisionBoxSphere(*self, center_sphere.into(), radius_sphere) }
5962
}
6063

raylib/src/audio.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,8 @@ impl<'bind, 'a> AudioStream<'bind, 'a> {
425425
/// Updates audio stream buffers with data.
426426
#[inline]
427427
pub fn update_audio_stream<T: AudioSample>(&mut self, data: &[T]) {
428-
assert_eq!(self.sampleSize as usize, core::mem::size_of::<T>() * 8);
428+
assert_eq!(self.0.sampleSize as usize, core::mem::size_of::<T>() * 8);
429429

430-
unsafe {
431-
ffi::UpdateAudioStream(self.0, data.as_ptr() as _, data.len() as i32);
432-
}
430+
unsafe { ffi::UpdateAudioStream(self.0, data.as_ptr() as _, data.len() as i32) }
433431
}
434432
}

raylib/src/core/camera.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Utility code for using Raylib [`Camera3D`] and [`Camera2D`]
22
use super::RaylibHandle;
3-
use crate::ffi::{self, Vector3, Camera3D, CameraMode};
3+
use crate::ffi::{self, Camera3D, CameraMode, Vector3};
44

55
impl RaylibHandle<'_> {
66
/// Updates camera position for selected mode.

0 commit comments

Comments
 (0)