#[derive(Clone, Copy, Debug)]
pub struct CartConfig {
pub width: u32,
pub height: u32,
pub max_width: u32,
pub max_height: u32,
pub audio_cap: u32,
pub audio_sample_rate: u32,
pub flags: u32,
pub gpu_api: u32,
pub save_size: u32,
pub panic_handler: bool,
}
impl CartConfig {
pub const fn new(width: u32, height: u32) -> Self {
Self {
width,
height,
max_width: width,
max_height: height,
audio_cap: 4096,
audio_sample_rate: 0,
flags: 0,
gpu_api: crate::WC_GPU_API_NONE,
save_size: 0,
panic_handler: true,
}
}
}
#[macro_export]
macro_rules! wc_cart {
($w:expr, $h:expr) => { $crate::wc_cart!($w, $h,); };
($w:expr, $h:expr, $($opts:tt)*) => {
$crate::__wc_split!([$w, $h] [] [panic_handler] $($opts)*);
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __wc_split {
([$w:expr, $h:expr] [$($f:tt)*] [$ph:ident] panic_handler : false, $($rest:tt)*) => {
$crate::__wc_split!([$w, $h] [$($f)* panic_handler: false,] [no_panic_handler] $($rest)*);
};
([$w:expr, $h:expr] [$($f:tt)*] [$ph:ident] panic_handler : true, $($rest:tt)*) => {
$crate::__wc_split!([$w, $h] [$($f)* panic_handler: true,] [panic_handler] $($rest)*);
};
([$w:expr, $h:expr] [$($f:tt)*] [$ph:ident] panic_handler : $other:expr, $($rest:tt)*) => {
::core::compile_error!(
"wc_cart!: panic_handler must be the literal `true` or `false`"
);
};
([$w:expr, $h:expr] [$($f:tt)*] [$ph:ident] $key:ident : $val:expr, $($rest:tt)*) => {
$crate::__wc_split!([$w, $h] [$($f)* $key: $val,] [$ph] $($rest)*);
};
([$w:expr, $h:expr] [$($f:tt)*] [$ph:ident] panic_handler : false) => {
$crate::__wc_split!([$w, $h] [$($f)* panic_handler: false,] [no_panic_handler]);
};
([$w:expr, $h:expr] [$($f:tt)*] [$ph:ident] panic_handler : true) => {
$crate::__wc_split!([$w, $h] [$($f)* panic_handler: true,] [panic_handler]);
};
([$w:expr, $h:expr] [$($f:tt)*] [$ph:ident] panic_handler : $other:expr) => {
::core::compile_error!(
"wc_cart!: panic_handler must be the literal `true` or `false`"
);
};
([$w:expr, $h:expr] [$($f:tt)*] [$ph:ident] $key:ident : $val:expr) => {
$crate::__wc_split!([$w, $h] [$($f)* $key: $val,] [$ph]);
};
([$w:expr, $h:expr] [$($f:tt)*] [$ph:ident]) => {
$crate::wc_cart_config!(
$crate::CartConfig { $($f)* ..$crate::CartConfig::new($w, $h) },
$ph
);
};
}
#[macro_export]
macro_rules! wc_cart_config {
($cfg:expr) => { $crate::wc_cart_config!($cfg, panic_handler); };
($cfg:expr, $ph:ident) => {
const __WC_CFG: $crate::CartConfig = $cfg;
#[doc(hidden)]
#[allow(non_snake_case, dead_code)]
mod __wc_cart {
use super::__WC_CFG as CFG;
use $crate::{WcHostInfo, WcInfo, WcPad, WcPointer, WcTime};
pub const FB_CAP: usize = (CFG.max_width as usize) * (CFG.max_height as usize);
pub const AUDIO_SAMPLES: usize = (CFG.audio_cap as usize) * 2;
pub const SAVE_BYTES: usize = CFG.save_size as usize;
const _: () = assert!(
CFG.width <= CFG.max_width && CFG.height <= CFG.max_height,
"wc_cart!: width/height exceed max_width/max_height"
);
pub static mut FB: [u32; FB_CAP] = [0; FB_CAP];
pub static mut AUDIO: [f32; AUDIO_SAMPLES] = [0.0; AUDIO_SAMPLES];
pub static mut AUDIO_CURSOR: u32 = 0;
pub static mut PADS: [WcPad; 4] = [WcPad {
buttons: 0,
left_x: 0,
left_y: 0,
right_x: 0,
right_y: 0,
left_trigger: 0,
right_trigger: 0,
connected: 0,
_pad: [0; 3],
}; 4];
pub static mut TIME: WcTime = WcTime {
time_ms: 0.0,
delta_ms: 0.0,
frame: 0,
};
pub static mut HOST_INFO: WcHostInfo = WcHostInfo {
preferred_width: 0,
preferred_height: 0,
_reserved0: 0,
audio_sample_rate: 0,
flags: 0,
};
pub static mut POINTERS: [WcPointer; 10] = [WcPointer {
x: 0,
y: 0,
buttons: 0,
active: 0,
_pad: [0; 2],
}; 10];
pub static mut KEYS: [u8; 32] = [0; 32];
pub static mut SAVE: [u8; SAVE_BYTES] = [0; SAVE_BYTES];
pub static mut INFO: WcInfo = WcInfo {
version: $crate::WC_ABI_VERSION,
width: CFG.width,
height: CFG.height,
fb_ptr: 0,
audio_ptr: 0,
audio_cap: CFG.audio_cap,
audio_write_ptr: 0,
input_ptr: 0,
save_ptr: 0,
save_size: CFG.save_size,
time_ptr: 0,
host_info_ptr: 0,
flags: CFG.flags | $crate::WC_FLAG_AUDIO_F32,
audio_sample_rate: CFG.audio_sample_rate,
pointer_ptr: 0,
keys_ptr: 0,
gpu_api: CFG.gpu_api,
};
}
#[allow(dead_code)]
#[inline]
pub fn info() -> &'static mut $crate::WcInfo {
unsafe { &mut *::core::ptr::addr_of_mut!(__wc_cart::INFO) }
}
#[allow(dead_code)]
#[inline]
pub fn fb() -> &'static mut [u32] {
let n = {
let i = info();
(i.width as usize) * (i.height as usize)
};
unsafe {
::core::slice::from_raw_parts_mut(
::core::ptr::addr_of_mut!(__wc_cart::FB) as *mut u32,
n,
)
}
}
#[allow(dead_code)]
#[inline]
pub fn fb_all() -> &'static mut [u32] {
unsafe {
::core::slice::from_raw_parts_mut(
::core::ptr::addr_of_mut!(__wc_cart::FB) as *mut u32,
__wc_cart::FB_CAP,
)
}
}
#[allow(dead_code)]
#[inline]
pub fn audio() -> &'static mut [f32] {
unsafe {
::core::slice::from_raw_parts_mut(
::core::ptr::addr_of_mut!(__wc_cart::AUDIO) as *mut f32,
__wc_cart::AUDIO_SAMPLES,
)
}
}
#[allow(dead_code)]
#[inline]
pub fn audio_cursor() -> &'static mut u32 {
unsafe { &mut *::core::ptr::addr_of_mut!(__wc_cart::AUDIO_CURSOR) }
}
#[allow(dead_code)]
#[inline]
pub fn pads() -> &'static [$crate::WcPad; 4] {
unsafe { &*::core::ptr::addr_of!(__wc_cart::PADS) }
}
#[allow(dead_code)]
#[inline]
pub fn time() -> &'static $crate::WcTime {
unsafe { &*::core::ptr::addr_of!(__wc_cart::TIME) }
}
#[allow(dead_code)]
#[inline]
pub fn host_info() -> &'static $crate::WcHostInfo {
unsafe { &*::core::ptr::addr_of!(__wc_cart::HOST_INFO) }
}
#[allow(dead_code)]
#[inline]
pub fn pointers() -> &'static [$crate::WcPointer; 10] {
unsafe { &*::core::ptr::addr_of!(__wc_cart::POINTERS) }
}
#[allow(dead_code)]
#[inline]
pub fn keys() -> &'static [u8; 32] {
unsafe { &*::core::ptr::addr_of!(__wc_cart::KEYS) }
}
#[allow(dead_code)]
#[inline]
pub fn save() -> &'static mut [u8] {
unsafe {
::core::slice::from_raw_parts_mut(
::core::ptr::addr_of_mut!(__wc_cart::SAVE) as *mut u8,
__wc_cart::SAVE_BYTES,
)
}
}
#[allow(dead_code)]
#[inline]
pub fn set_size(w: u32, h: u32) -> bool {
if w == 0 || h == 0 {
return false;
}
if (w as usize) * (h as usize) > __wc_cart::FB_CAP {
return false;
}
let i = info();
i.width = w;
i.height = h;
true
}
#[no_mangle]
pub extern "C" fn wc_get_info() -> *mut $crate::WcInfo {
unsafe {
let i = &mut *::core::ptr::addr_of_mut!(__wc_cart::INFO);
i.fb_ptr = ::core::ptr::addr_of!(__wc_cart::FB) as u32;
i.audio_ptr = if __wc_cart::AUDIO_SAMPLES > 0 {
::core::ptr::addr_of!(__wc_cart::AUDIO) as u32
} else {
0
};
i.audio_write_ptr = ::core::ptr::addr_of!(__wc_cart::AUDIO_CURSOR) as u32;
i.input_ptr = ::core::ptr::addr_of!(__wc_cart::PADS) as u32;
i.save_ptr = if __wc_cart::SAVE_BYTES > 0 {
::core::ptr::addr_of!(__wc_cart::SAVE) as u32
} else {
0
};
i.time_ptr = ::core::ptr::addr_of!(__wc_cart::TIME) as u32;
i.host_info_ptr = ::core::ptr::addr_of!(__wc_cart::HOST_INFO) as u32;
i.pointer_ptr = ::core::ptr::addr_of!(__wc_cart::POINTERS) as u32;
i.keys_ptr = ::core::ptr::addr_of!(__wc_cart::KEYS) as u32;
::core::ptr::addr_of_mut!(__wc_cart::INFO)
}
}
$crate::__wc_panic_handler!($ph);
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __wc_panic_handler {
(no_panic_handler) => {};
(panic_handler) => {
#[cfg(target_arch = "wasm32")]
$crate::__wc_panic_handler_inner!();
};
}
#[doc(hidden)]
#[cfg(not(feature = "std"))]
#[macro_export]
macro_rules! __wc_panic_handler_inner {
() => {
#[panic_handler]
fn __wc_panic(info: &::core::panic::PanicInfo) -> ! {
$crate::__private::report_panic(info)
}
};
}
#[doc(hidden)]
#[cfg(feature = "std")]
#[macro_export]
macro_rules! __wc_panic_handler_inner {
() => {};
}