Skip to content

Commit

Permalink
Add Android example
Browse files Browse the repository at this point in the history
  • Loading branch information
MarijnS95 committed Jun 12, 2022
1 parent 38f7344 commit 3fb7531
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 8 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ cargo run --example window

## Common issues

Please refer to [ISSUES.md.](ISSUES.md)
Please refer to [ISSUES.md](ISSUES.md).

### Usage

Glutin is an OpenGL context creation library and doesn't directly provide OpenGL bindings for you.

For examples, please look [here.](https://github.com/rust-windowing/glutin/tree/master/glutin_examples)
For examples, please look [here](https://github.com/rust-windowing/glutin/tree/master/glutin_examples).

Note that glutin aims at being a low-level brick in your rendering infrastructure. You are encouraged to write another layer of abstraction between glutin and your application.

Expand All @@ -48,9 +48,13 @@ Glutin is only officially supported on the latest stable version of the Rust com

### Android

To compile the examples for android, you have to use the `cargo apk` utility.
Be sure to handle Android's lifecycle correctly when using a `winit` window by only creating a GL surface (currently entire context) after `winit` raises `Event::Resumed`, and destroy it again upon receiving `Event::Suspended`. See this in action in [the `android.rs` example](./glutin_examples/examples/android.rs).

See [`cargo-apk` in the `android-ndk-rs` repository](https://github.com/rust-windowing/android-ndk-rs/cargo-apk) for instructions.
To compile and run the Android example on your device, install [`cargo-apk`](https://crates.io/crates/cargo-apk) and start the app using:

```console
$ cargo apk r -p glutin_examples --example android
```

### X11

Expand Down
5 changes: 3 additions & 2 deletions glutin/src/api/egl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,8 @@ where
out.push(surface_type as raw::c_int);

match (api, version) {
(Api::OpenGlEs, Some((3, _))) => {
// TODO: `version` is `None` when GLRequest is Latest
(Api::OpenGlEs, None | Some((3, _))) => {
if egl_version < &(1, 3) {
return Err(CreationError::NoAvailablePixelFormat);
}
Expand Down Expand Up @@ -1017,7 +1018,7 @@ where
out.push(ffi::egl::CONFORMANT as raw::c_int);
out.push(ffi::egl::OPENGL_BIT as raw::c_int);
}
(_, _) => unimplemented!(),
(api, version) => unimplemented!("{:?} at version {:?}", api, version),
};

if let Some(hardware_accelerated) = pf_reqs.hardware_accelerated {
Expand Down
2 changes: 1 addition & 1 deletion glutin/src/api/wgl/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(any(target_os = "windows"))]
#![cfg(target_os = "windows")]

mod make_current_guard;

Expand Down
7 changes: 7 additions & 0 deletions glutin_examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ image = "0.21"

[build-dependencies]
gl_generator = "0.14"

[target.'cfg(target_os = "android")'.dependencies]
ndk-glue = "0.5" # Keep in sync with winit

[[example]]
name = "android"
crate-type = ["cdylib"]
61 changes: 61 additions & 0 deletions glutin_examples/examples/android.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#![cfg(target_os = "android")]

mod support;

use glutin::event::{Event, WindowEvent};
use glutin::event_loop::{ControlFlow, EventLoop};
use glutin::window::WindowBuilder;

#[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "on"))]
fn main() {
println!("App started");
let el = EventLoop::new();

// On Android a window is only available (ndk_glue::native_window() returns Some,
// to create an EGL context on) after Resumed is received, and disappears when
// Suspended is received.

let mut state = None;

el.run(move |event, el, control_flow| {
*control_flow = ControlFlow::Wait;

println!("{:?}", &event);

match event {
Event::Resumed => {
println!("Android window available");
let wb = WindowBuilder::new().with_title("Hello world!");
let windowed_context =
glutin::ContextBuilder::new().build_windowed(wb, el).unwrap();

let windowed_context = unsafe { windowed_context.make_current().unwrap() };

let gl = support::load(windowed_context.context());

state = Some((windowed_context, gl));
}
Event::Suspended => {
println!("Android window removed");
// Destroy the GL context before ndk-glue releases the window back to the system
state = None;
}
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => {
*control_flow = ControlFlow::Exit
}
Event::WindowEvent { event: WindowEvent::Resized(size), .. } => {
if let Some((windowed_context, _)) = &state {
windowed_context.resize(size);
windowed_context.window().request_redraw();
}
}
Event::RedrawRequested(_) => {
if let Some((windowed_context, gl)) = &state {
gl.draw_frame([1.0, 0.5, 0.7, 1.0]);
windowed_context.swap_buffers().unwrap();
}
}
_ => {}
}
});
}
2 changes: 1 addition & 1 deletion glutin_wgl_sys/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(any(target_os = "windows"))]
#![cfg(target_os = "windows")]

/// WGL bindings
pub mod wgl {
Expand Down

0 comments on commit 3fb7531

Please sign in to comment.