gdritter repos chenoska / 6766437
Some re-org and a stubbed-out audio module Getty Ritter 6 years ago
6 changed file(s) with 89 addition(s) and 57 deletion(s). Collapse all Expand all
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 }
22 use imagefmt;
33 use std::{convert, ffi, io, string};
44
5 /// Nine errors should be enough for anyone
56 #[derive(Debug)]
67 pub enum Error {
78 BadCString,
336336
337337 pub fn bind_uv(&self, name: &str) -> Result<(), Error> {
338338 let attr = self.data.program.get_attrib_location(name)?;
339
339340 let (offset, num) = match Vtx::uv_location() {
340341 Some(p) => p,
341342 None => return Err(Error::OtherError(
342343 "No UV data for associated vertex".to_owned())),
343344 };
345
344346 unsafe {
345347 let off = ptr::null::<u8>().offset(
346348 (mem::size_of::<gl::GLfloat>() * offset as usize) as isize);
381383 }
382384
383385
386 #[allow(dead_code)]
384387 pub struct Texture {
385388 idx: u32,
389 image: Image<u8>,
386390 }
387391
388392 impl Texture {
389393
390 pub fn new_from_bitmap(image: &Image<u8>) -> Texture {
394 /// Take a raw Bitmap value and create an OpenGL texture from it
395 pub fn new_from_bitmap(image: Image<u8>) -> Texture {
391396 let mut idx = 0;
392397 unsafe {
393398 gl::GenTextures(1, &mut idx);
394399 gl::BindTexture(gl::TEXTURE_2D, idx);
395400
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 }
400411
401412 let fmt = match image.fmt {
402413 ColFmt::RGB => gl::RGB,
418429
419430 gl::BindTexture(gl::TEXTURE_2D, 0);
420431 }
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 }
88
99 pub mod board;
1010 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;
2214 use graphics::vertices::V2D;
2315
2416 static TILE_DATA: [V2D; 4] = [
2921 ];
3022
3123 // 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");
3626
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 }";
5527
5628 /// the main loop of the game, setting up and running the window and
5729 /// 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()?;
6433
65 let window = Window::create()?;
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 ])?;
6638
67 let vs = Shader::compile(ShaderType::Vertex, VS_SRC)?;
68 let fs = Shader::compile(ShaderType::Fragment, FS_SRC)?;
69 let program = Program::link(vec![vs, fs])?;
39 let tex = gfx::Texture::new_from_png("sample.png")?;
7040
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),
7543 &TILE_DATA,
7644 );
7745
9866 match k.virtual_keycode {
9967 Some(VirtualKeyCode::Q) =>
10068 return ControlFlow::Break,
101 Some(c) => println!("pressed {:#?}", c),
69 Some(c) =>
70 audio.play(format!("{:#?}", c)),
10271 _ => (),
10372 }
10473 }
10574 _ => (),
10675 }
10776
108 clear();
77 gfx::clear();
10978 program.set_texture("tex", &tex).unwrap();
11079 vbo.draw();
11180