forked from ggez/ggez
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventloop.rs
76 lines (67 loc) · 2.73 KB
/
eventloop.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//! This example demonstrates how to roll your own event loop,
//! if for some reason you want to do that instead of using the `EventHandler`
//! trait to do that for you.
//!
//! This is exactly how `ggez::event::run()` works, it really is not
//! doing anything magical. But, if you want a bit more power over
//! the control flow of your game, this is how you get it.
//!
//! It is functionally identical to the `super_simple.rs` example apart from that.
extern crate cgmath;
extern crate ggez;
use ggez::event::winit_event::{Event, KeyboardInput, WindowEvent};
use ggez::graphics::{self, DrawMode};
use ggez::{event, GameResult};
pub fn main() -> GameResult {
let cb = ggez::ContextBuilder::new("eventloop", "ggez");
let (ctx, events_loop) = &mut cb.build()?;
let mut position: f32 = 1.0;
// This is also used in the loop inside `::run()` - it can be flipped off with `ggez::quit()`
while ctx.continuing {
// Tell the timer stuff a frame has happened.
// Without this the FPS timer functions and such won't work.
ctx.timer_context.tick();
// Handle events. Refer to `winit` docs for more information.
events_loop.poll_events(|event| {
// This tells `ggez` to update it's internal states, should the event require that.
// These include cursor position, view updating on resize, etc.
ctx.process_event(&event);
match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => ggez::quit(ctx),
WindowEvent::KeyboardInput {
input:
KeyboardInput {
virtual_keycode: Some(keycode),
..
},
..
} => match keycode {
event::KeyCode::Escape => ggez::quit(ctx),
_ => (),
},
// `CloseRequested` and `KeyboardInput` events won't appear here.
x => println!("Other window event fired: {:?}", x),
},
x => println!("Device event fired: {:?}", x),
}
});
// Update
position += 1.0;
// Draw
graphics::clear(ctx, [0.1, 0.2, 0.3, 1.0].into());
use ggez::graphics::Drawable;
graphics::Mesh::new_circle(
ctx,
DrawMode::Fill,
cgmath::Point2::new(0.0, 0.0),
100.0,
2.0,
graphics::WHITE,
)?
.draw(ctx, (cgmath::Point2::new(position, 380.0),))?;
graphics::present(ctx)?;
ggez::timer::yield_now();
}
Ok(())
}