0

我想以我对 rust 非常陌生,因为我几天前才拿起它,所以我对所有事情都不是很清楚。但是,我遇到了第一个问题,我真的不知道该怎么办。

基本上,我想做的是使用一个模块来加载我所有的纹理,然后将它们放入一个向量中,这样我就可以从一个地方整齐地访问我的所有纹理。问题是,这实际上不适用于生锈。因为当我尝试访问纹理时,我收到一条错误消息 cannot move out of index of Vec<RawImage2d<'_, u8>>

查找此错误,我明白为什么它会在模糊的意义上发生,并且我已经查看了其他情况下错误的许多解决方案。问题是,如果我理解正确,就没有办法解决这个问题,使其按预期工作。

所以,我需要另一种解决方案,但我很难想出一个。这是我的代码,有什么办法可以使这项工作或任何人可以就如何重做事情提供任何建议?

这是加载资产的模块

extern crate image;

pub fn  load_textures() -> Vec<glium::texture::RawImage2d<'static,u8>> {
    use std::io::Cursor;

    let mut result = Vec::new();

    let image = image::load(Cursor::new(&include_bytes!("../assets/textures/textureTest.png")),
                            image::ImageFormat::Png).unwrap().to_rgba8();
    let image_dimensions = image.dimensions();
    let image = glium::texture::RawImage2d::from_raw_rgba_reversed(&image.into_raw(), image_dimensions);
    result.push(image);

    let image = image::load(Cursor::new(&include_bytes!("../assets/normals/normalMapTest.png")),
    image::ImageFormat::Png).unwrap().to_rgba8();
    let image_dimensions = image.dimensions();
    let image = glium::texture::RawImage2d::from_raw_rgba_reversed(&image.into_raw(), image_dimensions);
    result.push(image);

    return result;
}

主要代码


mod assets;

#[macro_use]
extern crate glium;
extern crate image;

fn main() {

    use glium::{glutin, Surface};

    let mut event_loop = glutin::event_loop::EventLoop::new();

    let wb = glutin::window::WindowBuilder::new()
    .with_inner_size(glium::glutin::dpi::LogicalSize::new(640.0, 360.0))
    .with_title("Delta_Engine");

    let cb = glutin::ContextBuilder::new();

    let display = glium::Display::new(wb, cb, &event_loop).unwrap();

    let texture_atlas = assets::load_textures();
    

    let texture = glium::texture::Texture2d::new(&display, texture_atlas[0]).unwrap();
    
   
}```
4

0 回答 0