a comically bad 2D UI library for rust nintendo 3DS homebrew using its PICA2000 gpu, intended for my music player and to learn how better to design a library in Rust.
a comically bad 2D UI library for rust nintendo 3DS homebrew using its PICA2000 gpu, intended for my music player and to learn how better to design a library in Rust.
the start of this god-foresaken UI library. this is the first time I’ve written code in a few months, and I don’t have much experience writing a library, so bare with me!
I decided on an immediate mode UI design inspired by the likes of egui. however, it is wasteful to have to create resources every frame for. as such, I decided to implement memoization. I’m not sure if this is the best way, but:
for every pub trait UiObj: Any
there is a pub trait UiObjMemoize: dyn_hash::DynHash {}.
this latter memoize trait is constructed only from the immediate mode method, and does not hold any other state required for blitting the object.
for instance, a if container.button("hello, world!") { /* ... */ } will create an instance of the struct
#[derive(Hash)]
pub(crate) struct ButtonMemoizeState<'a>(&'a str, &'a ButtonStyle);
as ButtonMemoizeState("hello, world!", &ButtonStyle::default()).
this is then looked up in a hashmap of the Container of the field
pub children: HashMap<u64, Box<dyn UiObj>>.
note that I am using a u64 here rather than a dyn UiObjMemoize – this is to workaround a limitation where you cannot use a dyn trait object as a hashmap key. instead, I use a stable hasher from the fx_hasher crate where I hash it to a u64, acheiving the same result but slightly more long-winded. (fn hash_memoized_obj<T: UiObjMemoize>(v: &T) -> u64;)
technicality aside, the API now looks like:
fn render(ui: &mut fruit::Ui) {
// todo: enter container with closure
if ui.top_root.button("HAIII") {
println!(">w<");
};
}
no tangible example yet, just base code.
todo: garbage collection / reference counting for memoization, closures and ctru context (HID, GFX, APT, citro2d::Instance, app state), layouts, rendering backend.
blockers: rendering backend (notably C2D_Text).
https://git.sr.ht/~sheepy/fruit/tree/460c3a620a6ecfec7da690d71e185de887286c18
Log in to leave a comment