wasm96-sdk 0.1.0

SDK for building WASM apps that run under the wasm96 libretro core.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#![cfg_attr(not(feature = "std"), no_std)]

//! wasm96-sdk (handwritten)
//!
//! This crate is used by **guest** WASM apps that run inside the `wasm96` libretro core.
//!
//! ABI model (Immediate Mode):
//! - Host owns the framebuffer and handles rendering.
//! - Guest issues drawing commands.
//! - Guest exports `setup`, `update`, and `draw`.
//!
//! This file intentionally contains **no WIT** and **no codegen**.

#[cfg(not(feature = "std"))]
extern crate alloc;

use core::ffi::c_void;

/// Joypad button ids.
#[repr(u32)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Button {
    B = 0,
    Y = 1,
    Select = 2,
    Start = 3,
    Up = 4,
    Down = 5,
    Left = 6,
    Right = 7,
    A = 8,
    X = 9,
    L1 = 10,
    R1 = 11,
    L2 = 12,
    R2 = 13,
    L3 = 14,
    R3 = 15,
}

/// Text size dimensions.
#[repr(C)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct TextSize {
    pub width: u32,
    pub height: u32,
}

/// Low-level raw ABI imports.
#[allow(non_camel_case_types)]
pub mod sys {
    unsafe extern "C" {
        // Graphics
        #[link_name = "wasm96_graphics_set_size"]
        pub fn graphics_set_size(width: u32, height: u32);
        #[link_name = "wasm96_graphics_set_color"]
        pub fn graphics_set_color(r: u32, g: u32, b: u32, a: u32);
        #[link_name = "wasm96_graphics_background"]
        pub fn graphics_background(r: u32, g: u32, b: u32);
        #[link_name = "wasm96_graphics_point"]
        pub fn graphics_point(x: i32, y: i32);
        #[link_name = "wasm96_graphics_line"]
        pub fn graphics_line(x1: i32, y1: i32, x2: i32, y2: i32);
        #[link_name = "wasm96_graphics_rect"]
        pub fn graphics_rect(x: i32, y: i32, w: u32, h: u32);
        #[link_name = "wasm96_graphics_rect_outline"]
        pub fn graphics_rect_outline(x: i32, y: i32, w: u32, h: u32);
        #[link_name = "wasm96_graphics_circle"]
        pub fn graphics_circle(x: i32, y: i32, r: u32);
        #[link_name = "wasm96_graphics_circle_outline"]
        pub fn graphics_circle_outline(x: i32, y: i32, r: u32);
        #[link_name = "wasm96_graphics_image"]
        pub fn graphics_image(x: i32, y: i32, w: u32, h: u32, ptr: u32, len: u32);
        #[link_name = "wasm96_graphics_image_png"]
        pub fn graphics_image_png(x: i32, y: i32, ptr: u32, len: u32);

        // --- Keyed resources (hashed keys) ---
        // SVG
        #[link_name = "wasm96_graphics_svg_register"]
        pub fn graphics_svg_register(key: u64, data_ptr: u32, data_len: u32) -> u32;
        #[link_name = "wasm96_graphics_svg_draw_key"]
        pub fn graphics_svg_draw_key(key: u64, x: i32, y: i32, w: u32, h: u32);
        #[link_name = "wasm96_graphics_svg_unregister"]
        pub fn graphics_svg_unregister(key: u64);

        // GIF
        #[link_name = "wasm96_graphics_gif_register"]
        pub fn graphics_gif_register(key: u64, data_ptr: u32, data_len: u32) -> u32;
        #[link_name = "wasm96_graphics_gif_draw_key"]
        pub fn graphics_gif_draw_key(key: u64, x: i32, y: i32);
        #[link_name = "wasm96_graphics_gif_draw_key_scaled"]
        pub fn graphics_gif_draw_key_scaled(key: u64, x: i32, y: i32, w: u32, h: u32);
        #[link_name = "wasm96_graphics_gif_unregister"]
        pub fn graphics_gif_unregister(key: u64);

        // PNG
        #[link_name = "wasm96_graphics_png_register"]
        pub fn graphics_png_register(key: u64, data_ptr: u32, data_len: u32) -> u32;
        #[link_name = "wasm96_graphics_png_draw_key"]
        pub fn graphics_png_draw_key(key: u64, x: i32, y: i32);
        #[link_name = "wasm96_graphics_png_draw_key_scaled"]
        pub fn graphics_png_draw_key_scaled(key: u64, x: i32, y: i32, w: u32, h: u32);
        #[link_name = "wasm96_graphics_png_unregister"]
        pub fn graphics_png_unregister(key: u64);

        // Fonts + text (keyed by string)
        #[link_name = "wasm96_graphics_font_register_ttf"]
        pub fn graphics_font_register_ttf(key: u64, data_ptr: u32, data_len: u32) -> u32;
        #[link_name = "wasm96_graphics_font_register_spleen"]
        pub fn graphics_font_register_spleen(key: u64, size: u32) -> u32;
        #[link_name = "wasm96_graphics_font_unregister"]
        pub fn graphics_font_unregister(key: u64);

        #[link_name = "wasm96_graphics_text_key"]
        pub fn graphics_text_key(x: i32, y: i32, font_key: u64, text_ptr: u32, text_len: u32);

        #[link_name = "wasm96_graphics_text_measure_key"]
        pub fn graphics_text_measure_key(font_key: u64, text_ptr: u32, text_len: u32) -> u64;

        #[link_name = "wasm96_graphics_triangle"]
        pub fn graphics_triangle(x1: i32, y1: i32, x2: i32, y2: i32, x3: i32, y3: i32);

        #[link_name = "wasm96_graphics_triangle_outline"]
        pub fn graphics_triangle_outline(x1: i32, y1: i32, x2: i32, y2: i32, x3: i32, y3: i32);

        #[link_name = "wasm96_graphics_bezier_quadratic"]
        pub fn graphics_bezier_quadratic(
            x1: i32,
            y1: i32,
            cx: i32,
            cy: i32,
            x2: i32,
            y2: i32,
            segments: u32,
        );

        #[link_name = "wasm96_graphics_bezier_cubic"]
        pub fn graphics_bezier_cubic(
            x1: i32,
            y1: i32,
            cx1: i32,
            cy1: i32,
            cx2: i32,
            cy2: i32,
            x2: i32,
            y2: i32,
            segments: u32,
        );

        #[link_name = "wasm96_graphics_pill"]
        pub fn graphics_pill(x: i32, y: i32, w: u32, h: u32);

        #[link_name = "wasm96_graphics_pill_outline"]
        pub fn graphics_pill_outline(x: i32, y: i32, w: u32, h: u32);

        // Input
        #[link_name = "wasm96_input_is_button_down"]
        pub fn input_is_button_down(port: u32, btn: u32) -> u32;
        #[link_name = "wasm96_input_is_key_down"]
        pub fn input_is_key_down(key: u32) -> u32;
        #[link_name = "wasm96_input_get_mouse_x"]
        pub fn input_get_mouse_x() -> i32;
        #[link_name = "wasm96_input_get_mouse_y"]
        pub fn input_get_mouse_y() -> i32;
        #[link_name = "wasm96_input_is_mouse_down"]
        pub fn input_is_mouse_down(btn: u32) -> u32;

        // Audio
        #[link_name = "wasm96_audio_init"]
        pub fn audio_init(sample_rate: u32) -> u32;
        #[link_name = "wasm96_audio_push_samples"]
        pub fn audio_push_samples(ptr: u32, len: u32);

        #[link_name = "wasm96_audio_play_wav"]
        pub fn audio_play_wav(ptr: u32, len: u32);

        #[link_name = "wasm96_audio_play_qoa"]
        pub fn audio_play_qoa(ptr: u32, len: u32);

        #[link_name = "wasm96_audio_play_xm"]
        pub fn audio_play_xm(ptr: u32, len: u32);

        // System
        #[link_name = "wasm96_system_log"]
        pub fn system_log(ptr: u32, len: u32);
        #[link_name = "wasm96_system_millis"]
        pub fn system_millis() -> u64;
    }
}

/// Graphics API.
pub mod graphics {
    use super::sys;
    use crate::TextSize;

    fn hash_key(key: &str) -> u64 {
        let mut hash: u64 = 0xcbf29ce484222325;
        for byte in key.bytes() {
            hash ^= byte as u64;
            hash = hash.wrapping_mul(0x100000001b3);
        }
        hash
    }

    /// Set the screen dimensions.
    pub fn set_size(width: u32, height: u32) {
        unsafe { sys::graphics_set_size(width, height) }
    }

    /// Set the current drawing color (RGBA).
    pub fn set_color(r: u8, g: u8, b: u8, a: u8) {
        unsafe { sys::graphics_set_color(r as u32, g as u32, b as u32, a as u32) }
    }

    /// Clear the screen with a specific color (RGB).
    pub fn background(r: u8, g: u8, b: u8) {
        unsafe { sys::graphics_background(r as u32, g as u32, b as u32) }
    }

    /// Draw a single pixel at (x, y).
    pub fn point(x: i32, y: i32) {
        unsafe { sys::graphics_point(x, y) }
    }

    /// Draw a line from (x1, y1) to (x2, y2).
    pub fn line(x1: i32, y1: i32, x2: i32, y2: i32) {
        unsafe { sys::graphics_line(x1, y1, x2, y2) }
    }

