pub struct EventLoop<T>where
T: 'static,{ /* private fields */ }
Expand description
Provides a way to retrieve events from the system and from the windows that were registered to the events loop.
An EventLoop
can be seen more or less as a “context”. Calling EventLoop::new
initializes everything that will be required to create windows. For example on Linux creating
an event loop opens a connection to the X or Wayland server.
To wake up an EventLoop
from a another thread, see the EventLoopProxy
docs.
Note that this cannot be shared across threads (due to platform-dependant logic
forbidding it), as such it is neither Send
nor Sync
. If you need cross-thread access,
the Window
created from this can be sent to an other thread, and the
EventLoopProxy
allows you to wake up an EventLoop
from another thread.
Implementations§
Source§impl EventLoop<()>
impl EventLoop<()>
Sourcepub fn new() -> Result<EventLoop<()>, EventLoopError>
pub fn new() -> Result<EventLoop<()>, EventLoopError>
Create the event loop.
This is an alias of EventLoop::builder().build()
.
Examples found in repository?
More examples
9fn main() {
10 use Action::*;
11 let event_loop = EventLoop::new().unwrap();
12 event_loop.set_control_flow(ControlFlow::Poll);
13
14 let input = { use base_input_codes::*; input_map!(
15 (Jump, Space, GamepadInput::South),
16 // square brackets mean that all input codes must be pressed for the bind to be pressed
17 (Exit, [ControlLeft, Escape], GamepadInput::Start),
18 (Left, ArrowLeft, KeyA, LeftStickLeft ),
19 (Right, ArrowRight, KeyD, LeftStickRight),
20 (Forward, ArrowUp, KeyW, LeftStickUp ),
21 (Back, ArrowDown, KeyS, LeftStickDown ),
22 (LookRight, MouseMoveRight, RightStickRight),
23 (LookLeft, MouseMoveLeft, RightStickLeft ),
24 (LookUp, MouseMoveUp, RightStickUp ),
25 (LookDown, MouseMoveDown, RightStickDown )
26 )};
27
28 struct Graphics {
29 program: Program,
30 indices: IndexBuffer<u16>,
31 vertices: VertexBuffer<Vertex>,
32 normals: VertexBuffer<Normal>
33 }
34 let graphics: Rc<RefCell<Option<Graphics>>> = Rc::new(RefCell::new(None));
35 let graphics_setup = graphics.clone();
36
37 let draw_parameters = DrawParameters {
38 backface_culling: draw_parameters::BackfaceCullingMode::CullClockwise,
39 ..params::alias_3d()
40 };
41
42 let mut pos = vec3(0.0, 0.0, -30.0);
43 let mut rot = vec2(0.0, 0.0);
44 let mut gravity = 0.0;
45
46 let mut frame_start = Instant::now();
47
48 thin_engine::builder(input).with_setup(|display, window, _| {
49 let _ = window.set_cursor_grab(CursorGrabMode::Confined);
50 let _ = window.set_cursor_grab(CursorGrabMode::Locked);
51 window.set_cursor_visible(false);
52 window.set_title("Walk Test");
53
54 let (indices, vertices, normals) = mesh!(
55 display, &teapot::INDICES, &teapot::VERTICES, &teapot::NORMALS
56 ).unwrap();
57 let program = Program::from_source(
58 display,
59 "#version 140
60 in vec3 position;
61 in vec3 normal;
62 out vec3 v_normal;
63
64 uniform mat4 camera;
65 uniform mat4 model;
66 uniform mat4 perspective;
67
68 void main() {
69 mat3 norm_mat = transpose(inverse(mat3(camera * model)));
70 v_normal = normalize(norm_mat * normal);
71 gl_Position = perspective * camera * model * vec4(position, 1);
72 }",
73 "#version 140
74 out vec4 colour;
75 in vec3 v_normal;
76
77 uniform vec3 light;
78 const vec3 albedo = vec3(0.1, 1.0, 0.3);
79
80 void main(){
81 float light_level = dot(light, v_normal);
82 colour = vec4(albedo * light_level, 1.0);
83 }", None,
84 ).unwrap();
85 graphics_setup.replace(Some(Graphics { program, indices, vertices, normals }));
86 }).with_update(|input, display, _, target, _| {
87 let graphics = graphics.borrow();
88 let Graphics { vertices, indices, normals, program } = graphics.as_ref().unwrap();
89 let delta_time = frame_start.elapsed().as_secs_f32();
90 frame_start = Instant::now();
91
92 let mut frame = display.draw();
93 let perspective = Mat4::perspective_3d(frame.get_dimensions(), 1.0, 1024.0, 0.1);
94
95 // handle gravity and jump
96 gravity += delta_time * 9.5;
97 if input.pressed(Jump) { gravity = -10.0 }
98
99 // set camera rotation
100 let look_move = input.dir(LookRight, LookLeft, LookUp, LookDown);
101 rot += look_move.scale(delta_time * 15.0);
102 rot.y = rot.y.clamp(-PI / 2.0, PI / 2.0);
103 let rx = Quat::from_y_rot(rot.x);
104 let ry = Quat::from_x_rot(-rot.y);
105 let rot = rx * ry;
106
107 // move player based on camera and gravity
108 let dir = input.dir_max_len_1(Right, Left, Forward, Back);
109 let move_dir = vec3(dir.x, 0.0, dir.y).scale(5.0*delta_time);
110 pos += Mat3::from_rot(rx) * move_dir;
111 pos.y = (pos.y - gravity * delta_time).max(0.0);
112
113 if input.pressed(Exit) { target.exit() }
114
115 frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
116 //draw teapot
117 frame.draw(
118 (vertices, normals), indices,
119 program, &uniform! {
120 perspective: perspective,
121 model: Mat4::from_scale(Vec3::splat(0.1)),
122 camera: Mat4::from_inverse_transform(pos, Vec3::ONE, rot),
123 light: vec3(1.0, -0.9, -1.0).normalise()
124 },
125 &draw_parameters,
126 ).unwrap();
127
128 frame.finish().unwrap();
129 }).build(event_loop).unwrap();
130}
13fn main() {
14 use Action::*;
15 let event_loop = EventLoop::new().unwrap();
16 event_loop.set_control_flow(ControlFlow::Poll);
17
18 let mut colour = ResizableTexture2d::default();
19 let mut depth = ResizableDepthTexture2d::default();
20
21 let input = { use base_input_codes::*; input_map!(
22 (Left, ArrowLeft, KeyA, LeftStickLeft ),
23 (Right, ArrowRight, KeyD, LeftStickRight),
24 (Forward, ArrowUp, KeyW, LeftStickUp ),
25 (Back, ArrowDown, KeyS, LeftStickDown ),
26 (LookRight, MouseMoveRight, RightStickRight),
27 (LookLeft, MouseMoveLeft, RightStickLeft ),
28 (LookUp, MouseMoveUp, RightStickUp ),
29 (LookDown, MouseMoveDown, RightStickDown ),
30 (FXAA, KeyF, GamepadInput::North)
31 ) };
32 struct Graphics {
33 screen_indices: IndexBuffer<u32>,
34 screen_vertices: VertexBuffer<Vertex>,
35 screen_uvs: VertexBuffer<TextureCoords>,
36
37 teapot_indices: IndexBuffer<u16>,
38 teapot_vertices: VertexBuffer<Vertex>,
39 teapot_uvs: VertexBuffer<TextureCoords>,
40 teapot_normals: VertexBuffer<Normal>,
41
42 fxaa: Program, normal: Program, program: Program
43 }
44 let graphics: Rc<RefCell<Option<Graphics>>> = Rc::default();
45 let graphics_setup = graphics.clone();
46
47 let draw_parameters = DrawParameters {
48 backface_culling: draw_parameters::BackfaceCullingMode::CullClockwise,
49 ..params::alias_3d()
50 };
51 let mut fxaa_on = true;
52
53 let mut pos = vec3(0.0, 0.0, -30.0);
54 let mut rot = vec2(0.0, 0.0);
55
56 let mut frame_start = Instant::now();
57
58 thin_engine::builder(input).with_setup(|display, window, _| {
59 window.set_title("FXAA Test");
60 let _ = window.set_cursor_grab(CursorGrabMode::Confined);
61 let _ = window.set_cursor_grab(CursorGrabMode::Locked);
62 window.set_cursor_visible(false);
63
64 let (screen_indices, screen_vertices, screen_uvs) = mesh!(
65 display, &screen::INDICES, &screen::VERTICES, &screen::UVS
66 ).unwrap();
67 let (teapot_indices, teapot_vertices, teapot_uvs, teapot_normals) = mesh!(
68 display, &teapot::INDICES, &teapot::VERTICES, &[] as &[TextureCoords; 0], &teapot::NORMALS
69 ).unwrap();
70
71 let program = Program::from_source(
72 display,
73 "#version 140
74 in vec3 position;
75 in vec3 normal;
76
77 out vec3 v_normal;
78
79 uniform mat4 model;
80 uniform mat4 perspective;
81 uniform mat4 camera;
82
83 void main() {
84 mat3 norm_mat = transpose(inverse(mat3(camera * model)));
85 v_normal = normalize(norm_mat * normal);
86 gl_Position = perspective * camera * model * vec4(position, 1);
87 }",
88 "#version 140
89 out vec4 colour;
90 in vec3 v_normal;
91 uniform vec3 light;
92 uniform mat4 camera;
93 uniform vec3 ambient;
94 uniform vec3 albedo;
95 uniform float shine;
96 void main() {
97 vec3 camera_dir = inverse(mat3(camera)) * vec3(0, 0, -1);
98 vec3 half_dir = normalize(camera_dir + light);
99 float specular = pow(max(dot(half_dir, v_normal), 0.0), shine);
100 float light_level = max(dot(light, v_normal), 0.0);
101 colour = vec4(albedo * light_level + ambient + vec3(specular), 1.0);
102 }", None
103 ).unwrap();
104 let fxaa = shaders::fxaa_shader(display).unwrap();
105 let normal = Program::from_source(
106 display,
107 "#version 140
108 in vec2 texture_coords;
109 out vec2 uv;
110 in vec3 position;
111 void main() {
112 uv = texture_coords;
113 gl_Position = vec4(position, 1);
114 }",
115 "#version 140
116 in vec2 uv;
117 uniform sampler2D tex;
118 out vec4 colour;
119 void main() {
120 colour = texture(tex, uv);
121 }", None
122 ).unwrap();
123 graphics_setup.replace(Some(Graphics {
124 screen_indices, screen_vertices, screen_uvs,
125 teapot_indices, teapot_vertices, teapot_uvs, teapot_normals,
126 program, normal, fxaa
127 }));
128 }).with_update(|input, display, _, _, _| {
129 let graphics = graphics.borrow();
130 let Graphics {
131 screen_indices, screen_vertices, screen_uvs,
132 teapot_indices, teapot_vertices, teapot_uvs, teapot_normals,
133 program, normal, fxaa
134 } = graphics.as_ref().unwrap();
135 let teapot_mesh = (teapot_vertices, teapot_normals, teapot_uvs);
136 let screen_mesh = (screen_vertices, screen_uvs);
137
138 let delta_time = frame_start.elapsed().as_secs_f32();
139 frame_start = Instant::now();
140
141 // using a small resolution to better show the effect of fxaa.
142 let size = (380, 216);
143 display.resize(size);
144 depth.resize_to_display(&display);
145 colour.resize_to_display(&display);
146
147 // press f or gamepad north to toggle FXAA
148 if input.pressed(FXAA) { fxaa_on = !fxaa_on }
149
150 let colour = colour.texture();
151 let depth = depth.texture();
152 let mut frame = SimpleFrameBuffer::with_depth_buffer(
153 display, colour, depth
154 ).unwrap();
155
156 let perspective = Mat4::perspective_3d(size, 1.0, 1024.0, 0.1);
157
158 // set camera rotation
159 let look_move = input.dir(LookRight, LookLeft, LookUp, LookDown);
160 rot += look_move.scale(delta_time * 15.0);
161 rot.y = rot.y.clamp(-PI / 2.0, PI / 2.0);
162 let rx = Quat::from_y_rot(rot.x);
163 let ry = Quat::from_x_rot(-rot.y);
164 let rot = rx * ry;
165
166 // move player based on camera
167 let dir = input.dir_max_len_1(Right, Left, Forward, Back);
168 let move_dir = vec3(dir.x, 0.0, dir.y).scale(5.0*delta_time);
169 pos += Mat3::from_rot(rx) * move_dir;
170
171 frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
172 // draw teapot
173 frame.draw(
174 teapot_mesh, teapot_indices,
175 program, &uniform! {
176 perspective: perspective,
177 model: Mat4::from_scale(Vec3::splat(0.1)),
178 camera: Mat4::from_inverse_transform(pos, Vec3::ONE, rot),
179 light: vec3(0.1, 0.25, -1.0).normalise(),
180 albedo: vec3(0.5, 0.1, 0.4),
181 ambient: vec3(0.0, 0.05, 0.1),
182 shine: 50.0f32,
183 },
184 &draw_parameters,
185 ).unwrap();
186
187 let mut frame = display.draw();
188 frame.draw(
189 screen_mesh, screen_indices, if fxaa_on { fxaa } else { normal },
190 &shaders::fxaa_uniforms(colour), &DrawParameters::default()
191 ).unwrap();
192 frame.finish().unwrap();
193 }).build(event_loop).unwrap();
194}
Sourcepub fn builder() -> EventLoopBuilder<()>
pub fn builder() -> EventLoopBuilder<()>
Start building a new event loop.
This returns an EventLoopBuilder
, to allow configuring the event loop before creation.
To get the actual event loop, call build
on that.
Source§impl<T> EventLoop<T>
impl<T> EventLoop<T>
Sourcepub fn with_user_event() -> EventLoopBuilder<T>
pub fn with_user_event() -> EventLoopBuilder<T>
Start building a new event loop, with the given type as the user event type.
Sourcepub fn run<F>(self, event_handler: F) -> Result<(), EventLoopError>
👎Deprecated: use EventLoop::run_app
instead
pub fn run<F>(self, event_handler: F) -> Result<(), EventLoopError>
EventLoop::run_app
insteadSee run_app
.
Sourcepub fn run_app<A>(self, app: &mut A) -> Result<(), EventLoopError>where
A: ApplicationHandler<T>,
pub fn run_app<A>(self, app: &mut A) -> Result<(), EventLoopError>where
A: ApplicationHandler<T>,
Run the application with the event loop on the calling thread.
See the set_control_flow()
docs on how to change the event loop’s behavior.
§Platform-specific
-
iOS: Will never return to the caller and so values not passed to this function will not be dropped before the process exits.
-
Web: Will act as if it never returns to the caller by throwing a Javascript exception (that Rust doesn’t see) that will also mean that the rest of the function is never executed and any values not passed to this function will not be dropped.
Web applications are recommended to use
EventLoopExtWebSys::spawn()
1 instead ofrun_app()
to avoid the need for the Javascript exception trick, and to make it clearer that the event loop runs asynchronously (via the browser’s own, internal, event loop) and doesn’t block the current thread of execution like it does on other platforms.This function won’t be available with
target_feature = "exception-handling"
.
EventLoopExtWebSys::spawn_app()
is only available on Web. ↩
Sourcepub fn create_proxy(&self) -> EventLoopProxy<T>
pub fn create_proxy(&self) -> EventLoopProxy<T>
Creates an EventLoopProxy
that can be used to dispatch user events
to the main event loop, possibly from another thread.
Sourcepub fn owned_display_handle(&self) -> OwnedDisplayHandle
pub fn owned_display_handle(&self) -> OwnedDisplayHandle
Gets a persistent reference to the underlying platform display.
See the OwnedDisplayHandle
type for more information.
Sourcepub fn listen_device_events(&self, allowed: DeviceEvents)
pub fn listen_device_events(&self, allowed: DeviceEvents)
Change if or when DeviceEvent
s are captured.
See ActiveEventLoop::listen_device_events
for details.
Sourcepub fn set_control_flow(&self, control_flow: ControlFlow)
pub fn set_control_flow(&self, control_flow: ControlFlow)
Sets the ControlFlow
.
Examples found in repository?
9fn main() {
10 use Action::*;
11 let event_loop = EventLoop::new().unwrap();
12 event_loop.set_control_flow(ControlFlow::Poll);
13
14 let input = { use base_input_codes::*; input_map!(
15 (Jump, Space, GamepadInput::South),
16 // square brackets mean that all input codes must be pressed for the bind to be pressed
17 (Exit, [ControlLeft, Escape], GamepadInput::Start),
18 (Left, ArrowLeft, KeyA, LeftStickLeft ),
19 (Right, ArrowRight, KeyD, LeftStickRight),
20 (Forward, ArrowUp, KeyW, LeftStickUp ),
21 (Back, ArrowDown, KeyS, LeftStickDown ),
22 (LookRight, MouseMoveRight, RightStickRight),
23 (LookLeft, MouseMoveLeft, RightStickLeft ),
24 (LookUp, MouseMoveUp, RightStickUp ),
25 (LookDown, MouseMoveDown, RightStickDown )
26 )};
27
28 struct Graphics {
29 program: Program,
30 indices: IndexBuffer<u16>,
31 vertices: VertexBuffer<Vertex>,
32 normals: VertexBuffer<Normal>
33 }
34 let graphics: Rc<RefCell<Option<Graphics>>> = Rc::new(RefCell::new(None));
35 let graphics_setup = graphics.clone();
36
37 let draw_parameters = DrawParameters {
38 backface_culling: draw_parameters::BackfaceCullingMode::CullClockwise,
39 ..params::alias_3d()
40 };
41
42 let mut pos = vec3(0.0, 0.0, -30.0);
43 let mut rot = vec2(0.0, 0.0);
44 let mut gravity = 0.0;
45
46 let mut frame_start = Instant::now();
47
48 thin_engine::builder(input).with_setup(|display, window, _| {
49 let _ = window.set_cursor_grab(CursorGrabMode::Confined);
50 let _ = window.set_cursor_grab(CursorGrabMode::Locked);
51 window.set_cursor_visible(false);
52 window.set_title("Walk Test");
53
54 let (indices, vertices, normals) = mesh!(
55 display, &teapot::INDICES, &teapot::VERTICES, &teapot::NORMALS
56 ).unwrap();
57 let program = Program::from_source(
58 display,
59 "#version 140
60 in vec3 position;
61 in vec3 normal;
62 out vec3 v_normal;
63
64 uniform mat4 camera;
65 uniform mat4 model;
66 uniform mat4 perspective;
67
68 void main() {
69 mat3 norm_mat = transpose(inverse(mat3(camera * model)));
70 v_normal = normalize(norm_mat * normal);
71 gl_Position = perspective * camera * model * vec4(position, 1);
72 }",
73 "#version 140
74 out vec4 colour;
75 in vec3 v_normal;
76
77 uniform vec3 light;
78 const vec3 albedo = vec3(0.1, 1.0, 0.3);
79
80 void main(){
81 float light_level = dot(light, v_normal);
82 colour = vec4(albedo * light_level, 1.0);
83 }", None,
84 ).unwrap();
85 graphics_setup.replace(Some(Graphics { program, indices, vertices, normals }));
86 }).with_update(|input, display, _, target, _| {
87 let graphics = graphics.borrow();
88 let Graphics { vertices, indices, normals, program } = graphics.as_ref().unwrap();
89 let delta_time = frame_start.elapsed().as_secs_f32();
90 frame_start = Instant::now();
91
92 let mut frame = display.draw();
93 let perspective = Mat4::perspective_3d(frame.get_dimensions(), 1.0, 1024.0, 0.1);
94
95 // handle gravity and jump
96 gravity += delta_time * 9.5;
97 if input.pressed(Jump) { gravity = -10.0 }
98
99 // set camera rotation
100 let look_move = input.dir(LookRight, LookLeft, LookUp, LookDown);
101 rot += look_move.scale(delta_time * 15.0);
102 rot.y = rot.y.clamp(-PI / 2.0, PI / 2.0);
103 let rx = Quat::from_y_rot(rot.x);
104 let ry = Quat::from_x_rot(-rot.y);
105 let rot = rx * ry;
106
107 // move player based on camera and gravity
108 let dir = input.dir_max_len_1(Right, Left, Forward, Back);
109 let move_dir = vec3(dir.x, 0.0, dir.y).scale(5.0*delta_time);
110 pos += Mat3::from_rot(rx) * move_dir;
111 pos.y = (pos.y - gravity * delta_time).max(0.0);
112
113 if input.pressed(Exit) { target.exit() }
114
115 frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
116 //draw teapot
117 frame.draw(
118 (vertices, normals), indices,
119 program, &uniform! {
120 perspective: perspective,
121 model: Mat4::from_scale(Vec3::splat(0.1)),
122 camera: Mat4::from_inverse_transform(pos, Vec3::ONE, rot),
123 light: vec3(1.0, -0.9, -1.0).normalise()
124 },
125 &draw_parameters,
126 ).unwrap();
127
128 frame.finish().unwrap();
129 }).build(event_loop).unwrap();
130}
More examples
13fn main() {
14 use Action::*;
15 let event_loop = EventLoop::new().unwrap();
16 event_loop.set_control_flow(ControlFlow::Poll);
17
18 let mut colour = ResizableTexture2d::default();
19 let mut depth = ResizableDepthTexture2d::default();
20
21 let input = { use base_input_codes::*; input_map!(
22 (Left, ArrowLeft, KeyA, LeftStickLeft ),
23 (Right, ArrowRight, KeyD, LeftStickRight),
24 (Forward, ArrowUp, KeyW, LeftStickUp ),
25 (Back, ArrowDown, KeyS, LeftStickDown ),
26 (LookRight, MouseMoveRight, RightStickRight),
27 (LookLeft, MouseMoveLeft, RightStickLeft ),
28 (LookUp, MouseMoveUp, RightStickUp ),
29 (LookDown, MouseMoveDown, RightStickDown ),
30 (FXAA, KeyF, GamepadInput::North)
31 ) };
32 struct Graphics {
33 screen_indices: IndexBuffer<u32>,
34 screen_vertices: VertexBuffer<Vertex>,
35 screen_uvs: VertexBuffer<TextureCoords>,
36
37 teapot_indices: IndexBuffer<u16>,
38 teapot_vertices: VertexBuffer<Vertex>,
39 teapot_uvs: VertexBuffer<TextureCoords>,
40 teapot_normals: VertexBuffer<Normal>,
41
42 fxaa: Program, normal: Program, program: Program
43 }
44 let graphics: Rc<RefCell<Option<Graphics>>> = Rc::default();
45 let graphics_setup = graphics.clone();
46
47 let draw_parameters = DrawParameters {
48 backface_culling: draw_parameters::BackfaceCullingMode::CullClockwise,
49 ..params::alias_3d()
50 };
51 let mut fxaa_on = true;
52
53 let mut pos = vec3(0.0, 0.0, -30.0);
54 let mut rot = vec2(0.0, 0.0);
55
56 let mut frame_start = Instant::now();
57
58 thin_engine::builder(input).with_setup(|display, window, _| {
59 window.set_title("FXAA Test");
60 let _ = window.set_cursor_grab(CursorGrabMode::Confined);
61 let _ = window.set_cursor_grab(CursorGrabMode::Locked);
62 window.set_cursor_visible(false);
63
64 let (screen_indices, screen_vertices, screen_uvs) = mesh!(
65 display, &screen::INDICES, &screen::VERTICES, &screen::UVS
66 ).unwrap();
67 let (teapot_indices, teapot_vertices, teapot_uvs, teapot_normals) = mesh!(
68 display, &teapot::INDICES, &teapot::VERTICES, &[] as &[TextureCoords; 0], &teapot::NORMALS
69 ).unwrap();
70
71 let program = Program::from_source(
72 display,
73 "#version 140
74 in vec3 position;
75 in vec3 normal;
76
77 out vec3 v_normal;
78
79 uniform mat4 model;
80 uniform mat4 perspective;
81 uniform mat4 camera;
82
83 void main() {
84 mat3 norm_mat = transpose(inverse(mat3(camera * model)));
85 v_normal = normalize(norm_mat * normal);
86 gl_Position = perspective * camera * model * vec4(position, 1);
87 }",
88 "#version 140
89 out vec4 colour;
90 in vec3 v_normal;
91 uniform vec3 light;
92 uniform mat4 camera;
93 uniform vec3 ambient;
94 uniform vec3 albedo;
95 uniform float shine;
96 void main() {
97 vec3 camera_dir = inverse(mat3(camera)) * vec3(0, 0, -1);
98 vec3 half_dir = normalize(camera_dir + light);
99 float specular = pow(max(dot(half_dir, v_normal), 0.0), shine);
100 float light_level = max(dot(light, v_normal), 0.0);
101 colour = vec4(albedo * light_level + ambient + vec3(specular), 1.0);
102 }", None
103 ).unwrap();
104 let fxaa = shaders::fxaa_shader(display).unwrap();
105 let normal = Program::from_source(
106 display,
107 "#version 140
108 in vec2 texture_coords;
109 out vec2 uv;
110 in vec3 position;
111 void main() {
112 uv = texture_coords;
113 gl_Position = vec4(position, 1);
114 }",
115 "#version 140
116 in vec2 uv;
117 uniform sampler2D tex;
118 out vec4 colour;
119 void main() {
120 colour = texture(tex, uv);
121 }", None
122 ).unwrap();
123 graphics_setup.replace(Some(Graphics {
124 screen_indices, screen_vertices, screen_uvs,
125 teapot_indices, teapot_vertices, teapot_uvs, teapot_normals,
126 program, normal, fxaa
127 }));
128 }).with_update(|input, display, _, _, _| {
129 let graphics = graphics.borrow();
130 let Graphics {
131 screen_indices, screen_vertices, screen_uvs,
132 teapot_indices, teapot_vertices, teapot_uvs, teapot_normals,
133 program, normal, fxaa
134 } = graphics.as_ref().unwrap();
135 let teapot_mesh = (teapot_vertices, teapot_normals, teapot_uvs);
136 let screen_mesh = (screen_vertices, screen_uvs);
137
138 let delta_time = frame_start.elapsed().as_secs_f32();
139 frame_start = Instant::now();
140
141 // using a small resolution to better show the effect of fxaa.
142 let size = (380, 216);
143 display.resize(size);
144 depth.resize_to_display(&display);
145 colour.resize_to_display(&display);
146
147 // press f or gamepad north to toggle FXAA
148 if input.pressed(FXAA) { fxaa_on = !fxaa_on }
149
150 let colour = colour.texture();
151 let depth = depth.texture();
152 let mut frame = SimpleFrameBuffer::with_depth_buffer(
153 display, colour, depth
154 ).unwrap();
155
156 let perspective = Mat4::perspective_3d(size, 1.0, 1024.0, 0.1);
157
158 // set camera rotation
159 let look_move = input.dir(LookRight, LookLeft, LookUp, LookDown);
160 rot += look_move.scale(delta_time * 15.0);
161 rot.y = rot.y.clamp(-PI / 2.0, PI / 2.0);
162 let rx = Quat::from_y_rot(rot.x);
163 let ry = Quat::from_x_rot(-rot.y);
164 let rot = rx * ry;
165
166 // move player based on camera
167 let dir = input.dir_max_len_1(Right, Left, Forward, Back);
168 let move_dir = vec3(dir.x, 0.0, dir.y).scale(5.0*delta_time);
169 pos += Mat3::from_rot(rx) * move_dir;
170
171 frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
172 // draw teapot
173 frame.draw(
174 teapot_mesh, teapot_indices,
175 program, &uniform! {
176 perspective: perspective,
177 model: Mat4::from_scale(Vec3::splat(0.1)),
178 camera: Mat4::from_inverse_transform(pos, Vec3::ONE, rot),
179 light: vec3(0.1, 0.25, -1.0).normalise(),
180 albedo: vec3(0.5, 0.1, 0.4),
181 ambient: vec3(0.0, 0.05, 0.1),
182 shine: 50.0f32,
183 },
184 &draw_parameters,
185 ).unwrap();
186
187 let mut frame = display.draw();
188 frame.draw(
189 screen_mesh, screen_indices, if fxaa_on { fxaa } else { normal },
190 &shaders::fxaa_uniforms(colour), &DrawParameters::default()
191 ).unwrap();
192 frame.finish().unwrap();
193 }).build(event_loop).unwrap();
194}
Sourcepub fn create_window(
&self,
window_attributes: WindowAttributes,
) -> Result<Window, OsError>
👎Deprecated: use ActiveEventLoop::create_window
instead
pub fn create_window( &self, window_attributes: WindowAttributes, ) -> Result<Window, OsError>
ActiveEventLoop::create_window
insteadCreate a window.
Creating window without event loop running often leads to improper window creation;
use ActiveEventLoop::create_window
instead.
Sourcepub fn create_custom_cursor(
&self,
custom_cursor: CustomCursorSource,
) -> CustomCursor
pub fn create_custom_cursor( &self, custom_cursor: CustomCursorSource, ) -> CustomCursor
Create custom cursor.
Trait Implementations§
Source§impl<T> AsFd for EventLoop<T>
impl<T> AsFd for EventLoop<T>
Source§fn as_fd(&self) -> BorrowedFd<'_>
fn as_fd(&self) -> BorrowedFd<'_>
Get the underlying EventLoop’s fd
which you can register
into other event loop, like calloop
or mio
. When doing so, the
loop must be polled with the pump_app_events
API.
Source§impl<T> EventLoopExtPumpEvents for EventLoop<T>
impl<T> EventLoopExtPumpEvents for EventLoop<T>
Source§type UserEvent = T
type UserEvent = T
Event::UserEvent
.Source§fn pump_events<F>(
&mut self,
timeout: Option<Duration>,
event_handler: F,
) -> PumpStatus
fn pump_events<F>( &mut self, timeout: Option<Duration>, event_handler: F, ) -> PumpStatus
pump_app_events
.Source§fn pump_app_events<A>(
&mut self,
timeout: Option<Duration>,
app: &mut A,
) -> PumpStatuswhere
A: ApplicationHandler<Self::UserEvent>,
fn pump_app_events<A>(
&mut self,
timeout: Option<Duration>,
app: &mut A,
) -> PumpStatuswhere
A: ApplicationHandler<Self::UserEvent>,
EventLoop
to check for and dispatch pending events. Read moreSource§impl<T> EventLoopExtRunOnDemand for EventLoop<T>
impl<T> EventLoopExtRunOnDemand for EventLoop<T>
Source§type UserEvent = T
type UserEvent = T
Event::UserEvent
.Source§fn run_on_demand<F>(&mut self, event_handler: F) -> Result<(), EventLoopError>
fn run_on_demand<F>(&mut self, event_handler: F) -> Result<(), EventLoopError>
run_app_on_demand
.Source§fn run_app_on_demand<A>(&mut self, app: &mut A) -> Result<(), EventLoopError>where
A: ApplicationHandler<Self::UserEvent>,
fn run_app_on_demand<A>(&mut self, app: &mut A) -> Result<(), EventLoopError>where
A: ApplicationHandler<Self::UserEvent>,
Source§impl<T> EventLoopExtWayland for EventLoop<T>where
T: 'static,
impl<T> EventLoopExtWayland for EventLoop<T>where
T: 'static,
Source§fn is_wayland(&self) -> bool
fn is_wayland(&self) -> bool
EventLoop
uses Wayland.Source§impl<T> EventLoopExtX11 for EventLoop<T>where
T: 'static,
impl<T> EventLoopExtX11 for EventLoop<T>where
T: 'static,
Source§impl<T> GliumEventLoop for EventLoop<T>
impl<T> GliumEventLoop for EventLoop<T>
Source§impl<T> HasDisplayHandle for EventLoop<T>
impl<T> HasDisplayHandle for EventLoop<T>
Source§fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError>
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError>
Auto Trait Implementations§
impl<T> !Freeze for EventLoop<T>
impl<T> !RefUnwindSafe for EventLoop<T>
impl<T> !Send for EventLoop<T>
impl<T> !Sync for EventLoop<T>
impl<T> Unpin for EventLoop<T>where
T: Unpin,
impl<T> !UnwindSafe for EventLoop<T>
Blanket Implementations§
Source§impl<T> AsSource for Twhere
T: AsFd,
impl<T> AsSource for Twhere
T: AsFd,
Source§fn source(&self) -> BorrowedFd<'_>
fn source(&self) -> BorrowedFd<'_>
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.Source§impl<T> HasRawDisplayHandle for Twhere
T: HasDisplayHandle + ?Sized,
impl<T> HasRawDisplayHandle for Twhere
T: HasDisplayHandle + ?Sized,
Source§fn raw_display_handle(&self) -> Result<RawDisplayHandle, HandleError>
fn raw_display_handle(&self) -> Result<RawDisplayHandle, HandleError>
HasDisplayHandle
instead