forked from ruffle-rs/ruffle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
101 lines (84 loc) · 3.09 KB
/
main.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
mod audio;
mod render;
use crate::render::GliumRenderBackend;
use glutin::{
dpi::{LogicalPosition, LogicalSize},
ContextBuilder, ElementState, Event, EventsLoop, MouseButton, WindowBuilder, WindowEvent,
};
use ruffle_core::{backend::audio::NullAudioBackend, Player};
use std::path::PathBuf;
use std::time::{Duration, Instant};
use structopt::StructOpt;
#[derive(StructOpt, Debug)]
#[structopt(name = "basic")]
struct Opt {
#[structopt(name = "FILE", parse(from_os_str))]
input_path: PathBuf,
}
fn main() {
env_logger::init();
let opt = Opt::from_args();
let ret = run_player(opt.input_path);
if let Err(e) = ret {
eprintln!("Fatal error:\n{}", e);
std::process::exit(-1);
}
}
fn run_player(input_path: PathBuf) -> Result<(), Box<std::error::Error>> {
let swf_data = std::fs::read(input_path)?;
let mut events_loop = EventsLoop::new();
let window_builder = WindowBuilder::new().with_title("Ruffle");
let windowed_context = ContextBuilder::new()
.with_vsync(true)
.with_multisampling(4)
.with_srgb(true)
.build_windowed(window_builder, &events_loop)?;
let audio = audio::RodioAudioBackend::new()?;
let renderer = GliumRenderBackend::new(windowed_context)?;
let display = renderer.display().clone();
let mut player = Player::new(Box::new(renderer), Box::new(audio), swf_data)?;
let logical_size: LogicalSize = (player.movie_width(), player.movie_height()).into();
let hidpi_factor = display.gl_window().get_hidpi_factor();
display
.gl_window()
.resize(logical_size.to_physical(hidpi_factor));
dbg!((player.movie_width(), player.movie_height()));
display.gl_window().set_inner_size(logical_size);
let mut mouse_pos = LogicalPosition::new(0.0, 0.0);
let mut time = Instant::now();
loop {
// Poll UI events
let mut request_close = false;
events_loop.poll_events(|event| {
if let Event::WindowEvent { event, .. } = event {
match event {
WindowEvent::CursorMoved { position, .. } => {
mouse_pos = position;
player.mouse_move((position.x as f32, position.y as f32));
}
WindowEvent::MouseInput {
button: MouseButton::Left,
state: ElementState::Pressed,
..
} => player.mouse_down(),
WindowEvent::MouseInput {
button: MouseButton::Left,
state: ElementState::Released,
..
} => player.mouse_up(),
WindowEvent::CloseRequested => request_close = true,
_ => (),
}
}
});
if request_close {
break;
}
let new_time = Instant::now();
let dt = new_time.duration_since(time).as_millis();
time = new_time;
player.tick(dt as f64);
std::thread::sleep(Duration::from_millis(1000 / 60));
}
Ok(())
}