    /// Draw a filled rectangle.
    pub fn rect(x: i32, y: i32, w: u32, h: u32) {
        unsafe { sys::graphics_rect(x, y, w, h) }
    }

    /// Draw a rectangle outline.
    pub fn rect_outline(x: i32, y: i32, w: u32, h: u32) {
        unsafe { sys::graphics_rect_outline(x, y, w, h) }
    }

    /// Draw a filled circle.
    pub fn circle(x: i32, y: i32, r: u32) {
        unsafe { sys::graphics_circle(x, y, r) }
    }

    /// Draw a circle outline.
    pub fn circle_outline(x: i32, y: i32, r: u32) {
        unsafe { sys::graphics_circle_outline(x, y, r) }
    }

    /// Draw an image/sprite.
    /// `data` is a slice of RGBA bytes (4 bytes per pixel).
    pub fn image(x: i32, y: i32, w: u32, h: u32, data: &[u8]) {
        unsafe { sys::graphics_image(x, y, w, h, data.as_ptr() as u32, data.len() as u32) }
    }

    /// Draw an image from raw PNG bytes.
    pub fn image_png(x: i32, y: i32, data: &[u8]) {
        unsafe { sys::graphics_image_png(x, y, data.as_ptr() as u32, data.len() as u32) }
    }

    /// Register an encoded PNG (bytes) with the host under a string key.
    /// Register a GIF resource (encoded bytes) under a string key.
    /// Returns true on success.
    pub fn gif_register(key: &str, gif_bytes: &[u8]) -> bool {
        unsafe {
            sys::graphics_gif_register(
                hash_key(key),
                gif_bytes.as_ptr() as u32,
                gif_bytes.len() as u32,
            ) != 0
        }
    }

    /// Draw a registered GIF by key at natural size.
    pub fn gif_draw_key(key: &str, x: i32, y: i32) {
        unsafe { sys::graphics_gif_draw_key(hash_key(key), x, y) }
    }

    /// Draw a registered GIF by key scaled.
    pub fn gif_draw_key_scaled(key: &str, x: i32, y: i32, w: u32, h: u32) {
        unsafe { sys::graphics_gif_draw_key_scaled(hash_key(key), x, y, w, h) }
    }

    /// Unregister a GIF by key.
    pub fn gif_unregister(key: &str) {
        unsafe { sys::graphics_gif_unregister(hash_key(key)) }
    }

