From 40f15643d94d3859899a6bd55a57bafcdcc3f477 Mon Sep 17 00:00:00 2001 From: Andreew Gregory Date: Fri, 17 Apr 2026 02:14:30 +0300 Subject: [PATCH] I made some poor geom.rs, and added set_clear_color API --- src/l2/alice/engine.h | 4 + src/l2/allie_rs/geom.rs | 127 ++++++++++++++++++++++++++++++ src/l2/allie_rs/int_primitives.rs | 30 +++++++ src/l2/allie_rs/lib.rs | 30 ++++--- src/l3/r4/r4d.rs | 6 +- 5 files changed, 187 insertions(+), 10 deletions(-) create mode 100644 src/l2/allie_rs/geom.rs create mode 100644 src/l2/allie_rs/int_primitives.rs diff --git a/src/l2/alice/engine.h b/src/l2/alice/engine.h index 91805f0..b9d03e5 100644 --- a/src/l2/alice/engine.h +++ b/src/l2/alice/engine.h @@ -1997,3 +1997,7 @@ void Alice_set_camera_info(Alice* alice, mat4 t_mat, vec3 cam_pos) { bool Alice_is_wl_key_pressed(Alice* alice, xkb_keysym_t keysym) { return keysym < 0x80 ? alice->wl.first_0x80_keys[keysym] : false; } + +void Alice_set_clear_color(Alice* alice, vec4 color){ + alice->rendering_config.clear_color = color; +} diff --git a/src/l2/allie_rs/geom.rs b/src/l2/allie_rs/geom.rs new file mode 100644 index 0000000..0504503 --- /dev/null +++ b/src/l2/allie_rs/geom.rs @@ -0,0 +1,127 @@ +use super::int_primitives::{*}; +use std::ops::{Add, Sub, Mul, Div}; +use std::array; + +pub struct Assert; + +pub trait AssertTrue {} +impl AssertTrue for Assert{} + +/* Turn out for tuple struct, where clause goes after the field list */ +#[repr(C)] +pub struct GeomVec (pub [T; N]) where T: Copy + Clone; + +impl Add for GeomVec where + T: SignedNumber +{ + type Output = GeomVec; + + fn add(self, rhs: Self) -> Self::Output { + GeomVec(array::from_fn(|i| self.0[i] + rhs.0[i])) + } +} + +trait HasY { + fn has_foo() -> bool { + return true; + } +} + +// impl HasY for GeomVec where +// T: Copy + Clone, +// Assert<{N >= 1}> : AssertTrue { +// +// } + + +impl GeomVec where + T: Copy + Clone, + Assert<{N >= 1}> : AssertTrue { + + pub fn x(&self) -> T { self.0[0] } +} + +impl GeomVec where + T: Copy + Clone, + Assert<{N >= 2}> : AssertTrue { + + pub fn y(&self) -> T { self.0[1] } +} + +impl GeomVec where + T: Copy + Clone, + Assert<{N >= 3}> : AssertTrue { + + pub fn z(&self) -> T { self.0[2] } +} + +impl GeomVec where + T: Copy + Clone, + Assert<{N >= 4}> : AssertTrue { + + pub fn w(&self) -> T { self.0[3] } +} + +pub type vec2 = GeomVec; +pub type vec3 = GeomVec; +pub type vec4 = GeomVec; + +pub const fn vec2(x: f32, y: f32) -> vec2 { + GeomVec([x, y]) +} + +pub const fn vec3(x: f32, y: f32, z: f32) -> vec3 { + GeomVec([x, y, z]) +} + +pub const fn vec4(x: f32, y: f32, z: f32, w: f32) -> vec4 { + GeomVec([x, y, z, w]) +} + +pub type ivec2 = GeomVec; +pub type ivec3 = GeomVec; +pub type ivec4 = GeomVec; + +pub const fn ivec2(x: i32, y: i32) -> ivec2 { + GeomVec([x, y]) +} + +pub const fn ivec3(x: i32, y: i32, z: i32) -> ivec3 { + GeomVec([x, y, z]) +} + +pub const fn ivec4(x: i32, y: i32, z: i32, w: i32) -> ivec4 { + GeomVec([x, y, z, w]) +} + +pub type uvec2 = GeomVec; +pub type uvec3 = GeomVec; +pub type uvec4 = GeomVec; + +pub const fn uvec2(x: u32, y: u32) -> uvec2 { + GeomVec([x, y]) +} + +pub const fn uvec3(x: u32, y: u32, z: u32) -> uvec3 { + GeomVec([x, y, z]) +} + +pub const fn uvec4(x: u32, y: u32, z: u32, w: u32) -> uvec4 { + GeomVec([x, y, z, w]) +} + +pub type cvec2 = GeomVec; +pub type cvec3 = GeomVec; +pub type cvec4 = GeomVec; + +pub const fn cvec2(x: u8, y: u8) -> cvec2 { + GeomVec([x, y]) +} + +pub const fn cvec3(x: u8, y: u8, z: u8) -> cvec3 { + GeomVec([x, y, z]) +} + +pub const fn cvec4(x: u8, y: u8, z: u8, w: u8) -> cvec4 { + GeomVec([x, y, z, w]) +} \ No newline at end of file diff --git a/src/l2/allie_rs/int_primitives.rs b/src/l2/allie_rs/int_primitives.rs new file mode 100644 index 0000000..e44a110 --- /dev/null +++ b/src/l2/allie_rs/int_primitives.rs @@ -0,0 +1,30 @@ +use std::ops::{Add, Sub, Mul, Div}; + +pub trait SignedNumber: Copy + Clone + + Add + + Sub + + Mul +{ + const ZERO : Self; + const ONE : Self; +} + +impl SignedNumber for i32 { + const ZERO: Self = 0; + const ONE: Self = 1; +} + +impl SignedNumber for f32 { + const ZERO: Self = 0.; + const ONE: Self = 1.; +} + +pub trait FloatingNumber: SignedNumber + Div { + fn sqrt(self) -> Self; +} + +impl FloatingNumber for f32 { + fn sqrt(self) -> Self{ + f32::sqrt(self) + } +} diff --git a/src/l2/allie_rs/lib.rs b/src/l2/allie_rs/lib.rs index 7352f4b..e6d24d7 100644 --- a/src/l2/allie_rs/lib.rs +++ b/src/l2/allie_rs/lib.rs @@ -1,14 +1,20 @@ +#![feature(generic_const_exprs)] +#![allow(incomplete_features)] + +#![allow(non_camel_case_types)] +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(unused)] + /* *c_void is nothing special, it is the same as *u8 for example */ +pub mod geom; +pub mod int_primitives; + use std::ffi::{c_void}; +use geom::{*}; type VoidPtr = *mut c_void; -#[repr(C)] -pub struct Vec2{ - pub x: f32, - pub y: f32 -} - #[repr(C)] struct allie_AliceCallbacks{ guest: VoidPtr, @@ -18,7 +24,7 @@ struct allie_AliceCallbacks{ on_wl_keyboard_key: extern "C" fn (VoidPtr, u32, u32), /* guest data, passed time */ on_another_frame: extern "C" fn (VoidPtr, f32), - on_wl_pointer_motion: extern "C" fn (VoidPtr, Vec2), + on_wl_pointer_motion: extern "C" fn (VoidPtr, vec2), } unsafe extern "C" { @@ -26,6 +32,8 @@ unsafe extern "C" { fn allie_Alice_new() -> *mut c_void; #[link_name="Alice_mainloop"] fn allie_Alice_mainloop(alice: VoidPtr, cb: *const allie_AliceCallbacks); + #[link_name="Alice_set_clear_color"] + fn allie_Alice_set_clear_color(alice: VoidPtr, color: vec4); } pub struct Alice { @@ -36,7 +44,7 @@ pub struct AliceCallbacks { pub on_wl_pointer_button: Box, pub on_wl_keyboard_key: Box, pub on_another_frame: Box, - pub on_wl_pointer_motion: Box, + pub on_wl_pointer_motion: Box, } extern "C" fn allie_AliceCallbacks_on_wl_pointer_button(dt: VoidPtr, button: u32, act: u32){ @@ -54,7 +62,7 @@ extern "C" fn allie_AliceCallbacks_on_another_frame(dt: VoidPtr, fl: f32){ (cb.on_another_frame)(fl); } -extern "C" fn allie_AliceCallbacks_on_wl_pointer_motion(dt: VoidPtr, pos: Vec2){ +extern "C" fn allie_AliceCallbacks_on_wl_pointer_motion(dt: VoidPtr, pos: vec2){ let cb: &mut AliceCallbacks = unsafe { &mut *(dt as *mut AliceCallbacks) }; (cb.on_wl_pointer_motion)(pos); } @@ -77,6 +85,10 @@ impl Alice { unsafe{ allie_Alice_mainloop(self.opa, &a_cb) }; } + + pub fn set_clear_color(&self, color: vec4) { + unsafe { allie_Alice_set_clear_color(self.opa, color) }; + } } impl Drop for Alice { fn drop(&mut self) { diff --git a/src/l3/r4/r4d.rs b/src/l3/r4/r4d.rs index 37893d1..9b65fb7 100644 --- a/src/l3/r4/r4d.rs +++ b/src/l3/r4/r4d.rs @@ -1,7 +1,11 @@ use alice_and_misc::{*}; +use alice_and_misc::geom::{*}; fn main() { + let v = vec2(1.0, 2.0); + let alice = Alice::new(); + alice.set_clear_color(vec4(0., 1., 0., 1.)); alice.mainloop(AliceCallbacks{ on_wl_pointer_button: Box::new(|_: u32, _: u32| { println!("pointer button") @@ -12,7 +16,7 @@ fn main() { on_another_frame: Box::new(|_: f32| { }), - on_wl_pointer_motion: Box::new(|_: Vec2| { + on_wl_pointer_motion: Box::new(|_: vec2| { println!("pointer motion"); }), })