1#[cfg(not(target_arch="wasm32"))]
9use render::{Display, new_display};
10#[cfg(not(target_arch="wasm32"))]
11pub use render::{Shape, Gradient, Model, Texture, TexCoords};
12
13use render::{Event};
14use afi::{VFrame, PathOp};
15
16use Matrix;
17use Vector;
18
19#[cfg(target_arch="wasm32")] mod win {mod wasm32; pub use self::wasm32::*;}
20
21#[cfg(target_arch="wasm32")]
22use self::win::{Display};
23#[cfg(target_arch="wasm32")]
24pub use self::win::{Shape, Gradient, Model, Texture, TexCoords};
25
26pub struct Screen<Ctx> where Ctx: Default {
28 #[cfg(not(target_arch="wasm32"))]
30 display: Box<Display>,
31 #[cfg(target_arch="wasm32")]
32 display: Display,
33
34 vframe: VFrame,
38 pub ctx: Ctx,
40 run: fn(&mut Screen<Ctx>, Event, f32),
42 running: bool,
43}
44
45#[derive(Debug)]
47pub enum ScreenError {
48
49}
50
51impl<Ctx> Screen<Ctx> where Ctx: Default {
52 pub fn start(run: fn(&mut Screen<Ctx>, Event, f32))
54 -> Result<(), ScreenError>
55 {
56 let mut screen = Screen::new(run);
57 let mut dt = 0.0;
58
59 while screen.running {
60 while let Some(input) = screen.display.input() {
61 (screen.run)(&mut screen, input, dt);
62 }
63
64 (screen.run)(&mut screen, Event::Timestep, dt);
65 dt = screen.display.update();
66 }
67
68 Ok(())
69 }
70
71 fn new(run: fn(&mut Screen<Ctx>, Event, f32)) -> Self {
73 let mut screen = Screen {
74 ctx: Ctx::default(),
75 vframe: VFrame(vec![]),
76
77 #[cfg(not(target_arch="wasm32"))]
78 display: new_display().unwrap(),
79 #[cfg(target_arch="wasm32")]
80 display: Display::new(),
81
82 run,
83 running: true,
84 };
85
86 let wh = screen.display.wh();
87 screen.vframe.0.resize((wh.0 as usize * wh.1 as usize) * 4, 0);
88
89 screen
90 }
91
92 pub fn stop(&mut self) {
94 ::std::process::exit(0);
95 }
96
97 pub fn switch(&mut self, run: fn(&mut Screen<Ctx>, Event, f32)) {
99 self.run = run;
100 }
101
102 pub fn clear(&mut self, color: (u8, u8, u8)) {
104 self.display.color(color)
105 }
106
107 pub fn model(&mut self, vertices: &[f32], fans: Vec<(u32, u32)>)
109 -> Model
110 {
111 self.display.model(vertices, fans)
112 }
113
114 pub fn texture(&mut self, wh: (u16, u16), graphic: &VFrame) -> Texture {
116 self.display.texture(wh, graphic)
117 }
118
119 pub fn gradient(&mut self, colors: &[f32]) -> Gradient {
121 self.display.gradient(colors)
122 }
123
124 pub fn texcoords(&mut self, texcoords: &[(f32, f32)]) -> TexCoords {
126 self.display.texcoords(texcoords)
127 }
128
129 pub fn set_texture(&mut self, texture: &mut Texture, wh: (u16, u16),
131 graphic: &VFrame)
132 {
133 self.display.set_texture(texture, wh, graphic)
134 }
135
136 pub fn shape_solid(&mut self, model: &Model, matrix: Matrix,
138 color: [f32; 4], blending: bool, fog: bool, camera: bool)
139 -> Shape
140 {
141 self.display.shape_solid(model, matrix, color, blending, fog,
142 camera)
143 }
144
145 pub fn shape_gradient(&mut self, model: &Model, matrix: Matrix,
147 gradient: Gradient, blending: bool, fog: bool, camera: bool)
148 -> Shape
149 {
150 self.display.shape_gradient(model, matrix, gradient,
151 blending, fog, camera)
152 }
153
154 pub fn shape_texture(&mut self, model: &Model, matrix: Matrix,
156 texture: &Texture, tc: TexCoords, blending: bool, fog: bool,
157 camera: bool) -> Shape
158 {
159 self.display.shape_texture(model, matrix, texture, tc,
160 blending, fog, camera)
161 }
162
163 pub fn shape_faded(&mut self, model: &Model, matrix: Matrix,
165 texture: &Texture, tc: TexCoords, alpha: f32, fog: bool,
166 camera: bool) -> Shape
167 {
168 self.display.shape_faded(model, matrix, texture, tc, alpha,
169 fog, camera)
170 }
171
172 pub fn shape_tinted(&mut self, model: &Model, matrix: Matrix,
174 texture: &Texture, tc: TexCoords, tint: [f32; 4],
175 blending: bool, fog: bool, camera: bool) -> Shape
176 {
177 self.display.shape_tinted(model, matrix, texture, tc, tint,
178 blending, fog, camera)
179 }
180
181 pub fn shape_complex(&mut self, model: &Model, matrix: Matrix,
183 texture: &Texture, tc: TexCoords, gradient: Gradient,
184 blending: bool, fog: bool, camera: bool) -> Shape
185 {
186 self.display.shape_complex(model, matrix, texture, tc,
187 gradient, blending, fog, camera)
188 }
189
190 pub fn drop_shape(&mut self, shape: &Shape) {
192 self.display.drop_shape(shape)
193 }
194
195 pub fn transform(&self, shape: &Shape, matrix: Matrix) {
197 self.display.transform(shape, matrix)
198 }
199
200 pub fn resize(&mut self, wh: (u16, u16)) {
202 self.vframe.0.resize((wh.0 as usize * wh.1 as usize) * 4, 0);
203 self.display.resize(wh);
204 }
205
206 pub fn wh(&self) -> (u16, u16) {
208 self.display.wh()
209 }
210
211 pub fn draw(&self, writer: &Fn(u16, u16) -> [u8; 4]) {
213 self.display.draw(writer)
214 }
215}