    /// Draw a filled triangle.
    pub fn triangle(x1: i32, y1: i32, x2: i32, y2: i32, x3: i32, y3: i32) {
        unsafe { sys::graphics_triangle(x1, y1, x2, y2, x3, y3) }
    }

    /// Draw a triangle outline.
    pub fn triangle_outline(x1: i32, y1: i32, x2: i32, y2: i32, x3: i32, y3: i32) {
        unsafe { sys::graphics_triangle_outline(x1, y1, x2, y2, x3, y3) }
    }

    /// Draw a quadratic Bezier curve.
    pub fn bezier_quadratic(x1: i32, y1: i32, cx: i32, cy: i32, x2: i32, y2: i32, segments: u32) {
        unsafe { sys::graphics_bezier_quadratic(x1, y1, cx, cy, x2, y2, segments) }
    }

    /// Draw a cubic Bezier curve.
    pub fn bezier_cubic(
        x1: i32,
        y1: i32,
        cx1: i32,
        cy1: i32,
        cx2: i32,
        cy2: i32,
        x2: i32,
        y2: i32,
        segments: u32,
    ) {
        unsafe { sys::graphics_bezier_cubic(x1, y1, cx1, cy1, cx2, cy2, x2, y2, segments) }
    }

    /// Draw a filled pill.
    pub fn pill(x: i32, y: i32, w: u32, h: u32) {
        unsafe { sys::graphics_pill(x, y, w, h) }
    }

    /// Draw a pill outline.
    pub fn pill_outline(x: i32, y: i32, w: u32, h: u32) {
        unsafe { sys::graphics_pill_outline(x, y, w, h) }
    }

    /// Register an SVG resource (encoded bytes) under a string key.
    /// Returns true on success.
    pub fn svg_register(key: &str, svg_bytes: &[u8]) -> bool {
        unsafe {
            sys::graphics_svg_register(
                hash_key(key),
                svg_bytes.as_ptr() as u32,
                svg_bytes.len() as u32,
            ) != 0
        }
    }

    /// Draw a keyed SVG.
    pub fn svg_draw_key(key: &str, x: i32, y: i32, w: u32, h: u32) {
        unsafe { sys::graphics_svg_draw_key(hash_key(key), x, y, w, h) }
    }

    /// Unregister a keyed SVG.
    pub fn svg_unregister(key: &str) {
        unsafe { sys::graphics_svg_unregister(hash_key(key)) }
    }

    /// Register a PNG resource (encoded bytes) under a string key.
    /// Returns true on success.
    pub fn png_register(key: &str, png_bytes: &[u8]) -> bool {
        unsafe {
            sys::graphics_png_register(
                hash_key(key),
                png_bytes.as_ptr() as u32,
                png_bytes.len() as u32,
            ) != 0
        }
    }

    /// Draw a registered PNG by key at natural size.
    pub fn png_draw_key(key: &str, x: i32, y: i32) {
        unsafe { sys::graphics_png_draw_key(hash_key(key), x, y) }
    }

    /// Draw a registered PNG by key scaled.
    pub fn png_draw_key_scaled(key: &str, x: i32, y: i32, w: u32, h: u32) {
        unsafe { sys::graphics_png_draw_key_scaled(hash_key(key), x, y, w, h) }
    }

    /// Unregister a PNG by key.
    pub fn png_unregister(key: &str) {
        unsafe { sys::graphics_png_unregister(hash_key(key)) }
    }

    /// Register a TTF font under a string key.
    ///
    /// Returns `true` if successful.
    pub fn font_register_ttf(key: &str, data: &[u8]) -> bool {
        unsafe {
            sys::graphics_font_register_ttf(hash_key(key), data.as_ptr() as u32, data.len() as u32)
                != 0
        }
    }

    /// Register the built-in Spleen font under a string key.
    ///
    /// Returns `true` if successful.
    pub fn font_register_spleen(key: &str, size: u32) -> bool {
        unsafe { sys::graphics_font_register_spleen(hash_key(key), size) != 0 }
    }

