Skip to content

Commit

Permalink
Use latest glutin and winit versions on 'hello' example
Browse files Browse the repository at this point in the history
  • Loading branch information
dmbfm committed Feb 1, 2024
1 parent 816ef22 commit bbaeafe
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 41 deletions.
8 changes: 7 additions & 1 deletion examples/hello/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ edition = "2018"
glow = { path = "../../" }

[target.'cfg(not(any(target_arch = "wasm32")))'.dependencies]
glutin = { version = "0.28", optional = true }
glutin = { version = "0.31.2", optional = true }
glutin-winit = { version = "0.4.2", optional = true}
winit = { version = "0.29.10", features = ["rwh_05"], optional = true }
raw-window-handle = { version = "0.5", optional = true }
sdl2 = { version = "0.35", optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
web-sys = { version = "0.3", features=["HtmlCanvasElement", "WebGl2RenderingContext", "Window"] }
wasm-bindgen = { version = "0.2" }

[features]
glutin_winit = ["glutin", "glutin-winit", "winit", "raw-window-handle"]
124 changes: 84 additions & 40 deletions examples/hello/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use glow::*;
use glutin::config::GlConfig;

fn main() {
unsafe {
Expand All @@ -25,21 +26,78 @@ fn main() {
};

// Create a context from a glutin window on non-wasm32 targets
#[cfg(feature = "glutin")]
let (gl, shader_version, window, event_loop) = {
let event_loop = glutin::event_loop::EventLoop::new();
let window_builder = glutin::window::WindowBuilder::new()
#[cfg(feature = "glutin_winit")]
let (gl, gl_surface, gl_context, shader_version, _window, event_loop) = {
use glutin::{
config::ConfigTemplateBuilder,
context::{ContextApi, ContextAttributesBuilder, NotCurrentGlContext},
display::{GetGlDisplay, GlDisplay},
surface::{GlSurface, SwapInterval},
};
use glutin_winit::{DisplayBuilder, GlWindow};
use raw_window_handle::HasRawWindowHandle;
use std::num::NonZeroU32;

let event_loop = winit::event_loop::EventLoopBuilder::new().build().unwrap();
let window_builder = winit::window::WindowBuilder::new()
.with_title("Hello triangle!")
.with_inner_size(glutin::dpi::LogicalSize::new(1024.0, 768.0));
let window = glutin::ContextBuilder::new()
.with_vsync(true)
.build_windowed(window_builder, &event_loop)
.unwrap()
.make_current()
.with_inner_size(winit::dpi::LogicalSize::new(1024.0, 768.0));

let template = ConfigTemplateBuilder::new();

let display_builder = DisplayBuilder::new().with_window_builder(Some(window_builder));

let (window, gl_config) = display_builder
.build(&event_loop, template, |configs| {
configs
.reduce(|accum, config| {
if config.num_samples() > accum.num_samples() {
config
} else {
accum
}
})
.unwrap()
})
.unwrap();
let gl =
glow::Context::from_loader_function(|s| window.get_proc_address(s) as *const _);
(gl, "#version 410", window, event_loop)

let raw_window_handle = window.as_ref().map(|window| window.raw_window_handle());

let gl_display = gl_config.display();
let context_attributes = ContextAttributesBuilder::new()
.with_context_api(ContextApi::OpenGl(Some(glutin::context::Version {
major: 4,
minor: 10,
})))
.build(raw_window_handle);

let not_current_gl_context = gl_display
.create_context(&gl_config, &context_attributes)
.unwrap();

let window = window.unwrap();

let attrs = window.build_surface_attributes(Default::default());
let gl_surface = gl_display
.create_window_surface(&gl_config, &attrs)
.unwrap();

let gl_context = not_current_gl_context.make_current(&gl_surface).unwrap();

let gl = glow::Context::from_loader_function_cstr(|s| gl_display.get_proc_address(s));

gl_surface
.set_swap_interval(&gl_context, SwapInterval::Wait(NonZeroU32::new(1).unwrap()))
.unwrap();

(
gl,
gl_surface,
gl_context,
"#version 410",
window,
event_loop,
)
};

// Create a context from a sdl2 window
Expand Down Expand Up @@ -124,37 +182,23 @@ fn main() {

// We handle events differently between targets

#[cfg(feature = "glutin")]
#[cfg(feature = "glutin_winit")]
{
use glutin::event::{Event, WindowEvent};
use glutin::event_loop::ControlFlow;

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
match event {
Event::LoopDestroyed => {
return;
}
Event::MainEventsCleared => {
window.window().request_redraw();
}
Event::RedrawRequested(_) => {
gl.clear(glow::COLOR_BUFFER_BIT);
gl.draw_arrays(glow::TRIANGLES, 0, 3);
window.swap_buffers().unwrap();
}
Event::WindowEvent { ref event, .. } => match event {
WindowEvent::Resized(physical_size) => {
window.resize(*physical_size);
}
use glutin::prelude::GlSurface;
use winit::event::{Event, WindowEvent};
let _ = event_loop.run(move |event, elwt| {
if let Event::WindowEvent { event, .. } = event {
match event {
WindowEvent::CloseRequested => {
gl.delete_program(program);
gl.delete_vertex_array(vertex_array);
*control_flow = ControlFlow::Exit
elwt.exit();
}
WindowEvent::RedrawRequested => {
gl.clear(glow::COLOR_BUFFER_BIT);
gl.draw_arrays(glow::TRIANGLES, 0, 3);
gl_surface.swap_buffers(&gl_context).unwrap();
}
_ => (),
},
_ => (),
}
}
});
}
Expand Down

0 comments on commit bbaeafe

Please sign in to comment.