1
+ use std:: sync:: Mutex ;
1
2
use std:: time:: Duration ;
2
3
use std:: collections:: VecDeque ;
3
- use include_dir:: { include_dir, DirEntry } ;
4
+ use include_dir:: { include_dir, DirEntry , FileSystem } ;
4
5
use regex:: Regex ;
5
6
use lazy_static:: lazy_static;
6
7
use ammolite_math:: * ;
7
8
use wasm_bindgen:: prelude:: * ;
8
9
use mlib:: * ;
9
10
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
+
10
52
#[ allow( unused) ]
11
53
macro_rules! print {
12
- ( $mapp : ident , $ ( $tt: tt) * ) => {
54
+ ( $( $tt: tt) * ) => {
13
55
let formatted = format!( $( $tt) * ) ;
14
- $mapp . io . out . extend ( formatted. as_bytes( ) ) ;
56
+ GLOBAL_IO . write_out ( & [ formatted. as_bytes( ) ] ) ;
15
57
}
16
58
}
17
59
18
60
#[ allow( unused) ]
19
61
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
+ ] ) ;
23
68
}
24
69
}
25
70
26
71
#[ allow( unused) ]
27
72
macro_rules! eprint {
28
- ( $mapp : ident , $ ( $tt: tt) * ) => {
73
+ ( $( $tt: tt) * ) => {
29
74
let formatted = format!( $( $tt) * ) ;
30
- $mapp . io . err . extend ( formatted. as_bytes( ) ) ;
75
+ GLOBAL_IO . write_err ( & [ formatted. as_bytes( ) ] ) ;
31
76
}
32
77
}
33
78
34
79
#[ allow( unused) ]
35
80
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
+ ] ) ;
39
87
}
40
88
}
41
89
42
90
// Implementation from https://doc.rust-lang.org/std/macro.dbg.html
43
91
#[ allow( unused) ]
44
92
macro_rules! dbg {
45
- ( $mapp : ident , ) => {
46
- eprintln!( $mapp , "[{}:{}]" , file!( ) , line!( ) ) ;
93
+ ( ) => {
94
+ eprintln!( "[{}:{}]" , file!( ) , line!( ) ) ;
47
95
} ;
48
- ( $mapp : ident , $ val: expr) => {
96
+ ( $val: expr) => {
49
97
match $val {
50
98
tmp => {
51
- eprintln!( $mapp , "[{}:{}] {} = {:#?}" ,
99
+ eprintln!( "[{}:{}] {} = {:#?}" ,
52
100
file!( ) , line!( ) , stringify!( $val) , & tmp) ;
53
101
tmp
54
102
}
55
103
}
56
104
} ;
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) ) ,+, )
60
108
} ;
61
109
}
62
110
@@ -68,10 +116,11 @@ const MODEL_BUTTON_PREVIOUS_BYTES: &'static [u8] = include_bytes!("../resources/
68
116
const MODEL_BUTTON_NEXT_BYTES : & ' static [ u8 ] = include_bytes ! ( "../resources/ui/button_next.glb" ) ;
69
117
const MODEL_MARKER_BYTES : & ' static [ u8 ] = include_bytes ! ( "../resources/ui/sphere_1m_radius.glb" ) ;
70
118
119
+ const DIR : FileSystem = include_dir ! ( "resources/showcase" ) ;
120
+
71
121
lazy_static ! {
72
122
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" )
75
124
. expect( "Could not traverse the resource directory tree." )
76
125
. flat_map( |dir_entry| {
77
126
match dir_entry {
@@ -85,15 +134,20 @@ lazy_static! {
85
134
panic!( "No `.glb` glTF models in the `resources/showcase` directory." )
86
135
}
87
136
137
+ println!( "Packaged showcase models:" ) ;
138
+
88
139
files. into_iter( )
89
- . map( |file| {
140
+ . enumerate( )
141
+ . map( |( index, file) | {
90
142
lazy_static! {
91
143
static ref PATTERN : Regex = Regex :: new( r"^(?P<name>.*)_\((?P<scale>.*?)\)$" ) . unwrap( ) ;
92
144
}
93
145
let stem = file. path( ) . file_stem( ) . unwrap( ) . to_str( ) . unwrap( ) ;
94
146
let captures = PATTERN . captures( stem) . unwrap( ) ;
95
147
let scale = captures. name( "scale" ) . unwrap( ) . as_str( ) . parse( ) . unwrap( ) ;
96
148
149
+ println!( "Model #{}: {:?}" , index, file. path( ) ) ;
150
+
97
151
( file. contents( ) , scale)
98
152
} )
99
153
. collect:: <Vec <_>>( )
@@ -131,7 +185,6 @@ pub struct Selection {
131
185
#[ mapp]
132
186
pub struct ExampleMapp {
133
187
elapsed : Duration ,
134
- io : IO ,
135
188
state : Vec < String > ,
136
189
command_id_next : usize ,
137
190
commands : VecDeque < Command > ,
@@ -188,7 +241,6 @@ impl Mapp for ExampleMapp {
188
241
fn new ( ) -> Self {
189
242
let mut result = Self {
190
243
elapsed : Default :: default ( ) ,
191
- io : Default :: default ( ) ,
192
244
state : Vec :: new ( ) ,
193
245
command_id_next : 0 ,
194
246
commands : VecDeque :: new ( ) ,
@@ -346,7 +398,9 @@ impl Mapp for ExampleMapp {
346
398
) . collect :: < Vec < _ > > ( ) ) ;
347
399
348
400
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[ ..] {
350
404
Some ( ( hmd. position . clone ( ) , hmd. direction . clone ( ) ) )
351
405
} else {
352
406
None
@@ -441,6 +495,6 @@ impl Mapp for ExampleMapp {
441
495
}
442
496
443
497
fn flush_io ( & mut self ) -> IO {
444
- std :: mem :: replace ( & mut self . io , Default :: default ( ) )
498
+ GLOBAL_IO . flush ( )
445
499
}
446
500
}
0 commit comments