added Spritesheet struct and texture loading Func
parent
decb65d0ce
commit
7759c4e991
|
@ -14,5 +14,6 @@ egui_glow = { version = "0.27.2", features = ["winit"] }
|
|||
glow = "0.13.1"
|
||||
glutin = "0.31.3"
|
||||
glutin-winit = "0.4.2"
|
||||
image = "0.25.1"
|
||||
raw-window-handle = "0.6.1"
|
||||
winit = "0.29.15"
|
||||
|
|
|
@ -1,2 +1,6 @@
|
|||
/// Render Module
|
||||
/// all of the code pertaining to Rendering will be written here
|
||||
/// this also includes all Bevy Components and Resources relating to rendering
|
||||
|
||||
pub mod shader;
|
||||
pub mod spritesheet;
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
use glow::HasContext;
|
||||
use std::path::Path;
|
||||
|
||||
|
||||
/// SpriteSheet Struct for Representing a Spritesheet
|
||||
/// feel free to clone
|
||||
#[derive(Clone)]
|
||||
pub struct SpriteSheet
|
||||
{
|
||||
texture_id: glow::NativeTexture,
|
||||
// NOTE where the spritesheet is saved
|
||||
path: String,
|
||||
cuts_x: u32,
|
||||
// NOTE possibly making it check vertically
|
||||
//cuts_y: u32,
|
||||
// NOTE maybe add an offset for each sprite
|
||||
// offsets: Vec<cgmath::Vec2<f32>>
|
||||
// NOTE Hold frames, if 0 it means it will be displayed for only 1 frame, 1 means it will be held for 2 etc
|
||||
// hold: Vec<u32>
|
||||
}
|
||||
|
||||
/// load texture from a filepath and return the ID
|
||||
/// NOTE this function does not check if the texture has already loaded
|
||||
fn texture_from_file(path: &str, gl: &glow::Context) -> glow::NativeTexture
|
||||
{
|
||||
|
||||
let texture_id;
|
||||
unsafe {
|
||||
texture_id = gl.create_texture().unwrap();
|
||||
|
||||
let img = image::open(&Path::new(&path)).expect("texture failed to load");
|
||||
let img = img.flipv();
|
||||
|
||||
let format = match img {
|
||||
image::DynamicImage::ImageLuma8(_) => glow::RED,
|
||||
image::DynamicImage::ImageLumaA8(_) => glow::RG,
|
||||
image::DynamicImage::ImageRgb8(_) => glow::RGB,
|
||||
image::DynamicImage::ImageRgba8(_) => glow::RGBA,
|
||||
_ => panic!("unknown image format at {path}")
|
||||
};
|
||||
|
||||
let data = img.as_bytes();
|
||||
|
||||
gl.bind_texture(glow::TEXTURE_2D, Some(texture_id));
|
||||
gl.tex_image_2d(glow::TEXTURE_2D, 0, format as i32, img.width() as i32, img.height() as i32,
|
||||
0, format, glow::UNSIGNED_BYTE, Some(&data));
|
||||
gl.generate_mipmap(glow::TEXTURE_2D);
|
||||
|
||||
|
||||
gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_WRAP_S, glow::REPEAT as i32);
|
||||
gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_MAG_FILTER, glow::NEAREST as i32);
|
||||
gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_MAG_FILTER, glow::NEAREST_MIPMAP_NEAREST as i32);
|
||||
|
||||
}
|
||||
|
||||
texture_id
|
||||
}
|
|
@ -76,6 +76,8 @@ fn main() {
|
|||
.build(None);
|
||||
|
||||
// decide on the opengl Context to use, will use the newest one available
|
||||
// If the device does NOT at least support opengl 3.3 it will crash
|
||||
// opengl 3.3 is supported by most 64 bit computers
|
||||
let mut not_current_gl_context = Some(unsafe {
|
||||
gl_display.create_context(&gl_config, &context_attributes).unwrap_or_else(|_| {
|
||||
gl_display.create_context(&gl_config, &fallback_attributes).unwrap_or_else(|_| {
|
||||
|
|
Loading…
Reference in New Issue