tiny_game_framework/graphics/
texture.rs

1use std::{path::PathBuf, rc::Rc};
2
3use gl::types::{GLint, GLsizei, GLuint, GLvoid};
4
5#[derive(PartialEq, Debug, Clone)]
6pub enum Texture{
7    Path(String),
8    Loaded(GLuint),
9    None,
10}
11
12pub unsafe fn load_texture(path: &str) -> GLuint {
13    let img = image::open(path).expect("Failed to load image");
14    let img = img.flipv();
15    let width = img.width();
16    let height = img.height();
17    let raw_pixels = img.to_rgba8().into_raw();
18
19    let mut texture: GLuint = 0;
20    gl::GenTextures(1, &mut texture);
21    gl::BindTexture(gl::TEXTURE_2D, texture);
22
23    gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_S, gl::REPEAT as GLint);
24    gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_T, gl::REPEAT as GLint);
25    gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::LINEAR as GLint);
26    gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::LINEAR as GLint);
27
28    gl::TexImage2D(
29        gl::TEXTURE_2D,
30        0,
31        gl::RGBA as GLint,
32        width as GLsizei,
33        height as GLsizei,
34        0,
35        gl::RGBA,
36        gl::UNSIGNED_BYTE,
37        raw_pixels.as_ptr() as *const GLvoid,
38    );
39
40    gl::GenerateMipmap(gl::TEXTURE_2D);
41
42    texture
43}