servicepoint-binding-c/src/commands/char_grid_command.rs
Vinzenz Schroeter 36f3d84dc8 a bunch of docs
2025-05-07 22:31:26 +02:00

99 lines
2.5 KiB
Rust

use crate::mem::{
heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove,
};
use servicepoint::{BitmapCommand, CharGrid, CharGridCommand, Origin, Packet};
use std::ptr::NonNull;
/// Show UTF-8 encoded text on the screen.
///
/// The passed [CharGrid] gets consumed.
///
/// Returns: a new [CharGridCommand] instance.
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_char_grid_new(
grid: NonNull<CharGrid>,
origin_x: usize,
origin_y: usize,
) -> NonNull<CharGridCommand> {
heap_move_nonnull(CharGridCommand {
grid: unsafe { heap_remove(grid) },
origin: Origin::new(origin_x, origin_y),
})
}
/// Moves the provided [CharGrid] into a new [CharGridCommand],
/// leaving other fields as their default values.
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_char_grid_from_grid(
grid: NonNull<CharGrid>,
) -> NonNull<CharGridCommand> {
heap_move_nonnull(unsafe { heap_remove(grid) }.into())
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_char_grid_into_packet(
command: NonNull<CharGridCommand>,
) -> *mut Packet {
heap_move_ok(unsafe { heap_remove(command) }.try_into())
}
/// Clones an [CharGridCommand] instance.
///
/// returns: a new [CharGridCommand] instance.
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_char_grid_clone(
command: NonNull<CharGridCommand>,
) -> NonNull<CharGridCommand> {
unsafe { heap_clone(command) }
}
/// Deallocates a [BitmapCommand].
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_char_grid_free(
command: NonNull<BitmapCommand>,
) {
unsafe { heap_drop(command) }
}
/// Moves the provided bitmap to be contained in the command.
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_char_grid_set(
mut command: NonNull<CharGridCommand>,
grid: NonNull<CharGrid>,
) {
unsafe {
command.as_mut().grid = heap_remove(grid);
}
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_char_grid_get(
mut command: NonNull<CharGridCommand>,
) -> *mut CharGrid {
&mut unsafe { command.as_mut() }.grid
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_char_grid_get_origin(
command: NonNull<CharGridCommand>,
origin_x: NonNull<usize>,
origin_y: NonNull<usize>,
) {
unsafe {
let origin = &command.as_ref().origin;
*origin_x.as_ptr() = origin.x;
*origin_y.as_ptr() = origin.y;
}
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_char_grid_set_origin(
mut command: NonNull<CharGridCommand>,
origin_x: usize,
origin_y: usize,
) {
unsafe {
command.as_mut().origin = Origin::new(origin_x, origin_y);
}
}