Skip to content

Commit

Permalink
Update window example.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvc94ch committed Feb 19, 2020
1 parent a792317 commit ad4ae79
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 18 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ 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.
To compile the examples for android, you have to use the `cargo apk` utility. You can run an example
with `cargo apk run --example $EXAMPLE --manifest-path android/Cargo.toml`.

See [the `android-ndk-rs` repository](https://github.com/rust-windowing/android-ndk-rs) for instructions.
See [the `android-ndk-rs` repository](https://github.com/rust-windowing/android-ndk-rs) for more information.

### Emscripten with asmjs

Expand Down
26 changes: 26 additions & 0 deletions android/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "android-examples"
version = "0.1.0"
authors = ["David Craven <david@craven.ch>"]
edition = "2018"
publish = false
build = "../build.rs"

[dependencies]
glutin = { path = ".." }

[build-dependencies]
gl_generator = "0.14.0"

[dev-dependencies]
image = "0.22.4"
ndk-glue = { path = "../../android-ndk-rs/ndk-glue" }
simple_logger = "1.4.0"
takeable-option = "0.5.0"
winit = { path = "../../winit" }
winit_types = { path = "../../winit_types" }

[[example]]
name = "window"
path = "../examples/window.rs"
crate-type = ["cdylib"]
Empty file added android/src/lib.rs
Empty file.
57 changes: 41 additions & 16 deletions examples/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,70 @@ use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;

#[cfg(target_os = "android")]
ndk_glue::ndk_glue!(main);

fn main() {
simple_logger::init().unwrap();
let el = EventLoop::new();
let wb = WindowBuilder::new().with_title("A fantastic window!");

let confs = unsafe { ConfigsFinder::new().find(&*el).unwrap() };
let conf = &confs[0];
let conf = confs[0].clone();
println!("Configeration chosen: {:?}", conf);

let ctx = unsafe { ContextBuilder::new().build(conf).unwrap() };
let (win, surf) = unsafe { Surface::new_window(conf, &*el, wb).unwrap() };

unsafe { ctx.make_current(&surf).unwrap() }
let gl = support::Gl::load(|s| ctx.get_proc_address(s).unwrap());
let ctx = unsafe { ContextBuilder::new().build(&conf).unwrap() };
let win = unsafe { Surface::build_window(&conf, &*el, wb).unwrap() };

// On android the surface can only be created after the resume event
// was received.
let mut surf = None;
let mut gl = None;
el.run(move |event, _, control_flow| {
println!("{:?}", event);
*control_flow = ControlFlow::Wait;

match event {
Event::LoopDestroyed => return,
Event::MainEventsCleared => {
win.request_redraw();
Event::Resumed => {
let surface = unsafe { Surface::new_from_existing_window(&conf, &win).unwrap() };
unsafe {
ctx.make_current(&surface).unwrap();
}
if gl.is_none() {
gl = Some(support::Gl::load(|s| ctx.get_proc_address(s).unwrap()));
}
surf = Some(surface)
}
Event::Suspended => surf = None,
Event::LoopDestroyed => return,
Event::MainEventsCleared => win.request_redraw(),
Event::RedrawRequested(_) => {
gl.draw_frame([1.0, 0.5, 0.7, 1.0]);
surf.swap_buffers().unwrap();
if let (Some(gl), Some(surf)) = (&gl, &surf) {
gl.draw_frame([1.0, 0.5, 0.7, 1.0]);
surf.swap_buffers().unwrap();
}
}
Event::WindowEvent { ref event, .. } => match event {
WindowEvent::Resized(size) => {
Event::WindowEvent { ref event, .. } => {
let size = match event {
WindowEvent::ScaleFactorChanged {
new_inner_size: size,
..
} => size,
WindowEvent::Resized(size) => size,
WindowEvent::CloseRequested => {
*control_flow = ControlFlow::Exit;
return;
}
_ => return,
};
if let (Some(gl), Some(surf)) = (&gl, &surf) {
ctx.update_after_resize();
surf.update_after_resize(*size);
unsafe {
gl.gl.Viewport(0, 0, size.width as _, size.height as _);
}
}
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
_ => (),
},
}
_ => (),
}
});
Expand Down

0 comments on commit ad4ae79

Please sign in to comment.