rokol/
lib.rs

1/*!
2Wrapper of [Sokol] libraries
3
4[Sokol]: https://github.com/floooh/sokol
5
6# Features (specified in `Cargo.toml`)
7
8Example settings:
9
10```toml
11rokol = { features = ["sdl", "impl-gfx", "glcore33", "fontstash"] }
12```
13
14* `impl-app`: implements `sokol_app.h` and enables `app` module
15* `sdl2`: generates [`glue`] code for `sdl2`
16* `impl-gfx`: implements `sokol_gfx.h` and enables `gfx` module
17  * `glcore33`: uses OpenGL backend
18  * `metal`: uses Metal backend
19  * `d3d11`: uses DirectX11 backend
20* `fontstash`: implements `fontstash.h` and enables `fons` module
21
22# Tips
23
24* Checkout [The Brain Dump]
25  * Sokol [considers] zero-initizialized structures to be in default state. It means
26    [`Default::default`] is ensured to make sense!
27* use `bytemuck` to cast types to `&[u8]`.
28
29[The Brain Dump]: https://floooh.github.io/
30[considers]: https://floooh.github.io/2017/08/06/sokol-api-update.html
31*/
32
33pub use rokol_ffi as ffi;
34
35/// Creates an `enum` from FFI enum type (output of bindgen as a rustified enum)
36macro_rules! ffi_enum {
37    (
38        $(#[$outer:meta])*
39        $vis:vis enum $Enum:ident around $Ffi:ty {
40            $(
41                $(#[$attr:ident $($args:tt)*])*
42                $variant:ident = $ffi_variant:ident,
43            )*
44        }
45
46        $($t:tt)*
47    ) => {
48        $(#[$outer])*
49        #[repr(u32)]
50        $vis enum $Enum {
51            $(
52                $(#[$attr $($args)*])*
53                $variant = <$Ffi>::$ffi_variant as u32,
54            )*
55        }
56
57        impl $Enum {
58            pub fn from_ffi(ffi_variant: $Ffi) -> Self {
59                match ffi_variant {
60                    $(
61                        <$Ffi>::$ffi_variant => Self::$variant,
62                    )*
63                    _ => panic!("Bug: not convered FFI enum!"),
64                }
65            }
66
67            pub fn to_ffi(self) -> $Ffi {
68                match self {
69                    $(
70                        <Self>::$variant => <$Ffi>::$ffi_variant,
71                    )*
72                }
73            }
74        }
75
76        impl From<$Ffi> for $Enum {
77            fn from(ffi_variant: $Ffi) -> Self {
78                Self::from_ffi(ffi_variant)
79            }
80        }
81
82        impl Into<$Ffi> for $Enum {
83            fn into(self) -> $Ffi {
84                Self::to_ffi(self)
85            }
86        }
87    };
88}
89
90#[cfg(feature = "impl-app")]
91pub mod app;
92
93#[cfg(feature = "impl-gfx")]
94pub mod gfx;
95
96#[cfg(feature = "impl-gfx")]
97pub mod glue;
98
99#[cfg(all(feature = "impl-gfx", feature = "fontstash"))]
100pub mod fons;