Created
July 17, 2015 09:12
-
-
Save anonymous/c8bcb319be13d5ef5af5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mod render { | |
use std::sync::mpsc::channel; | |
use std::sync::mpsc::Sender; | |
use std::thread; | |
#[derive(Debug)] | |
pub enum Event { | |
Grid(i32,i32,Vec<u8>,Vec<i16>), | |
} | |
pub struct Render { | |
tx: Sender<Event>, | |
} | |
impl Render { | |
pub fn new () -> Render { | |
let (tx,rx) = channel(); | |
thread::spawn(move || { | |
use ::glium::DisplayBuild; | |
use ::glium::Surface; | |
use ::glium::glutin::WindowBuilder; | |
use ::glium::index::NoIndices; | |
use ::glium::VertexBuffer; | |
use ::glium::index::PrimitiveType; | |
use ::glium::glutin; | |
use ::glium::Program; | |
use ::glium::uniforms::EmptyUniforms; | |
use ::glium::draw_parameters::PolygonMode; | |
use ::glium::draw_parameters::DrawParameters; | |
use std::sync::mpsc::TryRecvError; | |
let display = WindowBuilder::new() | |
.with_dimensions(256, 256) | |
.with_title(format!("render")) | |
.build_glium() | |
.unwrap(); | |
#[derive(Copy, Clone)] | |
struct Vertex { | |
position: [f32; 2], | |
tex_coord: [f32; 2], | |
} | |
implement_vertex!(Vertex, position, tex_coord); | |
let vertex1 = Vertex { position: [-0.8, 0.8], tex_coord: [0.0, 0.0] }; | |
let vertex2 = Vertex { position: [ 0.8, 0.8], tex_coord: [1.0, 0.0] }; | |
let vertex3 = Vertex { position: [ 0.8, -0.8], tex_coord: [1.0, 1.0] }; | |
let vertex4 = Vertex { position: [-0.8, -0.8], tex_coord: [0.0, 1.0] }; | |
let shape = vec![vertex1, vertex2, vertex4, | |
vertex2, vertex3, vertex4]; | |
let vertex_buffer = VertexBuffer::new(&display, shape); | |
let indices = NoIndices(PrimitiveType::TrianglesList); | |
let vertex_shader_src = r#" | |
#version 140 | |
in vec2 position; | |
in vec2 tex_coords; | |
out vec2 v_tex_coords; | |
void main() { | |
v_tex_coords = tex_coords; | |
gl_Position = vec4(position, 0.0, 1.0); | |
} | |
"#; | |
let fragment_shader_src = r#" | |
#version 140 | |
in vec2 v_tex_coords; | |
out vec4 color; | |
uniform sampler2D tex; | |
void main() { | |
color = texture(tex, v_tex_coords); | |
} | |
"#; | |
let program = Program::from_source(&display, vertex_shader_src, fragment_shader_src, None).unwrap(); | |
'ecto_loop: loop { | |
let mut target = display.draw(); | |
target.clear_color(0.1, 0.1, 0.1, 1.0); | |
let mut draw_params: DrawParameters = Default::default(); | |
draw_params.polygon_mode = PolygonMode::Line; | |
target.draw(&vertex_buffer, &indices, &program, &EmptyUniforms, &draw_params).unwrap(); | |
target.finish().unwrap(); | |
for ev in display.poll_events() { | |
match ev { | |
glutin::Event::KeyboardInput(_, _, Some(glutin::VirtualKeyCode::Escape)) => break 'ecto_loop, | |
glutin::Event::Closed => break 'ecto_loop, | |
_ => () | |
} | |
} | |
match rx.try_recv() { | |
Ok(_) => { | |
} | |
Err(e) => { | |
if let TryRecvError::Disconnected = e { | |
println!("render: disconnected"); | |
break 'ecto_loop; | |
} | |
} | |
} | |
} | |
}); | |
Render{tx:tx} | |
} | |
pub fn update (&mut self, event: Event) { | |
self.tx.send(event).unwrap(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment