I made some poor geom.rs, and added set_clear_color API

This commit is contained in:
Андреев Григорий 2026-04-17 02:14:30 +03:00
parent 87feca9222
commit 40f15643d9
5 changed files with 187 additions and 10 deletions

View File

@ -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;
}

127
src/l2/allie_rs/geom.rs Normal file
View File

@ -0,0 +1,127 @@
use super::int_primitives::{*};
use std::ops::{Add, Sub, Mul, Div};
use std::array;
pub struct Assert<const COND: bool>;
pub trait AssertTrue {}
impl AssertTrue for Assert<true>{}
/* Turn out for tuple struct, where clause goes after the field list */
#[repr(C)]
pub struct GeomVec<T, const N: usize> (pub [T; N]) where T: Copy + Clone;
impl<T, const N: usize> Add for GeomVec<T, N> where
T: SignedNumber
{
type Output = GeomVec<T, N>;
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<T, const N: usize> HasY for GeomVec<T, N> where
// T: Copy + Clone,
// Assert<{N >= 1}> : AssertTrue {
//
// }
impl<T, const N: usize> GeomVec<T, N> where
T: Copy + Clone,
Assert<{N >= 1}> : AssertTrue {
pub fn x(&self) -> T { self.0[0] }
}
impl<T, const N: usize> GeomVec<T, N> where
T: Copy + Clone,
Assert<{N >= 2}> : AssertTrue {
pub fn y(&self) -> T { self.0[1] }
}
impl<T, const N: usize> GeomVec<T, N> where
T: Copy + Clone,
Assert<{N >= 3}> : AssertTrue {
pub fn z(&self) -> T { self.0[2] }
}
impl<T, const N: usize> GeomVec<T, N> where
T: Copy + Clone,
Assert<{N >= 4}> : AssertTrue {
pub fn w(&self) -> T { self.0[3] }
}
pub type vec2 = GeomVec<f32, 2>;
pub type vec3 = GeomVec<f32, 3>;
pub type vec4 = GeomVec<f32, 4>;
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<i32, 2>;
pub type ivec3 = GeomVec<i32, 3>;
pub type ivec4 = GeomVec<i32, 4>;
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<u32, 2>;
pub type uvec3 = GeomVec<u32, 3>;
pub type uvec4 = GeomVec<u32, 4>;
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<u8, 2>;
pub type cvec3 = GeomVec<u8, 3>;
pub type cvec4 = GeomVec<u8, 4>;
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])
}

View File

@ -0,0 +1,30 @@
use std::ops::{Add, Sub, Mul, Div};
pub trait SignedNumber: Copy + Clone +
Add<Output = Self> +
Sub<Output = Self> +
Mul<Output = Self>
{
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<Output = Self> {
fn sqrt(self) -> Self;
}
impl FloatingNumber for f32 {
fn sqrt(self) -> Self{
f32::sqrt(self)
}
}

View File

@ -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<dyn FnMut(u32, u32)>,
pub on_wl_keyboard_key: Box<dyn FnMut(u32, u32)>,
pub on_another_frame: Box<dyn FnMut(f32)>,
pub on_wl_pointer_motion: Box<dyn FnMut(Vec2)>,
pub on_wl_pointer_motion: Box<dyn FnMut(vec2)>,
}
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) {

View File

@ -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");
}),
})