    /// Unregister a font.
    pub fn font_unregister(key: &str) {
        unsafe { sys::graphics_font_unregister(hash_key(key)) }
    }

    /// Draw text using a registered font.
    pub fn text_key(x: i32, y: i32, font_key: &str, text: &str) {
        unsafe {
            sys::graphics_text_key(
                x,
                y,
                hash_key(font_key),
                text.as_ptr() as u32,
                text.len() as u32,
            )
        }
    }

    /// Measure text using a registered font.
    pub fn text_measure_key(font_key: &str, text: &str) -> TextSize {
        let packed = unsafe {
            sys::graphics_text_measure_key(
                hash_key(font_key),
                text.as_ptr() as u32,
                text.len() as u32,
            )
        };
        TextSize {
            width: (packed >> 32) as u32,
            height: packed as u32,
        }
    }
}

/// Input API.
pub mod input {
    use super::{Button, sys};

    /// Returns true if the specified button is currently held down.
    pub fn is_button_down(port: u32, btn: Button) -> bool {
        unsafe { sys::input_is_button_down(port, btn as u32) != 0 }
    }

    /// Returns true if the specified key is currently held down.
    pub fn is_key_down(key: u32) -> bool {
        unsafe { sys::input_is_key_down(key) != 0 }
    }

    /// Get current mouse X position.
    pub fn get_mouse_x() -> i32 {
        unsafe { sys::input_get_mouse_x() }
    }

    /// Get current mouse Y position.
    pub fn get_mouse_y() -> i32 {
        unsafe { sys::input_get_mouse_y() }
    }

    /// Returns true if the specified mouse button is held down.
    /// 0 = Left, 1 = Right, 2 = Middle.
    pub fn is_mouse_down(btn: u32) -> bool {
        unsafe { sys::input_is_mouse_down(btn) != 0 }
    }
}

/// Audio API.
pub mod audio {
    use super::sys;

    /// Initialize audio system.
    pub fn init(sample_rate: u32) -> u32 {
        unsafe { sys::audio_init(sample_rate) }
    }

    /// Push a chunk of audio samples.
    /// Samples are interleaved stereo (L, R, L, R...) signed 16-bit integers.
    pub fn push_samples(samples: &[i16]) {
        unsafe { sys::audio_push_samples(samples.as_ptr() as u32, samples.len() as u32) }
    }

    /// Play a WAV file.
    /// The WAV data is decoded and played as a one-shot audio channel.
    pub fn play_wav(data: &[u8]) {
        unsafe { sys::audio_play_wav(data.as_ptr() as u32, data.len() as u32) }
    }

    /// Play a QOA file.
    /// The QOA data is decoded and played as a looping audio channel.
    pub fn play_qoa(data: &[u8]) {
        unsafe { sys::audio_play_qoa(data.as_ptr() as u32, data.len() as u32) }
    }

    /// Play an XM file.
    /// Play an XM file.
    /// The XM data is decoded using xmrsplayer and played as a looping audio channel.
    pub fn play_xm(data: &[u8]) {
        unsafe { sys::audio_play_xm(data.as_ptr() as u32, data.len() as u32) }
    }
}

/// System API.
pub mod system {
    use super::sys;

    /// Log a message to the host console.
    pub fn log(message: &str) {
        unsafe { sys::system_log(message.as_ptr() as u32, message.len() as u32) }
    }

    /// Get the number of milliseconds since the app started.
    pub fn millis() -> u64 {
        unsafe { sys::system_millis() }
    }
}

/// Convenience prelude for guest apps.
pub mod prelude {
    pub use crate::Button;
    pub use crate::TextSize;
    pub use crate::audio;
    pub use crate::graphics;
    pub use crate::input;
    pub use crate::system;
}

// Keep `c_void` referenced so it doesn't look unused in some configurations.
#[allow(dead_code)]
const _C_VOID: *const c_void = core::ptr::null();