From 0fb546baad25706ccad66e3ff62191dbd9a0eb6e Mon Sep 17 00:00:00 2001 From: Bergmann89 Date: Wed, 23 Dec 2020 18:39:03 +0100 Subject: [PATCH] Renamed glc 'ArrayBuffer' to 'Buffer' --- glc/src/{array_buffer.rs => buffer.rs} | 41 ++++++++++++----------- glc/src/lib.rs | 2 +- glc/src/vertex_array.rs | 16 ++++----- space-crush-app/src/misc/text.rs | 4 +-- space-crush-app/src/resources/camera.rs | 8 ++--- space-crush-app/src/resources/geometry.rs | 12 +++---- space-crush-app/src/resources/uniform.rs | 8 ++--- 7 files changed, 46 insertions(+), 45 deletions(-) rename glc/src/{array_buffer.rs => buffer.rs} (89%) diff --git a/glc/src/array_buffer.rs b/glc/src/buffer.rs similarity index 89% rename from glc/src/array_buffer.rs rename to glc/src/buffer.rs index 8789400..c248775 100644 --- a/glc/src/array_buffer.rs +++ b/glc/src/buffer.rs @@ -8,25 +8,20 @@ use crate::{ misc::{AsEnum, Bindable}, }; -/* ArrayBuffer */ +/* Buffer */ -pub struct ArrayBuffer { +pub struct Buffer { id: gl::GLuint, - target: Target, size: usize, } -impl ArrayBuffer { - pub fn new(target: Target) -> Result { +impl Buffer { + pub fn new() -> Result { let mut id = 0; Error::checked(|| gl::create_buffers(1, &mut id))?; - Ok(Self { - id, - target, - size: 0, - }) + Ok(Self { id, size: 0 }) } pub fn id(&self) -> gl::GLuint { @@ -107,12 +102,18 @@ impl ArrayBuffer { }) } - pub fn bind_buffer_base(&self, index: gl::GLuint) { - gl::bind_buffer_base(self.target.as_enum(), index, self.id); + pub fn bind_buffer_base(&self, target: Target, index: gl::GLuint) { + gl::bind_buffer_base(target.as_enum(), index, self.id); } - pub fn bind_buffer_range(&self, index: gl::GLuint, offset: gl::GLintptr, size: gl::GLsizeiptr) { - gl::bind_buffer_range(self.target.as_enum(), index, self.id, offset, size); + pub fn bind_buffer_range( + &self, + target: Target, + index: gl::GLuint, + offset: gl::GLintptr, + size: gl::GLsizeiptr, + ) { + gl::bind_buffer_range(target.as_enum(), index, self.id, offset, size); } fn inner_map(&self, access: gl::GLenum) -> Result<&mut [T], Error> { @@ -150,26 +151,26 @@ impl ArrayBuffer { } } -impl Drop for ArrayBuffer { +impl Drop for Buffer { fn drop(&mut self) { gl::delete_buffers(1, &self.id); } } -impl Bindable for ArrayBuffer { +impl<'a> Bindable for (Target, &'a Buffer) { fn bind(&self) { - gl::bind_buffer(self.target.as_enum(), self.id); + gl::bind_buffer(self.0.as_enum(), self.1.id); } fn unbind(&self) { - gl::bind_buffer(self.target.as_enum(), 0); + gl::bind_buffer(self.0.as_enum(), 0); } } /* Map */ pub struct Map<'a, T> { - buf: &'a ArrayBuffer, + buf: &'a Buffer, data: &'a [T], } @@ -190,7 +191,7 @@ impl Deref for Map<'_, T> { /* MapMut */ pub struct MapMut<'a, T> { - buf: &'a ArrayBuffer, + buf: &'a Buffer, data: &'a mut [T], } diff --git a/glc/src/lib.rs b/glc/src/lib.rs index 32dc8a1..591ea04 100644 --- a/glc/src/lib.rs +++ b/glc/src/lib.rs @@ -1,6 +1,6 @@ pub mod angle; pub mod animation; -pub mod array_buffer; +pub mod buffer; pub mod error; pub mod math; pub mod matrix; diff --git a/glc/src/vertex_array.rs b/glc/src/vertex_array.rs index 5f27108..1f78285 100644 --- a/glc/src/vertex_array.rs +++ b/glc/src/vertex_array.rs @@ -1,5 +1,5 @@ use crate::{ - array_buffer::ArrayBuffer, + buffer::{Buffer, Target}, error::Error, misc::{AsEnum, BindGuard, Bindable}, }; @@ -8,7 +8,7 @@ use crate::{ pub struct VertexArray { id: gl::GLuint, - buffers: Vec, + buffers: Vec, } impl VertexArray { @@ -16,11 +16,11 @@ impl VertexArray { Builder::default() } - pub fn buffers(&self) -> &Vec { + pub fn buffers(&self) -> &Vec { &self.buffers } - pub fn buffers_mut(&mut self) -> &mut Vec { + pub fn buffers_mut(&mut self) -> &mut Vec { &mut self.buffers } } @@ -49,7 +49,7 @@ pub struct Builder { } impl Builder { - pub fn bind_buffer(mut self, buffer: ArrayBuffer) -> BindingBuilder { + pub fn bind_buffer(mut self, buffer: Buffer) -> BindingBuilder { let binding = Binding { buffer, pointers: Vec::new(), @@ -74,7 +74,7 @@ impl Builder { let mut buffers = Vec::new(); for binding in self.bindings { - let guard = BindGuard::new(&binding.buffer); + let guard = BindGuard::new((Target::ArrayBuffer, &binding.buffer)); for pointer in binding.pointers { Error::checked(|| gl::enable_vertex_attrib_array(pointer.index))?; @@ -119,7 +119,7 @@ pub struct BindingBuilder { } impl BindingBuilder { - pub fn bind_buffer(self, buffer: ArrayBuffer) -> Self { + pub fn bind_buffer(self, buffer: Buffer) -> Self { self.builder.bind_buffer(buffer) } @@ -219,7 +219,7 @@ impl AsEnum for DataType { /* Binding */ struct Binding { - buffer: ArrayBuffer, + buffer: Buffer, pointers: Vec, } diff --git a/space-crush-app/src/misc/text.rs b/space-crush-app/src/misc/text.rs index 1a3431e..e239da9 100644 --- a/space-crush-app/src/misc/text.rs +++ b/space-crush-app/src/misc/text.rs @@ -11,7 +11,7 @@ use std::ptr::null; use std::rc::{Rc, Weak}; use glc::{ - array_buffer::{ArrayBuffer, Target, Usage}, + buffer::{Buffer, Usage}, error::Error as GlcError, misc::BindGuard, shader::{Program, Type, Uniform}, @@ -592,7 +592,7 @@ impl Text { const OFFSET_TEX_MAX: gl::GLsizei = OFFSET_TEX_MIN + SIZE_VEC2; const OFFSET_COLOR: gl::GLsizei = OFFSET_TEX_MAX + SIZE_VEC2; - let buffer = ArrayBuffer::new(Target::ArrayBuffer)?; + let buffer = Buffer::new()?; let array = VertexArray::builder() .bind_buffer(buffer) .vertex_attrib_pointer(0, 2, DataType::Float, false, STRIDE, OFFSET_POS_MIN)? diff --git a/space-crush-app/src/resources/camera.rs b/space-crush-app/src/resources/camera.rs index b58a7d1..cefced8 100644 --- a/space-crush-app/src/resources/camera.rs +++ b/space-crush-app/src/resources/camera.rs @@ -1,14 +1,14 @@ #![allow(dead_code)] use glc::{ - array_buffer::{ArrayBuffer, Target, Usage}, + buffer::{Buffer, Target, Usage}, error::Error, matrix::Matrix4f, vector::Vector2f, }; pub struct Camera { - buffer: ArrayBuffer, + buffer: Buffer, data: Data, view_invert: Matrix4f, projection_invert: Matrix4f, @@ -30,7 +30,7 @@ impl Camera { view: Matrix4f::identity(), size: Vector2f::default(), }; - let mut buffer = ArrayBuffer::new(Target::UniformBuffer)?; + let mut buffer = Buffer::new()?; buffer.buffer_data(Usage::StaticDraw, &[data.clone()])?; Ok(Self { @@ -100,7 +100,7 @@ impl Camera { } pub fn bind(&self, index: gl::GLuint) -> Result<(), Error> { - Error::checked(|| self.buffer.bind_buffer_base(index)) + Error::checked(|| self.buffer.bind_buffer_base(Target::UniformBuffer, index)) } pub fn world_to_view>(&self, pos: T) -> Vector2f { diff --git a/space-crush-app/src/resources/geometry.rs b/space-crush-app/src/resources/geometry.rs index 4026030..e40ec36 100644 --- a/space-crush-app/src/resources/geometry.rs +++ b/space-crush-app/src/resources/geometry.rs @@ -3,7 +3,7 @@ use std::mem::size_of; use glc::{ - array_buffer::{ArrayBuffer, Target, Usage}, + buffer::{Buffer, Usage}, misc::Bindable, shader::{Program, Type, Uniform}, vector::{Vector2f, Vector4f}, @@ -78,11 +78,11 @@ fn create_array_quad() -> Result { const STRIDE_POS: gl::GLsizei = size_of::() as gl::GLsizei; const OFFSET_POS: gl::GLsizei = 0; - let mut array_buffer = ArrayBuffer::new(Target::ArrayBuffer)?; - array_buffer.buffer_data(Usage::StaticDraw, vertices)?; + let mut buffer = Buffer::new()?; + buffer.buffer_data(Usage::StaticDraw, vertices)?; let vertex_array = VertexArray::builder() - .bind_buffer(array_buffer) + .bind_buffer(buffer) .vertex_attrib_pointer(0, 2, DataType::Float, false, STRIDE_POS, OFFSET_POS)? .build()?; @@ -93,10 +93,10 @@ fn create_array_line() -> Result { const STRIDE_POS: gl::GLsizei = size_of::() as gl::GLsizei; const OFFSET_POS: gl::GLsizei = 0; - let array_buffer = ArrayBuffer::new(Target::ArrayBuffer)?; + let buffer = Buffer::new()?; let vertex_array = VertexArray::builder() - .bind_buffer(array_buffer) + .bind_buffer(buffer) .vertex_attrib_pointer(0, 2, DataType::Float, false, STRIDE_POS, OFFSET_POS)? .build()?; diff --git a/space-crush-app/src/resources/uniform.rs b/space-crush-app/src/resources/uniform.rs index 4572b36..c65a7cf 100644 --- a/space-crush-app/src/resources/uniform.rs +++ b/space-crush-app/src/resources/uniform.rs @@ -1,12 +1,12 @@ use std::time::Instant; use glc::{ - array_buffer::{ArrayBuffer, Target, Usage}, + buffer::{Buffer, Target, Usage}, error::Error, }; pub struct Uniform { - buffer: ArrayBuffer, + buffer: Buffer, start_time: Instant, } @@ -17,7 +17,7 @@ struct Data { impl Uniform { pub fn new() -> Result { - let mut buffer = ArrayBuffer::new(Target::UniformBuffer)?; + let mut buffer = Buffer::new()?; buffer.buffer_data(Usage::StaticDraw, &[Data { time: 0.0 }])?; Ok(Self { @@ -37,6 +37,6 @@ impl Uniform { } pub fn bind(&self, index: gl::GLuint) -> Result<(), Error> { - Error::checked(|| self.buffer.bind_buffer_base(index)) + Error::checked(|| self.buffer.bind_buffer_base(Target::UniformBuffer, index)) } }