loki_draw/opengl/
texture.rs

1use gl::types::GLuint;
2
3use crate::drawer::ImageSource;
4
5impl ImageSource {
6    pub fn from_memory(width: u32, height: u32, pixels_rgba: &[u8]) -> Self {
7        let mut id: GLuint = 0;
8
9        unsafe {
10            gl::GenTextures(1, &mut id);
11            gl::BindTexture(gl::TEXTURE_2D, id);
12
13            gl::TexImage2D(
14                gl::TEXTURE_2D,
15                0,
16                gl::RGBA as i32,
17                width as i32,
18                height as i32,
19                0,
20                gl::RGBA,
21                gl::UNSIGNED_BYTE,
22                pixels_rgba.as_ptr() as *const _,
23            );
24        }
25
26        Self { id, width, height }
27    }
28
29    pub fn bind(&self) {
30        unsafe { gl::BindTexture(gl::TEXTURE_2D, self.id) }
31    }
32}
33
34impl Drop for ImageSource {
35    fn drop(&mut self) {
36        unsafe {
37            gl::DeleteTextures(1, [self.id].as_ptr());
38        }
39    }
40}