veeenu / hudhook

A videogame overlay framework written in Rust, supporting DirectX and OpenGL
MIT License
185 stars 27 forks source link

index out of bounds error when loading texture using new RenderContext API (DX9) #190

Closed SkyLeite closed 3 months ago

SkyLeite commented 3 months ago

Hi! I'm trying to use the new load_texture API introduced in 0.7.0 with DirectX 9, but attempting to load a texture gives me this error:

thread '<unnamed>' panicked at /sources/hudhook-0.7.0-9b05a34bc3a174ceed011f8233730fd9ecdcdd13008e8e00c2546c8508bd3519/src/renderer/backend/dx9.rs:435:42:
index out of bounds: the len is 3912 but the index is 3914

This is the file being loaded: button_A

And here's my code:

Texture::iter().for_each(|texture| {
    println!("Loading texture {:?}", texture);

    let tex_path = PathBuf::from(texture);

    if !tex_path.exists() {
        warn!("Texture file {:?} doesn't exist.", tex_path);
        return;
    }

    let tex_data = std::fs::read(tex_path).unwrap();

    let texture_id = render_ctx.load_texture(&tex_data, 100, 100).unwrap();
    texture_manager.add_texture(texture, texture_id);
});

The only way I could get this to not crash at runtime was to set the width and height parameters to 10, for some reason, but then of course the texture is loaded incorrectly.

veeenu commented 3 months ago

It seems you are loading the data directly off the PNG file. The byte slice you supply as texture needs to be in the R8G8B8A8 format. You can get that by using a crate such as image to decode the PNG into said format.

SkyLeite commented 3 months ago

Oh, that makes sense! I figured since that's how I was loading them before with D3Dx9CreateTextureFromFile, it'd work the same way here. Thank you! :)