1use glutin::{ContextBuilder, Event, EventsLoop, WindowBuilder, WindowEvent};
2use glutin::dpi::{LogicalSize};
3use glutin::ContextTrait;
4use gllite;
5use gllite::gli;
6use gllite::texture::Texture;
7use gllite::uniforms::UniformValue;
8use gl;
9use std::rc::Rc;
10use std::thread;
11use std::time::{self, SystemTime};
12
13fn main() {
14 let mut events_loop = EventsLoop::new();
15 let wb = WindowBuilder::new()
16 .with_title("Demo")
17 .with_resizable(false)
18 .with_dimensions(LogicalSize::from((400, 400)));
19 let context = ContextBuilder::new()
20 .with_vsync(true)
21 .build_windowed(wb, &events_loop)
22 .unwrap();
23
24 unsafe {
25 context.make_current().unwrap();
26 gl::load_with(|symbol| context.get_proc_address(symbol) as *const _);
27 }
28
29 gllite::gli::clear_color(0.0, 0.0, 0.0, 1.0);
30
31 let shader_frag = "#version 330
32precision mediump float;
33out vec4 outColor;
34
35in vec2 v_position;
36
37uniform vec4 color;
38uniform sampler2D tex;
39
40void main() {
41 outColor = color * texture(tex, v_position);
42}";
43 let shader_vert = "#version 330
44in vec2 a_position;
45out vec2 v_position;
46
47void main() {
48 v_position = a_position;
49 gl_Position = vec4(a_position.xy, 0, 1);
50}";
51 let mut prog = gllite::program::Program::new();
52 prog
53 .add_shader(shader_vert, gl::VERTEX_SHADER)
54 .add_shader(shader_frag, gl::FRAGMENT_SHADER)
55 .compile();
56
57 let p = Rc::new(prog);
58
59 let mut node = gllite::node::Node::for_program(Rc::clone(&p));
60
61 let vertices: [f32; 6] = [
62 0.0, 1.0,
63 -1.0, -1.0,
64 1.0, -1.0,
65 ];
66
67 node.add_attribute(String::from("a_position"));
68 node.buffer_data(&vertices);
69 node.set_uniform(String::from("color"), UniformValue::FloatVec4(1.0, 1.0, 0.0, 1.0));
70
71 let tex = Texture::new();
72 let check: [u8;16] = [
73 30, 30, 30, 255,
74 200, 200, 200, 255,
75 200, 200, 200, 255,
76 30, 30, 30, 255,
77 ];
78 tex.set_from_bytes(gli::RGBA, 2, 2, gli::RGBA, &check[0] as *const u8);
79 tex.set_wrap_mode(gli::REPEAT, gli::REPEAT);
80 tex.set_filter_mode(gli::NEAREST, gli::NEAREST);
81 node.set_uniform(String::from("tex"), tex.as_uniform_value());
82
83 let mut last_frame_time = SystemTime::now();
84 loop {
85 let now = SystemTime::now();
86 let delta = match now.duration_since(last_frame_time) {
87 Ok(n) => n.as_millis(),
88 Err(_) => 1,
89 };
90 last_frame_time = now;
91
92 if delta < 16 {
93 let diff = 16 - delta;
94 let sleeptime = time::Duration::from_millis(diff as u64);
95 thread::sleep(sleeptime);
96 }
97
98 let mut should_exit = false;
99 events_loop.poll_events(|event| {
100 match event {
101 Event::WindowEvent {event, ..} => match event {
102 WindowEvent::CloseRequested => should_exit = true,
103 _ => (),
104 },
105 _ => (),
106 }
107 });
108 if should_exit {
109 break;
110 }
111
112 p.make_current();
113 unsafe {
114 gl::Clear(gl::COLOR_BUFFER_BIT);
115 node.draw();
116 }
117 context.swap_buffers().unwrap();
118 }
119}