forked from glium/glium
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
c0753ae
commit 5ca9edc
Showing
4 changed files
with
147 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Performances | ||
# Performance | ||
|
||
Here is the estimated cost of various operations: | ||
|
||
|
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,39 @@ | ||
# The event loop | ||
|
||
Before we continue animating things we need to make a short detour to make animation possible. | ||
To simplify things we were only ever rendering a single frame during startup. | ||
To change this we need to alter the structure a little bit, but first we should put the code that creates the context into a separate function. We'll use the following function in the following tutorials: | ||
|
||
```rust | ||
fn create_window_and_display(title: &str) -> (window: winit::Window, display: glium::Display<WindowSurface>) { | ||
// First we start by opening a new Window | ||
let window_builder = WindowBuilder::new().with_title(title); | ||
let display_builder = DisplayBuilder::new().with_window_builder(Some(window_builder)); | ||
let config_template_builder = ConfigTemplateBuilder::new(); | ||
let (window, gl_config) = display_builder | ||
.build(&event_loop, config_template_builder, |mut configs| { | ||
// Just use the first configuration since we don't have any special preferences right now | ||
configs.next().unwrap() | ||
}) | ||
.unwrap(); | ||
let window = window.unwrap(); | ||
|
||
// Now we get the window size to use as the initial size of the Surface | ||
let (width, height): (u32, u32) = window.inner_size().into(); | ||
let attrs = glutin::surface::SurfaceAttributesBuilder::<WindowSurface>::new().build( | ||
window.raw_window_handle(), | ||
NonZeroU32::new(width).unwrap(), | ||
NonZeroU32::new(height).unwrap(), | ||
); | ||
|
||
// Finally we can create a Surface, use it to make a PossiblyCurrentContext and create the glium Display | ||
let surface = unsafe { gl_config.display().create_window_surface(&gl_config, &attrs).unwrap() }; | ||
let context_attributes = glutin::context::ContextAttributesBuilder::new().build(Some(window.raw_window_handle())); | ||
let current_context = Some(unsafe { | ||
gl_config.display().create_context(&gl_config, &context_attributes).expect("failed to create context") | ||
}).unwrap().make_current(&surface).unwrap(); | ||
let display = glium::Display::from_context_surface(current_context, surface).unwrap(); | ||
|
||
(window, display) | ||
} | ||
``` |
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,105 @@ | ||
#[macro_use] | ||
extern crate glium; | ||
|
||
use winit::event_loop::EventLoopBuilder; | ||
use winit::window::WindowBuilder; | ||
use glutin_winit::DisplayBuilder; | ||
use glutin::config::ConfigTemplateBuilder; | ||
use glutin::prelude::*; | ||
use glutin::display::GetGlDisplay; | ||
use glutin::surface::WindowSurface; | ||
use raw_window_handle::HasRawWindowHandle; | ||
use std::num::NonZeroU32; | ||
use glium::Surface; | ||
|
||
fn main() { | ||
// We start by creating the EventLoop, this can only be done once per process. | ||
// This also needs to happen on the main thread to make the program portable. | ||
let event_loop = EventLoopBuilder::new().build(); | ||
let (_window, display) = create_window_and_display(&event_loop, "Glium tutorial #3"); | ||
|
||
#[derive(Copy, Clone)] | ||
struct Vertex { | ||
position: [f32; 2], | ||
} | ||
implement_vertex!(Vertex, position); | ||
|
||
let vertex1 = Vertex { position: [-0.5, -0.5] }; | ||
let vertex2 = Vertex { position: [ 0.0, 0.5] }; | ||
let vertex3 = Vertex { position: [ 0.5, -0.25] }; | ||
let shape = vec![vertex1, vertex2, vertex3]; | ||
let vertex_buffer = glium::VertexBuffer::new(&display, &shape).unwrap(); | ||
let indices = glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList); | ||
|
||
let vertex_shader_src = r#" | ||
#version 140 | ||
in vec2 position; | ||
void main() { | ||
gl_Position = vec4(position, 0.0, 1.0); | ||
} | ||
"#; | ||
|
||
let fragment_shader_src = r#" | ||
#version 140 | ||
out vec4 color; | ||
void main() { | ||
color = vec4(1.0, 0.0, 0.0, 1.0); | ||
} | ||
"#; | ||
|
||
let program = glium::Program::from_source(&display, vertex_shader_src, fragment_shader_src, None).unwrap(); | ||
|
||
let mut target = display.draw(); | ||
target.clear_color(0.0, 0.0, 1.0, 1.0); | ||
target.draw(&vertex_buffer, &indices, &program, &glium::uniforms::EmptyUniforms, | ||
&Default::default()).unwrap(); | ||
target.finish().unwrap(); | ||
|
||
event_loop.run(move |ev, _, control_flow| { | ||
match ev { | ||
winit::event::Event::WindowEvent { event, .. } => match event { | ||
winit::event::WindowEvent::CloseRequested => { | ||
*control_flow = winit::event_loop::ControlFlow::Exit; | ||
}, | ||
_ => (), | ||
}, | ||
_ => (), | ||
} | ||
}); | ||
} | ||
|
||
fn create_window_and_display(event_loop: &winit::event_loop::EventLoop<()>, title: &str) -> (winit::window::Window, glium::Display<WindowSurface>) { | ||
// First we start by opening a new Window | ||
let window_builder = WindowBuilder::new().with_title(title); | ||
let display_builder = DisplayBuilder::new().with_window_builder(Some(window_builder)); | ||
let config_template_builder = ConfigTemplateBuilder::new(); | ||
let (window, gl_config) = display_builder | ||
.build(event_loop, config_template_builder, |mut configs| { | ||
// Just use the first configuration since we don't have any special preferences right now | ||
configs.next().unwrap() | ||
}) | ||
.unwrap(); | ||
let window = window.unwrap(); | ||
|
||
// Now we get the window size to use as the initial size of the Surface | ||
let (width, height): (u32, u32) = window.inner_size().into(); | ||
let attrs = glutin::surface::SurfaceAttributesBuilder::<WindowSurface>::new().build( | ||
window.raw_window_handle(), | ||
NonZeroU32::new(width).unwrap(), | ||
NonZeroU32::new(height).unwrap(), | ||
); | ||
|
||
// Finally we can create a Surface, use it to make a PossiblyCurrentContext and create the glium Display | ||
let surface = unsafe { gl_config.display().create_window_surface(&gl_config, &attrs).unwrap() }; | ||
let context_attributes = glutin::context::ContextAttributesBuilder::new().build(Some(window.raw_window_handle())); | ||
let current_context = Some(unsafe { | ||
gl_config.display().create_context(&gl_config, &context_attributes).expect("failed to create context") | ||
}).unwrap().make_current(&surface).unwrap(); | ||
let display = glium::Display::from_context_surface(current_context, surface).unwrap(); | ||
|
||
(window, display) | ||
} |