veeenu / hudhook

A videogame overlay framework written in Rust, supporting DirectX and OpenGL

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

SkyLeite opened this issue · comments

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.

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.

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! :)