Split apart functionality into library
Getty Ritter
6 years ago
2 | 2 | name = "chenoska" |
3 | 3 | version = "0.1.0" |
4 | 4 | authors = ["Getty Ritter <samothes@infinitenegativeutility.com>"] |
5 | ||
6 | [lib] | |
7 | name = "chenoska" | |
8 | path = "src/lib.rs" | |
9 | ||
10 | [[bin]] | |
11 | name = "chenoska" | |
12 | path = "src/main.rs" | |
5 | 13 | |
6 | 14 | [dependencies] |
7 | 15 | gl = "0.6.0" |
1 | 1 | use glutin; |
2 |
use |
|
2 | use imagefmt; | |
3 | use std::{convert, ffi, io, string}; | |
3 | 4 | |
4 | 5 | #[derive(Debug)] |
5 | 6 | pub enum Error { |
9 | 10 | LinkError(String), |
10 | 11 | GlutinCreation(glutin::CreationError), |
11 | 12 | GlutinContext(glutin::ContextError), |
13 | ImageError(imagefmt::Error), | |
14 | IOError(io::Error), | |
12 | 15 | OtherError(String), |
13 | 16 | } |
14 | 17 | |
35 | 38 | Error::InvalidShaderLog |
36 | 39 | } |
37 | 40 | } |
41 | ||
42 | impl convert::From<imagefmt::Error> for Error { | |
43 | fn from(image_error: imagefmt::Error) -> Error { | |
44 | Error::ImageError(image_error) | |
45 | } | |
46 | } | |
47 | ||
48 | impl convert::From<io::Error> for Error { | |
49 | fn from(io_error: io::Error) -> Error { | |
50 | Error::IOError(io_error) | |
51 | } | |
52 | } |
1 | mod gl { | |
2 | extern crate gl; | |
3 | pub use self::gl::types::*; | |
4 | pub use self::gl::*; | |
5 | } | |
6 | extern crate glutin; | |
7 | extern crate imagefmt; | |
8 | ||
9 | pub mod board; | |
10 | pub mod graphics; | |
11 | pub mod pan { | |
12 | pub use graphics::*; | |
13 | } | |
14 | use graphics::vertices::V2D; | |
15 | ||
16 | static TILE_DATA: [V2D; 4] = [ | |
17 | V2D { x: 0.5, y: -0.5, u: 1.0, v: 1.0 }, | |
18 | V2D { x: -0.5, y: -0.5, u: 0.0, v: 1.0 }, | |
19 | V2D { x: 0.5, y: 0.5, u: 1.0, v: 0.0 }, | |
20 | V2D { x: -0.5, y: 0.5, u: 0.0, v: 0.0 }, | |
21 | ]; | |
22 | ||
23 | // Shader sources | |
24 | static VS_SRC: &'static str = " | |
25 | #version 150 | |
26 | in vec2 position; | |
27 | in vec2 uv; | |
28 | ||
29 | out vec2 t_uv; | |
30 | ||
31 | void main() { | |
32 | gl_Position = vec4(position.x, position.y, 0.5, 1.0); | |
33 | t_uv = uv; | |
34 | }"; | |
35 | ||
36 | static FS_SRC: &'static str = " | |
37 | #version 150 | |
38 | ||
39 | in vec2 t_uv; | |
40 | out vec4 out_color; | |
41 | ||
42 | uniform sampler2D tex; | |
43 | ||
44 | void main() { | |
45 | out_color = texture(tex, t_uv); | |
46 | }"; | |
47 | ||
48 | pub fn main_loop() -> Result<(), pan::Error> { | |
49 | let mut f = std::fs::File::open("sample.png")?; | |
50 | let texture: imagefmt::Image<u8> = imagefmt::png::read( | |
51 | &mut f, | |
52 | imagefmt::ColFmt::RGBA, | |
53 | )?; | |
54 | ||
55 | let window = pan::Window::create()?; | |
56 | ||
57 | let vs = pan::Shader::compile(pan::ShaderType::Vertex, VS_SRC)?; | |
58 | let fs = pan::Shader::compile(pan::ShaderType::Fragment, FS_SRC)?; | |
59 | let program = pan::Program::link(vec![vs, fs])?; | |
60 | ||
61 | let tex = pan::Texture::new_from_bitmap(&texture); | |
62 | ||
63 | let vbo = pan::VertexBuffer::new_array_buffer( | |
64 | pan::VertexArray::new(&program), | |
65 | &TILE_DATA, | |
66 | ); | |
67 | ||
68 | program.use_program(); | |
69 | program.bind_frag_data_location(0, "out_color")?; | |
70 | ||
71 | vbo.bind_position("position")?; | |
72 | vbo.bind_uv("uv")?; | |
73 | vbo.unbind(); | |
74 | ||
75 | window.run(|event| { | |
76 | use glutin::{ControlFlow, Event, WindowEvent, VirtualKeyCode}; | |
77 | ||
78 | match event { | |
79 | Event::WindowEvent { event: WindowEvent::Closed, .. } => | |
80 | return ControlFlow::Break, | |
81 | Event::WindowEvent { | |
82 | event: WindowEvent::KeyboardInput { | |
83 | input: k, | |
84 | .. | |
85 | }, | |
86 | .. | |
87 | } if k.state == glutin::ElementState::Pressed => { | |
88 | match k.virtual_keycode { | |
89 | Some(VirtualKeyCode::Q) => | |
90 | return ControlFlow::Break, | |
91 | Some(c) => println!("pressed {:#?}", c), | |
92 | _ => (), | |
93 | } | |
94 | } | |
95 | _ => (), | |
96 | } | |
97 | ||
98 | pan::clear(); | |
99 | program.set_texture("tex", &tex).unwrap(); | |
100 | vbo.draw(); | |
101 | ||
102 | ControlFlow::Continue | |
103 | })?; | |
104 | ||
105 | Ok(()) | |
106 | } |
1 | mod gl { | |
2 | extern crate gl; | |
3 | pub use self::gl::types::*; | |
4 | pub use self::gl::*; | |
5 | } | |
6 | extern crate glutin; | |
7 | extern crate imagefmt; | |
8 | ||
9 | mod board; | |
10 | mod graphics; | |
11 | mod pan { | |
12 | pub use graphics::*; | |
13 | } | |
14 | use graphics::vertices::V2D; | |
15 | ||
16 | static TILE_DATA: [V2D; 4] = [ | |
17 | V2D { x: 0.5, y: -0.5, u: 1.0, v: 1.0 }, | |
18 | V2D { x: -0.5, y: -0.5, u: 0.0, v: 1.0 }, | |
19 | V2D { x: 0.5, y: 0.5, u: 1.0, v: 0.0 }, | |
20 | V2D { x: -0.5, y: 0.5, u: 0.0, v: 0.0 }, | |
21 | ]; | |
22 | ||
23 | // Shader sources | |
24 | static VS_SRC: &'static str = " | |
25 | #version 150 | |
26 | in vec2 position; | |
27 | in vec2 uv; | |
28 | ||
29 | out vec2 t_uv; | |
30 | ||
31 | void main() { | |
32 | gl_Position = vec4(position.x, position.y, 0.5, 1.0); | |
33 | t_uv = uv; | |
34 | }"; | |
35 | ||
36 | static FS_SRC: &'static str = " | |
37 | #version 150 | |
38 | ||
39 | in vec2 t_uv; | |
40 | out vec4 out_color; | |
41 | ||
42 | uniform sampler2D tex; | |
43 | ||
44 | void main() { | |
45 | out_color = texture(tex, t_uv); | |
46 | }"; | |
47 | ||
48 | pub fn main_loop() -> Result<(), pan::Error> { | |
49 | let mut f = std::fs::File::open("sample.png").unwrap(); | |
50 | let texture: imagefmt::Image<u8> = imagefmt::png::read(&mut f, imagefmt::ColFmt::RGBA).unwrap(); | |
51 | ||
52 | let window = pan::Window::create()?; | |
53 | ||
54 | let vs = pan::Shader::compile(pan::ShaderType::Vertex, VS_SRC)?; | |
55 | let fs = pan::Shader::compile(pan::ShaderType::Fragment, FS_SRC)?; | |
56 | let program = pan::Program::link(vec![vs, fs])?; | |
57 | ||
58 | let tex = pan::Texture::new_from_bitmap(&texture); | |
59 | ||
60 | let vbo = pan::VertexBuffer::new_array_buffer( | |
61 | pan::VertexArray::new(&program), | |
62 | &TILE_DATA, | |
63 | ); | |
64 | ||
65 | program.use_program(); | |
66 | program.bind_frag_data_location(0, "out_color")?; | |
67 | ||
68 | vbo.bind_position("position")?; | |
69 | vbo.bind_uv("uv")?; | |
70 | vbo.unbind(); | |
71 | ||
72 | window.run(|event| { | |
73 | use glutin::{ControlFlow, Event, WindowEvent, VirtualKeyCode}; | |
74 | ||
75 | match event { | |
76 | Event::WindowEvent { event: WindowEvent::Closed, .. } => | |
77 | return ControlFlow::Break, | |
78 | Event::WindowEvent { | |
79 | event: WindowEvent::KeyboardInput { | |
80 | input: k, | |
81 | .. | |
82 | }, | |
83 | .. | |
84 | } if k.state == glutin::ElementState::Pressed => { | |
85 | match k.virtual_keycode { | |
86 | Some(VirtualKeyCode::Q) => | |
87 | return ControlFlow::Break, | |
88 | Some(c) => println!("pressed {:#?}", c), | |
89 | _ => (), | |
90 | } | |
91 | } | |
92 | _ => (), | |
93 | } | |
94 | ||
95 | pan::clear(); | |
96 | program.set_texture("tex", &tex).unwrap(); | |
97 | vbo.draw(); | |
98 | ||
99 | ControlFlow::Continue | |
100 | })?; | |
101 | ||
102 | Ok(()) | |
103 | } | |
104 | ||
1 | extern crate chenoska; | |
105 | 2 | |
106 | 3 | fn main() { |
107 |
if let Err(err) = |
|
4 | if let Err(err) = chenoska::main_loop() { | |
108 | 5 | println!("{:?}", err); |
109 | 6 | } |
110 | 7 | } |