Skip to content

Commit 8610068

Browse files
author
Jakub Hlusička
committedJan 8, 2020
Better IO, raytrace without HMD
1 parent 1e05ac1 commit 8610068

File tree

2 files changed

+81
-26
lines changed

2 files changed

+81
-26
lines changed
 

‎.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# user-provided resources
2-
resources/showcase
2+
resources
3+
!resources/ui
34

45
# generated by wasm-pack
56
pkg

‎src/lib.rs

+79-25
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,110 @@
1+
use std::sync::Mutex;
12
use std::time::Duration;
23
use std::collections::VecDeque;
3-
use include_dir::{include_dir, DirEntry};
4+
use include_dir::{include_dir, DirEntry, FileSystem};
45
use regex::Regex;
56
use lazy_static::lazy_static;
67
use ammolite_math::*;
78
use wasm_bindgen::prelude::*;
89
use mlib::*;
910

11+
#[derive(Default)]
12+
pub struct SyncIO {
13+
out: Mutex<Vec<u8>>,
14+
err: Mutex<Vec<u8>>,
15+
}
16+
17+
impl SyncIO {
18+
pub fn flush(&self) -> IO {
19+
IO {
20+
out: {
21+
let mut mut_out = GLOBAL_IO.out.lock().expect("Could not lock the stdout.");
22+
std::mem::replace(mut_out.as_mut(), Vec::new())
23+
},
24+
err: {
25+
let mut mut_err = GLOBAL_IO.err.lock().expect("Could not lock the stderr.");
26+
std::mem::replace(mut_err.as_mut(), Vec::new())
27+
},
28+
}
29+
}
30+
31+
pub fn write_out<T: AsRef<[u8]>>(&self, bytes: impl IntoIterator<Item=T>) {
32+
let mut mut_out = GLOBAL_IO.out.lock().expect("Could not lock the stdout.");
33+
34+
for item in bytes {
35+
mut_out.extend(item.as_ref());
36+
}
37+
}
38+
39+
pub fn write_err<T: AsRef<[u8]>>(&self, bytes: impl IntoIterator<Item=T>) {
40+
let mut mut_err = GLOBAL_IO.err.lock().expect("Could not lock the stderr.");
41+
42+
for item in bytes {
43+
mut_err.extend(item.as_ref());
44+
}
45+
}
46+
}
47+
48+
lazy_static! {
49+
pub static ref GLOBAL_IO: SyncIO = Default::default();
50+
}
51+
1052
#[allow(unused)]
1153
macro_rules! print {
12-
($mapp:ident, $($tt:tt)*) => {
54+
($($tt:tt)*) => {
1355
let formatted = format!($($tt)*);
14-
$mapp.io.out.extend(formatted.as_bytes());
56+
GLOBAL_IO.write_out(&[formatted.as_bytes()]);
1557
}
1658
}
1759

1860
#[allow(unused)]
1961
macro_rules! println {
20-
($mapp:ident, $($tt:tt)*) => {
21-
print!($mapp, $($tt)*);
22-
$mapp.io.out.push('\n' as u8)
62+
($($tt:tt)*) => {
63+
let formatted = format!($($tt)*);
64+
GLOBAL_IO.write_out(&[
65+
formatted.as_bytes(),
66+
std::slice::from_ref(&('\n' as u8))
67+
]);
2368
}
2469
}
2570

2671
#[allow(unused)]
2772
macro_rules! eprint {
28-
($mapp:ident, $($tt:tt)*) => {
73+
($($tt:tt)*) => {
2974
let formatted = format!($($tt)*);
30-
$mapp.io.err.extend(formatted.as_bytes());
75+
GLOBAL_IO.write_err(&[formatted.as_bytes()]);
3176
}
3277
}
3378

3479
#[allow(unused)]
3580
macro_rules! eprintln {
36-
($mapp:ident, $($tt:tt)*) => {
37-
eprint!($mapp, $($tt)*);
38-
$mapp.io.err.push('\n' as u8)
81+
($($tt:tt)*) => {
82+
let formatted = format!($($tt)*);
83+
GLOBAL_IO.write_err(&[
84+
formatted.as_bytes(),
85+
std::slice::from_ref(&('\n' as u8))
86+
]);
3987
}
4088
}
4189

4290
// Implementation from https://doc.rust-lang.org/std/macro.dbg.html
4391
#[allow(unused)]
4492
macro_rules! dbg {
45-
($mapp:ident, ) => {
46-
eprintln!($mapp, "[{}:{}]", file!(), line!());
93+
() => {
94+
eprintln!("[{}:{}]", file!(), line!());
4795
};
48-
($mapp:ident, $val:expr) => {
96+
($val:expr) => {
4997
match $val {
5098
tmp => {
51-
eprintln!($mapp, "[{}:{}] {} = {:#?}",
99+
eprintln!("[{}:{}] {} = {:#?}",
52100
file!(), line!(), stringify!($val), &tmp);
53101
tmp
54102
}
55103
}
56104
};
57-
($mapp:ident, $val:expr,) => { dbg!($mapp, $val) };
58-
($mapp:ident, $($val:expr),+ $(,)?) => {
59-
($(dbg!($mapp, $val)),+,)
105+
($val:expr,) => { dbg!($val) };
106+
($($val:expr),+ $(,)?) => {
107+
($(dbg!($val)),+,)
60108
};
61109
}
62110

@@ -68,10 +116,11 @@ const MODEL_BUTTON_PREVIOUS_BYTES: &'static [u8] = include_bytes!("../resources/
68116
const MODEL_BUTTON_NEXT_BYTES: &'static [u8] = include_bytes!("../resources/ui/button_next.glb");
69117
const MODEL_MARKER_BYTES: &'static [u8] = include_bytes!("../resources/ui/sphere_1m_radius.glb");
70118

119+
const DIR: FileSystem = include_dir!("resources/showcase");
120+
71121
lazy_static! {
72122
static ref MODELS_MAIN_BYTES_SCALE: Vec<(&'static [u8], f32)> = {
73-
let dir = include_dir!("resources/showcase");
74-
let files = dir.find("**/*_(*).glb")
123+
let files = DIR.find("**/*_(*).glb")
75124
.expect("Could not traverse the resource directory tree.")
76125
.flat_map(|dir_entry| {
77126
match dir_entry {
@@ -85,15 +134,20 @@ lazy_static! {
85134
panic!("No `.glb` glTF models in the `resources/showcase` directory.")
86135
}
87136

137+
println!("Packaged showcase models:");
138+
88139
files.into_iter()
89-
.map(|file| {
140+
.enumerate()
141+
.map(|(index, file)| {
90142
lazy_static! {
91143
static ref PATTERN: Regex = Regex::new(r"^(?P<name>.*)_\((?P<scale>.*?)\)$").unwrap();
92144
}
93145
let stem = file.path().file_stem().unwrap().to_str().unwrap();
94146
let captures = PATTERN.captures(stem).unwrap();
95147
let scale = captures.name("scale").unwrap().as_str().parse().unwrap();
96148

149+
println!("Model #{}: {:?}", index, file.path());
150+
97151
(file.contents(), scale)
98152
})
99153
.collect::<Vec<_>>()
@@ -131,7 +185,6 @@ pub struct Selection {
131185
#[mapp]
132186
pub struct ExampleMapp {
133187
elapsed: Duration,
134-
io: IO,
135188
state: Vec<String>,
136189
command_id_next: usize,
137190
commands: VecDeque<Command>,
@@ -188,7 +241,6 @@ impl Mapp for ExampleMapp {
188241
fn new() -> Self {
189242
let mut result = Self {
190243
elapsed: Default::default(),
191-
io: Default::default(),
192244
state: Vec::new(),
193245
command_id_next: 0,
194246
commands: VecDeque::new(),
@@ -346,7 +398,9 @@ impl Mapp for ExampleMapp {
346398
).collect::<Vec<_>>());
347399

348400
let ray_trace_cmd = self.view_orientations.as_ref().and_then(|view_orientations| {
349-
if let [Some(hmd), _] = &view_orientations[..] {
401+
if let [Some(any)] = &view_orientations[..] {
402+
Some((any.position.clone(), any.direction.clone()))
403+
} else if let [Some(hmd), _] = &view_orientations[..] {
350404
Some((hmd.position.clone(), hmd.direction.clone()))
351405
} else {
352406
None
@@ -441,6 +495,6 @@ impl Mapp for ExampleMapp {
441495
}
442496

443497
fn flush_io(&mut self) -> IO {
444-
std::mem::replace(&mut self.io, Default::default())
498+
GLOBAL_IO.flush()
445499
}
446500
}

0 commit comments

Comments
 (0)
Please sign in to comment.