feat(graphics): implement ANativeWindow C-ABI types and pixel formats
This commit is contained in:
parent
ea9115fed8
commit
6ae2bcb091
4 changed files with 63 additions and 0 deletions
2
crates/molotov-core/src/graphics/mod.rs
Normal file
2
crates/molotov-core/src/graphics/mod.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
pub mod types;
|
||||
50
crates/molotov-core/src/graphics/types.rs
Normal file
50
crates/molotov-core/src/graphics/types.rs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
use std::ffi::c_void;
|
||||
|
||||
pub const WINDOW_FORMAT_RGBA_8888: i32 = 1;
|
||||
pub const WINDOW_FORMAT_RGBX_8888: i32 = 2;
|
||||
pub const WINDOW_FORMAT_RGB_565: i32 = 4;
|
||||
|
||||
#[repr(i32)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum WindowFormat {
|
||||
Rgba8888 = 1,
|
||||
Rgbx8888 = 2,
|
||||
Rgb565 = 4,
|
||||
}
|
||||
|
||||
impl WindowFormat {
|
||||
pub fn from_i32(val: i32) -> Option<Self> {
|
||||
match val {
|
||||
1 => Some(WindowFormat::Rgba8888),
|
||||
2 => Some(WindowFormat::Rgbx8888),
|
||||
4 => Some(WindowFormat::Rgb565),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn bytes_per_pixel(&self) -> usize {
|
||||
match self {
|
||||
WindowFormat::Rgba8888 | WindowFormat::Rgbx8888 => 4,
|
||||
WindowFormat::Rgb565 => 2,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
pub struct ARect {
|
||||
pub left: i32,
|
||||
pub top: i32,
|
||||
pub right: i32,
|
||||
pub bottom: i32,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
pub struct ANativeWindow_Buffer {
|
||||
pub width: i32,
|
||||
pub height: i32,
|
||||
pub stride: i32,
|
||||
pub format: i32,
|
||||
pub bits: *mut c_void,
|
||||
pub reserved: [u32; 6],
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
pub mod audio;
|
||||
pub mod dynamic;
|
||||
pub mod elf;
|
||||
pub mod graphics;
|
||||
pub mod input;
|
||||
pub mod jni;
|
||||
pub mod linker;
|
||||
|
|
|
|||
10
crates/molotov-core/tests/graphics_types_test.rs
Normal file
10
crates/molotov-core/tests/graphics_types_test.rs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
use molotov_core::graphics::types::{WindowFormat, WINDOW_FORMAT_RGBA_8888, WINDOW_FORMAT_RGBX_8888};
|
||||
|
||||
#[test]
|
||||
fn test_window_format_constants() {
|
||||
assert_eq!(WindowFormat::Rgba8888 as i32, WINDOW_FORMAT_RGBA_8888);
|
||||
assert_eq!(WindowFormat::Rgbx8888 as i32, WINDOW_FORMAT_RGBX_8888);
|
||||
assert_eq!(WindowFormat::from_i32(1), Some(WindowFormat::Rgba8888));
|
||||
assert_eq!(WindowFormat::from_i32(999), None);
|
||||
}
|
||||
Reference in a new issue