-
-
Notifications
You must be signed in to change notification settings - Fork 477
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
164 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
use std::num::NonZeroU32; | ||
|
||
use raw_window_handle::HasRawDisplayHandle; | ||
|
||
use winit::event::{Event, WindowEvent}; | ||
use winit::event_loop::{ControlFlow, EventLoop}; | ||
use winit::window::WindowBuilder; | ||
|
||
use glutin::config::ConfigTemplate; | ||
use glutin::context::ContextAttributes; | ||
use glutin::prelude::*; | ||
use glutin::surface::SwapInterval; | ||
|
||
mod support; | ||
|
||
use support::*; | ||
|
||
#[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "on"))] | ||
fn main() { | ||
let event_loop = EventLoop::new(); | ||
|
||
let raw_display = event_loop.raw_display_handle(); | ||
|
||
// Create the GL display. This will create display automatically for the | ||
// underlying GL platform. See support module on how it's being done. | ||
let gl_display = create_display(raw_display, None); | ||
|
||
let template = ConfigTemplate::default(); | ||
let config = unsafe { gl_display.find_configs(template) }.unwrap().next().unwrap(); | ||
|
||
// The context creation part. It can be created before surface and that's how | ||
// it's expected in multithreaded + multiwindow operation mode, since you | ||
// can send NotCurrentContext, but not Surface. | ||
let context_attributes = ContextAttributes::default(); | ||
let mut not_current_gl_context = | ||
Some(unsafe { gl_display.create_context(&config, &context_attributes) }.unwrap()); | ||
|
||
let mut state = None; | ||
let mut renderer = None; | ||
|
||
event_loop.run(move |event, event_loop_window_target, control_flow| { | ||
*control_flow = ControlFlow::Wait; | ||
match event { | ||
Event::Resumed => { | ||
println!("Android window available"); | ||
|
||
let window = WindowBuilder::new().build(event_loop_window_target).unwrap(); | ||
|
||
// Create a wrapper for GL window and surface. | ||
let gl_window = GlWindow::from_existing(&gl_display, window, &config); | ||
|
||
// Make it current. | ||
let gl_context = not_current_gl_context | ||
.take() | ||
.unwrap() | ||
.make_current(&gl_window.surface) | ||
.unwrap(); | ||
|
||
// The renderer only needs to be initialized once, but needs the context to be current | ||
// (which in turn requires a window). | ||
renderer.get_or_insert_with(|| Renderer::new(&gl_display)); | ||
|
||
// Try setting vsync. | ||
if let Err(res) = gl_window | ||
.surface | ||
.set_swap_interval(&gl_context, SwapInterval::Wait(NonZeroU32::new(1).unwrap())) | ||
{ | ||
eprintln!("Error setting vsync: {:?}", res); | ||
} | ||
|
||
assert!(state.replace((gl_context, gl_window)).is_none()); | ||
}, | ||
Event::Suspended => { | ||
println!("Android window removed"); | ||
// Destroy the GL Surface and un-current the GL Context before ndk-glue releases | ||
// the window back to the system. | ||
let (gl_context, _) = state.take().unwrap(); | ||
assert!(not_current_gl_context | ||
.replace(gl_context.make_not_current().unwrap()) | ||
.is_none()); | ||
}, | ||
|
||
Event::WindowEvent { event, .. } => match event { | ||
WindowEvent::Resized(size) => { | ||
if size.width != 0 && size.height != 0 { | ||
// Some platforms like EGL require resizing GL surface to update the size | ||
// Notable platforms here are Wayland and macOS, other don't require it | ||
// and the function is no-op, but it's wise to resize it for portability | ||
// reasons. | ||
if let Some((gl_context, gl_window)) = &state { | ||
gl_window.surface.resize( | ||
gl_context, | ||
NonZeroU32::new(size.width).unwrap(), | ||
NonZeroU32::new(size.height).unwrap(), | ||
); | ||
let renderer = renderer.as_ref().unwrap(); | ||
renderer.resize(size.width as i32, size.height as i32); | ||
} | ||
} | ||
}, | ||
WindowEvent::CloseRequested => { | ||
*control_flow = ControlFlow::Exit; | ||
}, | ||
_ => (), | ||
}, | ||
Event::RedrawEventsCleared => { | ||
if let Some((gl_context, gl_window)) = &state { | ||
let renderer = renderer.as_ref().unwrap(); | ||
renderer.draw(); | ||
gl_window.window.request_redraw(); | ||
|
||
gl_window.surface.swap_buffers(gl_context).unwrap(); | ||
} | ||
}, | ||
_ => (), | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters