feat(android): basic android support via winit native activity

This commit is contained in:
Chance 2025-04-17 23:04:24 -04:00 committed by lily
parent 19e13d697f
commit 69bf41c684
Signed by: lily
GPG key ID: 601F3263FBCBC4B9
3 changed files with 37 additions and 8 deletions

1
src/lib.rs Normal file
View file

@ -0,0 +1 @@
include!("main.rs");

View file

@ -1,7 +1,6 @@
use bytemuck::bytes_of;
use std::collections::BTreeMap;
use std::sync::Arc;
use bytemuck::bytes_of;
use tracing::info;
use wgpu::util::DeviceExt;
use wgpu::{
@ -11,6 +10,8 @@ use wgpu::{
use winit::application::ApplicationHandler;
use winit::event::{ElementState, MouseButton};
use winit::event_loop::{ActiveEventLoop, EventLoop};
#[cfg(target_os = "android")]
use winit::platform::android::activity::AndroidApp;
use winit::window::{Window, WindowAttributes, WindowId};
use zlog::LogLevel;
use zlog::config::LoggerConfig;
@ -493,8 +494,7 @@ impl ApplicationHandler for App<'_> {
}
}
}
fn main() -> Result<(), terminator::Terminator> {
pub fn main() -> Result<(), terminator::Terminator> {
let config = LoggerConfig::default()
.colored_stdout(true)
.log_to_stdout(true)
@ -503,11 +503,33 @@ fn main() -> Result<(), terminator::Terminator> {
.level(LogLevel::Info)
.log_path("zenyx.log");
let _logger = zlog::Logger::new(config);
let event_loop = EventLoop::new()?;
event_loop.set_control_flow(winit::event_loop::ControlFlow::Poll);
#[cfg(not(target_os = "android"))]
{
_main()
}
}
pub fn run_app(event_loop: winit::event_loop::EventLoop<()>) -> Result<(), terminator::Terminator> {
let mut app = App::new();
event_loop.set_control_flow(winit::event_loop::ControlFlow::Poll);
event_loop.run_app(&mut app)?;
Ok(())
}
#[unsafe(no_mangle)]
#[cfg(target_os = "android")]
extern "C" fn android_main(app: AndroidApp) {
use winit::event_loop::EventLoopBuilder;
use winit::platform::android::EventLoopBuilderExtAndroid;
let event_loop = EventLoopBuilder::default()
.with_android_app(app)
.build()
.unwrap();
run_app(event_loop).unwrap()
}
fn _main() -> Result<(), terminator::Terminator> {
let event_loop = EventLoop::new()?;
run_app(event_loop)?;
info!("Exiting...");
Ok(())
}