main
_ 2020-12-25 10:03:20 -06:00
parent 0a484ffa8d
commit 84207f9eb9
1 changed files with 46 additions and 18 deletions

View File

@ -23,6 +23,39 @@ use opengl_rust::{
timestep::TimeStep,
};
struct GraphicsContext {
window: sdl2::video::Window,
gl_ctx: sdl2::video::GLContext,
vertex_buffer: gpu_buffers::VertexBuffer,
index_buffer: gpu_buffers::IndexBuffer,
shader_program: shader::ShaderProgram,
attr_pos: u32,
}
fn draw_graphics (ctx: &GraphicsContext) {
let attr_pos = ctx.attr_pos;
ctx.window.gl_make_current (&ctx.gl_ctx).unwrap ();
glezz::clear_color (0.392f32, 0.710f32, 0.965f32, 1.0f32);
glezz::clear (gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT | gl::STENCIL_BUFFER_BIT);
ctx.shader_program.use_program ();
glezz::enable_vertex_attrib_array (Some (attr_pos));
ctx.vertex_buffer.bind ();
ctx.index_buffer.bind ();
unsafe {
gl::VertexAttribPointer (attr_pos, 2, gl::FLOAT, 0, 4 * 2, 0 as *const u8 as *const c_void);
gl::DrawRangeElements (gl::TRIANGLES, 0, 6, 6, gl::UNSIGNED_INT, 0 as *const u8 as *const c_void);
}
ctx.window.gl_swap_window ();
}
fn main () -> anyhow::Result <()> {
let sdl_context = sdl2::init ().map_err (|e| anyhow! ("Can't init SDL: {}", e))?;
@ -76,7 +109,7 @@ void main (void) {
let shader_program = shader::ShaderProgram::new (&vertex_shader, &fragment_shader).map_err (|e| anyhow! ("Can't link shader program: {}", e))?;
let attr_pos = shader_program.get_attribute_location (&CString::new ("attr_pos".as_bytes ())?);
let attr_pos = shader_program.get_attribute_location (&CString::new ("attr_pos".as_bytes ())?).try_into ()?;
let vertex_buffer = gpu_buffers::VertexBuffer::from_slice (&[
0.0, 0.0,
@ -90,6 +123,17 @@ void main (void) {
0, 2, 3
]);
let graphics_ctx = GraphicsContext {
window,
gl_ctx,
vertex_buffer,
index_buffer,
shader_program,
attr_pos,
};
'running: loop {
let frames_to_do = time_step.step ();
@ -109,23 +153,7 @@ void main (void) {
}
window.gl_make_current (&gl_ctx).unwrap ();
glezz::clear_color (0.392f32, 0.710f32, 0.965f32, 1.0f32);
glezz::clear (gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT | gl::STENCIL_BUFFER_BIT);
shader_program.use_program ();
glezz::enable_vertex_attrib_array (Some (attr_pos.try_into ()?));
vertex_buffer.bind ();
index_buffer.bind ();
unsafe {
gl::VertexAttribPointer (attr_pos.try_into ()?, 2, gl::FLOAT, 0, 4 * 2, 0 as *const u8 as *const c_void);
gl::DrawRangeElements (gl::TRIANGLES, 0, 6, 6, gl::UNSIGNED_INT, 0 as *const u8 as *const c_void);
}
window.gl_swap_window ();
draw_graphics (&graphics_ctx);
graphics_frames += 1;
std::thread::sleep (Duration::from_millis (15));