diff options
author | HampusM <hampus@hampusmat.com> | 2023-10-13 22:34:23 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-10-13 22:34:23 +0200 |
commit | 25b5ca97c5e5597570360c37d7452662e0118a00 (patch) | |
tree | be724d8a454faf955b6cdc59203b94a4a6ddeee0 | |
parent | 4da06868266c12622207e2e61f92aee93c24aaf8 (diff) |
refactor(engine): make vertex buffer wrapper hold single buffer
-rw-r--r-- | engine/src/renderer/mod.rs | 12 | ||||
-rw-r--r-- | engine/src/renderer/vertex_buffer.rs (renamed from engine/src/renderer/vertex_buffers.rs) | 30 |
2 files changed, 15 insertions, 27 deletions
diff --git a/engine/src/renderer/mod.rs b/engine/src/renderer/mod.rs index bbcb5e9..364b7f4 100644 --- a/engine/src/renderer/mod.rs +++ b/engine/src/renderer/mod.rs @@ -4,12 +4,12 @@ use std::process::abort; use glfw::WindowSize; use crate::renderer::vertex_array::{PrimitiveKind, VertexArray}; -use crate::renderer::vertex_buffers::{BufferUsage, VertexBuffers}; +use crate::renderer::vertex_buffer::{BufferUsage, VertexBuffer}; use crate::vector::Vec2; use crate::vertex::Vertex; mod vertex_array; -mod vertex_buffers; +mod vertex_buffer; pub fn initialize(window: &glfw::Window) -> Result<(), Error> { @@ -56,7 +56,7 @@ pub struct Renderable vertex_arr: VertexArray, /// Vertex buffer has to live as long as the vertex array - _vertex_buffer: VertexBuffers<1>, + _vertex_buffer: VertexBuffer, } impl Renderable @@ -67,11 +67,9 @@ impl Renderable vertex_arr.bind(); - let vertex_buffer = VertexBuffers::<1>::new(); + let vertex_buffer = VertexBuffer::new(); - vertex_buffer - .store(0, vertices, BufferUsage::Static) - .unwrap(); + vertex_buffer.store(vertices, BufferUsage::Static); VertexArray::configure_attrs(); diff --git a/engine/src/renderer/vertex_buffers.rs b/engine/src/renderer/vertex_buffer.rs index e7cba91..27ed705 100644 --- a/engine/src/renderer/vertex_buffers.rs +++ b/engine/src/renderer/vertex_buffer.rs @@ -3,36 +3,28 @@ use std::mem::size_of_val; use crate::vertex::Vertex; #[derive(Debug)] -pub struct VertexBuffers<const CNT: usize> +pub struct VertexBuffer { - buffers: [gl::types::GLuint; CNT], + buffer: gl::types::GLuint, } -impl<const CNT: usize> VertexBuffers<CNT> +impl VertexBuffer { pub fn new() -> Self { - let mut buffers = [gl::types::GLuint::default(); CNT]; + let mut buffer = gl::types::GLuint::default(); unsafe { - #[allow(clippy::cast_possible_wrap, clippy::cast_possible_truncation)] - gl::GenBuffers(CNT as gl::types::GLsizei, buffers.as_mut_ptr()); + gl::GenBuffers(1, &mut buffer); }; - Self { buffers } + Self { buffer } } - pub fn store( - &self, - buffer_index: usize, - vertices: &[Vertex], - usage: BufferUsage, - ) -> Option<()> + pub fn store(&self, vertices: &[Vertex], usage: BufferUsage) { - let buffer = *self.buffers.get(buffer_index)?; - unsafe { - gl::BindBuffer(gl::ARRAY_BUFFER, buffer); + gl::BindBuffer(gl::ARRAY_BUFFER, self.buffer); } unsafe { @@ -44,18 +36,16 @@ impl<const CNT: usize> VertexBuffers<CNT> usage.into_gl(), ); } - - Some(()) } } -impl<const CNT: usize> Drop for VertexBuffers<CNT> +impl Drop for VertexBuffer { fn drop(&mut self) { #[allow(clippy::cast_possible_wrap, clippy::cast_possible_truncation)] unsafe { - gl::DeleteBuffers(CNT as gl::types::GLsizei, self.buffers.as_ptr()); + gl::DeleteBuffers(1, &self.buffer); } } } |