Skip to content

Commit

Permalink
Use new NativeWindow::lock() API in dummy_render()
Browse files Browse the repository at this point in the history
  • Loading branch information
MarijnS95 committed Aug 26, 2023
1 parent 3ad2b59 commit c5954d6
Show file tree
Hide file tree
Showing 9 changed files with 615 additions and 270 deletions.
289 changes: 227 additions & 62 deletions agdk-mainloop/Cargo.lock

Large diffs are not rendered by default.

13 changes: 5 additions & 8 deletions agdk-mainloop/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ name = "agdk-mainloop"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
log = "0.4"
android-activity = { version = "0.5.0-beta.1", features = ["native-activity"] }
android_logger = "0.11.0"
android-activity = { version = "0.4", features = ["game-activity"] }
ndk-sys = "0.4"
ndk = "0.7"
log = "0.4"
ndk = "0.8.0-beta.0"

[lib]
name="main"
crate_type=["cdylib"]
name = "main"
crate_type = ["cdylib"]
32 changes: 21 additions & 11 deletions agdk-mainloop/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use android_activity::{AndroidApp, InputStatus, MainEvent, PollEvent};
use log::info;
use ndk::native_window::HardwareBufferFormat;

#[no_mangle]
fn android_main(app: AndroidApp) {
Expand Down Expand Up @@ -38,6 +39,16 @@ fn android_main(app: AndroidApp) {
}
MainEvent::InitWindow { .. } => {
native_window = app.native_window();
if let Some(nw) = &native_window {
// Set the backing buffer to a known format (without changing
// the size) so that we can safely draw to it in dummy_render().
nw.set_buffers_geometry(
0,
0,
Some(HardwareBufferFormat::R8G8B8A8_UNORM),
)
.unwrap()
}
redraw_pending = true;
}
MainEvent::TerminateWindow { .. } => {
Expand Down Expand Up @@ -91,16 +102,15 @@ fn android_main(app: AndroidApp) {
/// responsive, otherwise it will stop delivering input
/// events to us.
fn dummy_render(native_window: &ndk::native_window::NativeWindow) {
unsafe {
let mut buf: ndk_sys::ANativeWindow_Buffer = std::mem::zeroed();
let mut rect: ndk_sys::ARect = std::mem::zeroed();
ndk_sys::ANativeWindow_lock(
native_window.ptr().as_ptr() as _,
&mut buf as _,
&mut rect as _,
);
// Note: we don't try and touch the buffer since that
// also requires us to handle various buffer formats
ndk_sys::ANativeWindow_unlockAndPost(native_window.ptr().as_ptr() as _);
let mut lock = native_window.lock(None).unwrap();
let (w, h) = (lock.width(), lock.height());

for (y, line) in lock.lines().unwrap().enumerate() {
let r = y * 255 / h;
for (x, pixels) in line.chunks_mut(4).enumerate() {
let g = x * 255 / w;
pixels[0].write(r as u8);
pixels[1].write(g as u8);
}
}
}
Loading

0 comments on commit c5954d6

Please sign in to comment.