Skip to main content

live2d_cubism_core_sys/
lib.rs

1//! Raw FFI bindings to the Live2D Cubism SDK Core (v5).
2//!
3//! These bindings correspond to `Live2DCubismCore.h` (SDK v5).
4//! All functions are unsafe — see the Cubism SDK documentation for usage.
5//!
6//! # Memory Alignment
7//!
8//! - Moc data must be aligned to [`ALIGN_OF_MOC`] (64 bytes).
9//! - Model memory must be aligned to [`ALIGN_OF_MODEL`] (16 bytes).
10
11#![allow(non_camel_case_types, non_upper_case_globals)]
12
13use std::os::raw::{c_char, c_float, c_int, c_uchar, c_uint, c_ushort, c_void};
14
15// ── Opaque types ────────────────────────────────────────────────────────
16
17/// Opaque Cubism moc handle.
18#[repr(C)]
19pub struct csmMoc {
20    _opaque: [u8; 0],
21}
22
23/// Opaque Cubism model handle.
24#[repr(C)]
25pub struct csmModel {
26    _opaque: [u8; 0],
27}
28
29// ── Scalar types ────────────────────────────────────────────────────────
30
31/// Cubism version identifier.
32pub type csmVersion = c_uint;
33
34/// Moc file format version.
35pub type csmMocVersion = c_uint;
36
37/// Bitfield flags.
38pub type csmFlags = c_uchar;
39
40/// Parameter type.
41pub type csmParameterType = c_int;
42
43// ── Vector types ────────────────────────────────────────────────────────
44
45/// 2-component vector (X, Y).
46#[repr(C)]
47#[derive(Debug, Clone, Copy, Default)]
48pub struct csmVector2 {
49    pub x: c_float,
50    pub y: c_float,
51}
52
53/// 4-component vector (X, Y, Z, W).
54#[repr(C)]
55#[derive(Debug, Clone, Copy, Default)]
56pub struct csmVector4 {
57    pub x: c_float,
58    pub y: c_float,
59    pub z: c_float,
60    pub w: c_float,
61}
62
63// ── Constants ───────────────────────────────────────────────────────────
64
65/// Required alignment for moc memory (bytes).
66pub const ALIGN_OF_MOC: usize = 64;
67
68/// Required alignment for model memory (bytes).
69pub const ALIGN_OF_MODEL: usize = 16;
70
71// Constant drawable flags (non-dynamic)
72pub const BLEND_ADDITIVE: csmFlags = 1 << 0;
73pub const BLEND_MULTIPLICATIVE: csmFlags = 1 << 1;
74pub const IS_DOUBLE_SIDED: csmFlags = 1 << 2;
75pub const IS_INVERTED_MASK: csmFlags = 1 << 3;
76
77// Dynamic drawable flags
78pub const IS_VISIBLE: csmFlags = 1 << 0;
79pub const VISIBILITY_DID_CHANGE: csmFlags = 1 << 1;
80pub const OPACITY_DID_CHANGE: csmFlags = 1 << 2;
81pub const DRAW_ORDER_DID_CHANGE: csmFlags = 1 << 3;
82pub const RENDER_ORDER_DID_CHANGE: csmFlags = 1 << 4;
83pub const VERTEX_POSITIONS_DID_CHANGE: csmFlags = 1 << 5;
84pub const BLEND_COLOR_DID_CHANGE: csmFlags = 1 << 6;
85
86// Moc versions
87pub const MOC_VERSION_UNKNOWN: csmMocVersion = 0;
88pub const MOC_VERSION_30: csmMocVersion = 1;
89pub const MOC_VERSION_33: csmMocVersion = 2;
90pub const MOC_VERSION_40: csmMocVersion = 3;
91pub const MOC_VERSION_42: csmMocVersion = 4;
92pub const MOC_VERSION_50: csmMocVersion = 5;
93pub const MOC_VERSION_53: csmMocVersion = 6;
94
95// Parameter types
96pub const PARAMETER_TYPE_NORMAL: csmParameterType = 0;
97pub const PARAMETER_TYPE_BLEND_SHAPE: csmParameterType = 1;
98
99/// Log handler function pointer.
100pub type csmLogFunction = Option<extern "C" fn(message: *const c_char)>;
101
102// ── FFI declarations ────────────────────────────────────────────────────
103
104extern "C" {
105    // ── Version ──
106
107    pub fn csmGetVersion() -> csmVersion;
108    pub fn csmGetLatestMocVersion() -> csmMocVersion;
109    pub fn csmGetMocVersion(address: *const c_void, size: c_uint) -> csmMocVersion;
110
111    // ── Consistency ──
112
113    pub fn csmHasMocConsistency(address: *mut c_void, size: c_uint) -> c_int;
114
115    // ── Logging ──
116
117    pub fn csmGetLogFunction() -> csmLogFunction;
118    pub fn csmSetLogFunction(handler: csmLogFunction);
119
120    // ── Moc ──
121
122    pub fn csmReviveMocInPlace(address: *mut c_void, size: c_uint) -> *mut csmMoc;
123
124    // ── Model ──
125
126    pub fn csmGetSizeofModel(moc: *const csmMoc) -> c_uint;
127    pub fn csmInitializeModelInPlace(
128        moc: *const csmMoc,
129        address: *mut c_void,
130        size: c_uint,
131    ) -> *mut csmModel;
132    pub fn csmUpdateModel(model: *mut csmModel);
133
134    /// SDK v5: model-level render orders (replaces csmGetDrawableRenderOrders).
135    pub fn csmGetRenderOrders(model: *const csmModel) -> *const c_int;
136
137    // ── Canvas ──
138
139    pub fn csmReadCanvasInfo(
140        model: *const csmModel,
141        out_size_in_pixels: *mut csmVector2,
142        out_origin_in_pixels: *mut csmVector2,
143        out_pixels_per_unit: *mut c_float,
144    );
145
146    // ── Parameters ──
147
148    pub fn csmGetParameterCount(model: *const csmModel) -> c_int;
149    pub fn csmGetParameterIds(model: *const csmModel) -> *const *const c_char;
150    pub fn csmGetParameterTypes(model: *const csmModel) -> *const csmParameterType;
151    pub fn csmGetParameterMinimumValues(model: *const csmModel) -> *const c_float;
152    pub fn csmGetParameterMaximumValues(model: *const csmModel) -> *const c_float;
153    pub fn csmGetParameterDefaultValues(model: *const csmModel) -> *const c_float;
154    pub fn csmGetParameterValues(model: *mut csmModel) -> *mut c_float;
155    pub fn csmGetParameterRepeats(model: *const csmModel) -> *const c_int;
156    pub fn csmGetParameterKeyCounts(model: *const csmModel) -> *const c_int;
157    pub fn csmGetParameterKeyValues(model: *const csmModel) -> *const *const c_float;
158
159    // ── Parts ──
160
161    pub fn csmGetPartCount(model: *const csmModel) -> c_int;
162    pub fn csmGetPartIds(model: *const csmModel) -> *const *const c_char;
163    pub fn csmGetPartOpacities(model: *mut csmModel) -> *mut c_float;
164    pub fn csmGetPartParentPartIndices(model: *const csmModel) -> *const c_int;
165    pub fn csmGetPartOffscreenIndices(model: *const csmModel) -> *const c_int;
166
167    // ── Drawables ──
168
169    pub fn csmGetDrawableCount(model: *const csmModel) -> c_int;
170    pub fn csmGetDrawableIds(model: *const csmModel) -> *const *const c_char;
171    pub fn csmGetDrawableConstantFlags(model: *const csmModel) -> *const csmFlags;
172    pub fn csmGetDrawableDynamicFlags(model: *const csmModel) -> *const csmFlags;
173    /// SDK v5: explicit blend mode per drawable.
174    pub fn csmGetDrawableBlendModes(model: *const csmModel) -> *const c_int;
175    pub fn csmGetDrawableTextureIndices(model: *const csmModel) -> *const c_int;
176    pub fn csmGetDrawableDrawOrders(model: *const csmModel) -> *const c_int;
177    pub fn csmGetDrawableOpacities(model: *const csmModel) -> *const c_float;
178    pub fn csmGetDrawableMaskCounts(model: *const csmModel) -> *const c_int;
179    pub fn csmGetDrawableMasks(model: *const csmModel) -> *const *const c_int;
180    pub fn csmGetDrawableVertexCounts(model: *const csmModel) -> *const c_int;
181    pub fn csmGetDrawableVertexPositions(model: *const csmModel) -> *const *const csmVector2;
182    pub fn csmGetDrawableVertexUvs(model: *const csmModel) -> *const *const csmVector2;
183    pub fn csmGetDrawableIndexCounts(model: *const csmModel) -> *const c_int;
184    pub fn csmGetDrawableIndices(model: *const csmModel) -> *const *const c_ushort;
185    pub fn csmGetDrawableMultiplyColors(model: *const csmModel) -> *const csmVector4;
186    pub fn csmGetDrawableScreenColors(model: *const csmModel) -> *const csmVector4;
187    pub fn csmGetDrawableParentPartIndices(model: *const csmModel) -> *const c_int;
188    pub fn csmResetDrawableDynamicFlags(model: *mut csmModel);
189
190    // ── Offscreens (SDK v5) ──
191
192    pub fn csmGetOffscreenCount(model: *const csmModel) -> c_int;
193    pub fn csmGetOffscreenBlendModes(model: *const csmModel) -> *const c_int;
194    pub fn csmGetOffscreenOpacities(model: *const csmModel) -> *const c_float;
195    pub fn csmGetOffscreenOwnerIndices(model: *const csmModel) -> *const c_int;
196    pub fn csmGetOffscreenMultiplyColors(model: *const csmModel) -> *const csmVector4;
197    pub fn csmGetOffscreenScreenColors(model: *const csmModel) -> *const csmVector4;
198    pub fn csmGetOffscreenMaskCounts(model: *const csmModel) -> *const c_int;
199    pub fn csmGetOffscreenMasks(model: *const csmModel) -> *const *const c_int;
200    pub fn csmGetOffscreenConstantFlags(model: *const csmModel) -> *const csmFlags;
201}