get_pixel_pointer seems to work

This commit is contained in:
IQuant 2024-09-23 22:42:03 +03:00
parent a0fee57d0c
commit 42e720959e
3 changed files with 39 additions and 17 deletions

View file

@ -1,8 +1,7 @@
use std::{
borrow::BorrowMut,
cell::{LazyCell, RefCell},
ffi::{c_int, c_void},
sync::{LazyLock, Mutex},
sync::LazyLock,
};
use lua_bindings::{lua_State, Lua51};
@ -18,7 +17,7 @@ static LUA: LazyLock<Lua51> = LazyLock::new(|| unsafe {
});
thread_local! {
static STATE: LazyCell<ExtState> = LazyCell::new(|| ExtState::default());
static STATE: LazyCell<RefCell<ExtState>> = LazyCell::new(|| ExtState::default().into());
}
#[derive(Default)]
@ -34,15 +33,28 @@ extern "C" fn init_particle_world_state(lua: *mut lua_State) -> c_int {
let chunk_map_pointer = unsafe { LUA.lua_tointeger(lua, 2) };
println!("pws stuff: {world_pointer:?} {chunk_map_pointer:?}");
STATE.with(|mut state| {
state.particle_world_state = Some(ParticleWorldState::new(
STATE.with(|state| {
state.borrow_mut().particle_world_state = Some(ParticleWorldState::new(
world_pointer as *mut c_void,
chunk_map_pointer as *mut c_void,
))
));
});
0
}
extern "C" fn get_pixel_pointer(lua: *mut lua_State) -> c_int {
let x = unsafe { LUA.lua_tointeger(lua, 1) } as i32;
let y = unsafe { LUA.lua_tointeger(lua, 2) } as i32;
STATE.with(|state| {
let state = state.borrow_mut();
let pws = state.particle_world_state.as_ref().unwrap();
let pixel_pointer = unsafe { pws.get_cell(x, y) };
unsafe { LUA.lua_pushinteger(lua, pixel_pointer as isize) };
});
1
}
#[no_mangle]
pub extern "C" fn luaopen_ewext(lua: *mut lua_State) -> c_int {
println!("Initializing ewext");
@ -51,6 +63,8 @@ pub extern "C" fn luaopen_ewext(lua: *mut lua_State) -> c_int {
LUA.lua_pushcclosure(lua, Some(init_particle_world_state), 0);
LUA.lua_setfield(lua, -2, c"init_particle_world_state".as_ptr());
LUA.lua_pushcclosure(lua, Some(get_pixel_pointer), 0);
LUA.lua_setfield(lua, -2, c"get_pixel_pointer".as_ptr());
}
println!("Initializing ewext - Ok");
1

View file

@ -1,4 +1,4 @@
use std::ffi::c_void;
use std::{ffi::c_void, ptr::null};
pub(crate) struct ParticleWorldState {
world_pointer: *mut c_void,
@ -6,16 +6,17 @@ pub(crate) struct ParticleWorldState {
}
impl ParticleWorldState {
unsafe fn get_cell(&self, x: u32, y: u32) -> *const c_void {
let x = x as isize;
let y = y as isize;
let chunk_index = (((((y) >> 9) - 256) & 511) * 512 + ((((x) >> 9) - 256) & 511)) * 4;
let chunk_arr = self
.chunk_map_this
.offset(8)
.cast::<*const *const c_void>()
.read();
let chunk = chunk_arr.offset(chunk_index).read();
pub(crate) unsafe fn get_cell(&self, x: i32, y: i32) -> *const c_void {
let x = dbg!(x as isize);
let y = dbg!(y as isize);
let chunk_index = dbg!((((((y) >> 9) - 256) & 511) * 512 + ((((x) >> 9) - 256) & 511)) * 4);
let chunk_arr = self.chunk_map_this.offset(8).cast::<*const c_void>().read();
// dbg!(chunk_arr);
let chunk = chunk_arr.offset(chunk_index).cast::<*const c_void>().read();
dbg!(chunk);
if chunk.is_null() {
return null();
}
let pixel = chunk.offset(((y & 511) << 9 | x & 511) * 4);
pixel
}

View file

@ -11,4 +11,11 @@ function module.on_world_initialized()
ewext.init_particle_world_state(grid_world, chunk_map)
end
function module.on_local_player_spawn()
local pix_p = ewext.get_pixel_pointer(0, 0)
-- assert(pix_p ~= 0)
-- assert(tonumber(ffi.cast("intptr_t", ppixel)) == pix_p)
end
return module