Some re-org and a stubbed-out audio module
Getty Ritter
6 years ago
1 | #version 150 | |
2 | ||
3 | in vec2 t_uv; | |
4 | out vec4 out_color; | |
5 | ||
6 | uniform sampler2D tex; | |
7 | ||
8 | void main() { | |
9 | out_color = texture(tex, t_uv); | |
10 | } |
1 | #version 150 | |
2 | in vec2 position; | |
3 | in vec2 uv; | |
4 | ||
5 | out vec2 t_uv; | |
6 | ||
7 | void main() { | |
8 | gl_Position = vec4(position.x, position.y, 0.5, 1.0); | |
9 | t_uv = uv; | |
10 | } |
1 | use std::{thread,sync}; | |
2 | ||
3 | #[allow(dead_code)] | |
4 | pub struct Audio { | |
5 | thread: thread::JoinHandle<()>, | |
6 | sender: sync::mpsc::Sender<String>, | |
7 | } | |
8 | ||
9 | impl Audio { | |
10 | pub fn init() -> Audio { | |
11 | let (sender, receiver) = sync::mpsc::channel(); | |
12 | let thread = thread::spawn(move || { | |
13 | while let Ok(v) = receiver.recv() { | |
14 | println!("should play {}", v); | |
15 | } | |
16 | }); | |
17 | Audio { thread, sender } | |
18 | } | |
19 | ||
20 | pub fn play(&self, sound: String) { | |
21 | self.sender.send(sound).unwrap(); | |
22 | } | |
23 | } |
2 | 2 | use imagefmt; |
3 | 3 | use std::{convert, ffi, io, string}; |
4 | 4 | |
5 | /// Nine errors should be enough for anyone | |
5 | 6 | #[derive(Debug)] |
6 | 7 | pub enum Error { |
7 | 8 | BadCString, |
336 | 336 | |
337 | 337 | pub fn bind_uv(&self, name: &str) -> Result<(), Error> { |
338 | 338 | let attr = self.data.program.get_attrib_location(name)?; |
339 | ||
339 | 340 | let (offset, num) = match Vtx::uv_location() { |
340 | 341 | Some(p) => p, |
341 | 342 | None => return Err(Error::OtherError( |
342 | 343 | "No UV data for associated vertex".to_owned())), |
343 | 344 | }; |
345 | ||
344 | 346 | unsafe { |
345 | 347 | let off = ptr::null::<u8>().offset( |
346 | 348 | (mem::size_of::<gl::GLfloat>() * offset as usize) as isize); |
381 | 383 | } |
382 | 384 | |
383 | 385 | |
386 | #[allow(dead_code)] | |
384 | 387 | pub struct Texture { |
385 | 388 | idx: u32, |
389 | image: Image<u8>, | |
386 | 390 | } |
387 | 391 | |
388 | 392 | impl Texture { |
389 | 393 | |
390 |
|
|
394 | /// Take a raw Bitmap value and create an OpenGL texture from it | |
395 | pub fn new_from_bitmap(image: Image<u8>) -> Texture { | |
391 | 396 | let mut idx = 0; |
392 | 397 | unsafe { |
393 | 398 | gl::GenTextures(1, &mut idx); |
394 | 399 | gl::BindTexture(gl::TEXTURE_2D, idx); |
395 | 400 | |
396 | gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::NEAREST as gl::GLint); | |
397 | gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::NEAREST as gl::GLint); | |
398 | gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_S, gl::CLAMP_TO_EDGE as gl::GLint); | |
399 | gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_T, gl::CLAMP_TO_EDGE as gl::GLint); | |
401 | let params = [ | |
402 | (gl::TEXTURE_MIN_FILTER, gl::NEAREST as gl::GLint), | |
403 | (gl::TEXTURE_MAG_FILTER, gl::NEAREST as gl::GLint), | |
404 | (gl::TEXTURE_WRAP_S, gl::CLAMP_TO_EDGE as gl::GLint), | |
405 | (gl::TEXTURE_WRAP_T, gl::CLAMP_TO_EDGE as gl::GLint), | |
406 | ]; | |
407 | ||
408 | for &(param, value) in params.into_iter() { | |
409 | gl::TexParameteri(gl::TEXTURE_2D, param, value) | |
410 | } | |
400 | 411 | |
401 | 412 | let fmt = match image.fmt { |
402 | 413 | ColFmt::RGB => gl::RGB, |
418 | 429 | |
419 | 430 | gl::BindTexture(gl::TEXTURE_2D, 0); |
420 | 431 | } |
421 | Texture { idx } | |
422 | } | |
423 |
|
|
432 | Texture { idx, image } | |
433 | } | |
434 | ||
435 | pub fn new_from_png(p: &str) -> Result<Texture, Error> { | |
436 | use imagefmt; | |
437 | let mut f = ::std::fs::File::open(p)?; | |
438 | let bitmap = imagefmt::png::read(&mut f, imagefmt::ColFmt::RGBA)?; | |
439 | Ok(Texture::new_from_bitmap(bitmap)) | |
440 | } | |
441 | ||
442 | } |
8 | 8 | |
9 | 9 | pub mod board; |
10 | 10 | pub mod graphics; |
11 | use graphics::{ | |
12 | Error, | |
13 | Shader, | |
14 | ShaderType, | |
15 | Program, | |
16 | Texture, | |
17 | VertexArray, | |
18 | VertexBuffer, | |
19 | Window, | |
20 | clear, | |
21 |
|
|
11 | pub mod audio; | |
12 | ||
13 | use graphics as gfx; | |
22 | 14 | use graphics::vertices::V2D; |
23 | 15 | |
24 | 16 | static TILE_DATA: [V2D; 4] = [ |
29 | 21 | ]; |
30 | 22 | |
31 | 23 | // Shader sources |
32 | static VS_SRC: &'static str = " | |
33 | #version 150 | |
34 | in vec2 position; | |
35 | in vec2 uv; | |
24 | static VS_SRC: &'static str = include_str!("../shaders/basic_vertex.glsl"); | |
25 | static FS_SRC: &'static str = include_str!("../shaders/basic_fragment.glsl"); | |
36 | 26 | |
37 | out vec2 t_uv; | |
38 | ||
39 | void main() { | |
40 | gl_Position = vec4(position.x, position.y, 0.5, 1.0); | |
41 | t_uv = uv; | |
42 | }"; | |
43 | ||
44 | static FS_SRC: &'static str = " | |
45 | #version 150 | |
46 | ||
47 | in vec2 t_uv; | |
48 | out vec4 out_color; | |
49 | ||
50 | uniform sampler2D tex; | |
51 | ||
52 | void main() { | |
53 | out_color = texture(tex, t_uv); | |
54 | }"; | |
55 | 27 | |
56 | 28 | /// the main loop of the game, setting up and running the window and |
57 | 29 | /// event loop |
58 | pub fn main_loop() -> Result<(), Error> { | |
59 | let mut f = std::fs::File::open("sample.png")?; | |
60 | let texture: imagefmt::Image<u8> = imagefmt::png::read( | |
61 | &mut f, | |
62 | imagefmt::ColFmt::RGBA, | |
63 | )?; | |
30 | pub fn main_loop() -> Result<(), gfx::Error> { | |
31 | let audio = audio::Audio::init(); | |
32 | let window = gfx::Window::create()?; | |
64 | 33 | |
65 |
let |
|
34 | let program = gfx::Program::link(vec![ | |
35 | gfx::Shader::compile(gfx::ShaderType::Vertex, VS_SRC)?, | |
36 | gfx::Shader::compile(gfx::ShaderType::Fragment, FS_SRC)?, | |
37 | ])?; | |
66 | 38 | |
67 | let vs = Shader::compile(ShaderType::Vertex, VS_SRC)?; | |
68 | let fs = Shader::compile(ShaderType::Fragment, FS_SRC)?; | |
69 |
let |
|
39 | let tex = gfx::Texture::new_from_png("sample.png")?; | |
70 | 40 | |
71 | let tex = Texture::new_from_bitmap(&texture); | |
72 | ||
73 | let vbo = VertexBuffer::new_array_buffer( | |
74 | VertexArray::new(&program), | |
41 | let vbo = gfx::VertexBuffer::new_array_buffer( | |
42 | gfx::VertexArray::new(&program), | |
75 | 43 | &TILE_DATA, |
76 | 44 | ); |
77 | 45 | |
98 | 66 | match k.virtual_keycode { |
99 | 67 | Some(VirtualKeyCode::Q) => |
100 | 68 | return ControlFlow::Break, |
101 |
Some(c) => |
|
69 | Some(c) => | |
70 | audio.play(format!("{:#?}", c)), | |
102 | 71 | _ => (), |
103 | 72 | } |
104 | 73 | } |
105 | 74 | _ => (), |
106 | 75 | } |
107 | 76 | |
108 |
|
|
77 | gfx::clear(); | |
109 | 78 | program.set_texture("tex", &tex).unwrap(); |
110 | 79 | vbo.draw(); |
111 | 80 |