gdritter repos chenoska / c32dda9
Add lifetimes to structs to reduce parameter-passing Getty Ritter 6 years ago
2 changed file(s) with 21 addition(s) and 20 deletion(s). Collapse all Expand all
211211
212212 //
213213
214 pub struct VertexArray {
214 pub struct VertexArray<'p> {
215215 idx: u32,
216 }
217
218 impl Drop for VertexArray {
216 program: &'p Program,
217 }
218
219 impl<'p> Drop for VertexArray<'p> {
219220 fn drop(&mut self) {
220221 unsafe { gl::DeleteVertexArrays(1, &self.idx); }
221222 }
222223 }
223224
224 pub struct VertexBuffer<Vtx> {
225 pub struct VertexBuffer<'p, Vtx> {
225226 idx: u32,
226 data: VertexArray,
227 data: VertexArray<'p>,
227228 phantom: PhantomData<Vtx>,
228229 len: usize,
229230 }
230231
231 impl<Vtx> Drop for VertexBuffer<Vtx> {
232 impl<'p, Vtx> Drop for VertexBuffer<'p, Vtx> {
232233 fn drop(&mut self) {
233234 unsafe { gl::DeleteBuffers(1, &self.idx); }
234235 }
235236 }
236237
237 impl VertexArray {
238 pub fn new() -> VertexArray {
238 impl<'p> VertexArray<'p> {
239 pub fn new(program: &'p Program) -> VertexArray<'p> {
239240 let mut idx = 0;
240241 unsafe {
241242 gl::GenVertexArrays(1, &mut idx);
242243 gl::BindVertexArray(idx);
243244 }
244245
245 VertexArray { idx }
246 VertexArray { idx, program }
246247 }
247248
248249 pub fn bind(&self) {
258259 }
259260 }
260261
261 impl<Vtx: vertices::Vertex> VertexBuffer<Vtx> {
262 impl<'p, Vtx: vertices::Vertex> VertexBuffer<'p, Vtx> {
262263 pub fn new_array_buffer(
263 data: VertexArray,
264 data: VertexArray<'p>,
264265 vertex_data: &[Vtx],
265 ) -> VertexBuffer<Vtx> {
266 ) -> VertexBuffer<'p, Vtx> {
266267 let mut idx = 0;
267268 unsafe {
268269 gl::GenBuffers(1, &mut idx);
280281 VertexBuffer { idx, data, phantom, len }
281282 }
282283
283 pub fn bind_position(&self, p: &Program, name: &str) -> Result<(), Error> {
284 let attr = p.get_attrib_location(name)?;
284 pub fn bind_position(&self, name: &str) -> Result<(), Error> {
285 let attr = self.data.program.get_attrib_location(name)?;
285286 let (offset, num) = Vtx::pos_location();
286287 unsafe {
287288 let off = ptr::null::<u8>().offset(
299300 Ok(())
300301 }
301302
302 pub fn bind_uv(&self, p: &Program, name: &str) -> Result<(), Error> {
303 let attr = p.get_attrib_location(name)?;
303 pub fn bind_uv(&self, name: &str) -> Result<(), Error> {
304 let attr = self.data.program.get_attrib_location(name)?;
304305 let (offset, num) = Vtx::uv_location();
305306 unsafe {
306307 let off = ptr::null::<u8>().offset(
6060 let tex = pan::Texture::new_from_bitmap(&texture);
6161
6262 let vbo = pan::VertexBuffer::new_array_buffer(
63 pan::VertexArray::new(),
63 pan::VertexArray::new(&program),
6464 &TILE_DATA,
6565 );
6666
6767 program.use_program();
6868 program.bind_frag_data_location(0, "out_color")?;
6969
70 vbo.bind_position(&program, "position")?;
71 vbo.bind_uv(&program, "uv")?;
70 vbo.bind_position("position")?;
71 vbo.bind_uv("uv")?;
7272 vbo.unbind();
7373
7474 window.run(|event| {