awi/screen/
mod.rs

1// Copyright Jeron A. Lau 2017-2018.
2// Dual-licensed under either the MIT License or the Boost Software License,
3// Version 1.0.  (See accompanying file LICENSE_1_0.txt or copy at
4// https://www.boost.org/LICENSE_1_0.txt)
5
6//! Screen Module (Computer Monitor / Phone Screen / etc.)
7
8#[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
26/// A Window to the Screen.
27pub struct Screen<Ctx> where Ctx: Default {
28	// The platform-dependant implementation.
29	#[cfg(not(target_arch="wasm32"))]
30	display: Box<Display>,
31	#[cfg(target_arch="wasm32")]
32	display: Display,
33
34	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
35
36	// For Vector Graphics Rendering.
37	vframe: VFrame,
38	// program context.
39	pub ctx: Ctx,
40	// current function pointer.
41	run: fn(&mut Screen<Ctx>, Event, f32),
42	running: bool,
43}
44
45/// An error in the connection to the screen.
46#[derive(Debug)]
47pub enum ScreenError {
48	
49}
50
51impl<Ctx> Screen<Ctx> where Ctx: Default {
52	/// Start the program.
53	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	/// Open a new Window to the Screen.
72	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	/// Stop the program.
93	pub fn stop(&mut self) {
94		::std::process::exit(0);
95	}
96
97	/// Switch the run function
98	pub fn switch(&mut self, run: fn(&mut Screen<Ctx>, Event, f32)) {
99		self.run = run;
100	}
101
102	/// Update the clear color of the Window.
103	pub fn clear(&mut self, color: (u8, u8, u8)) {
104		self.display.color(color)
105	}
106
107	/// Upload a model to the GPU.
108	pub fn model(&mut self, vertices: &[f32], fans: Vec<(u32, u32)>)
109		-> Model
110	{
111		self.display.model(vertices, fans)
112	}
113
114	/// Upload a texture to the GPU.
115	pub fn texture(&mut self, wh: (u16, u16), graphic: &VFrame) -> Texture {
116		self.display.texture(wh, graphic)
117	}
118
119	/// Create gradient object.
120	pub fn gradient(&mut self, colors: &[f32]) -> Gradient {
121		self.display.gradient(colors)
122	}
123
124	/// Create texture coordinate object.
125	pub fn texcoords(&mut self, texcoords: &[(f32, f32)]) -> TexCoords {
126		self.display.texcoords(texcoords)
127	}
128
129	/// Set the pixels of a texture to something other than the original.
130	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	/// Make a shape with solid color.
137	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	/// Make a shape with gradient
146	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	/// Make a shape will solid texture.
155	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	/// Make a shape will texture and transparency
164	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	/// Make a shape with texture, and tint (color)
173	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	/// Make a shape with texture, and gradent
182	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	/// Stop drawing a shape.
191	pub fn drop_shape(&mut self, shape: &Shape) {
192		self.display.drop_shape(shape)
193	}
194
195	/// Apply a matrix transform to a shape.
196	pub fn transform(&self, shape: &Shape, matrix: Matrix) {
197		self.display.transform(shape, matrix)
198	}
199
200	/// Call this function when you get a resize event.
201	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	/// Get the width and height of the window.
207	pub fn wh(&self) -> (u16, u16) {
208		self.display.wh()
209	}
210
211	/// Update 2D overlay with writer function.
212	pub fn draw(&self, writer: &Fn(u16, u16) -> [u8; 4]) {
213		self.display.draw(writer)
214	}
215}