rust/lib-hedgewars-engine/src/render/gear.rs
changeset 15290 fcf0fb0cddbf
parent 15288 0f734fa371e1
child 15291 07ab4206f2a9
equal deleted inserted replaced
15289:fd20e0a134af 15290:fcf0fb0cddbf
    22 struct SpriteData {
    22 struct SpriteData {
    23     size: Size,
    23     size: Size,
    24     filename: PathBuf,
    24     filename: PathBuf,
    25 }
    25 }
    26 
    26 
    27 const ATLAS_SIZE: Size = Size::square(2024);
    27 const ATLAS_SIZE: Size = Size::square(2048);
    28 
    28 
    29 impl GearRenderer {
    29 impl GearRenderer {
    30     pub fn new() -> Self {
    30     pub fn new() -> Self {
    31         let mut lookup = Vec::with_capacity(2048);
    31         let mut lookup = Vec::with_capacity(2048);
    32 
    32 
    44             "Filled atlas with {} sprites:\n{}",
    44             "Filled atlas with {} sprites:\n{}",
    45             sprites.len(),
    45             sprites.len(),
    46             atlas.used_space()
    46             atlas.used_space()
    47         );
    47         );
    48 
    48 
    49         let texture = Texture2D::new(max_size, gl::RGBA8, gl::LINEAR);
    49         let texture = Texture2D::new(ATLAS_SIZE, gl::RGBA8, gl::LINEAR);
    50 
    50 
    51         let mut pixels = vec![0; max_size.area() * 4].into_boxed_slice();
    51         let mut pixels = vec![0; max_size.area()].into_boxed_slice();
       
    52         let mut pixels_transposed = vec![0; max_size.area()].into_boxed_slice();
       
    53 
    52         for (path, sprite_index) in lookup.drain(..) {
    54         for (path, sprite_index) in lookup.drain(..) {
    53             if let Some((atlas_index, rect)) = atlas.get_rect(sprite_index) {
    55             if let Some((atlas_index, rect)) = atlas.get_rect(sprite_index) {
    54                 load_sprite_pixels(&path, &mut pixels[..]).expect("Unable to load Graphics");
    56                 let size = load_sprite_pixels(&path, mapgen::theme::slice_u32_to_u8_mut(&mut pixels[..])).expect("Unable to load Graphics");
    55                 texture.update(rect, &pixels, 0, gl::RGBA, gl::UNSIGNED_BYTE);
    57 
       
    58                 let used_pixels = if size.width == rect.width() {
       
    59                     for y in 0..rect.height() {
       
    60                         for x in 0..rect.width() {
       
    61                             pixels_transposed[y * rect.width() + x] = pixels[x * rect.height() + y];
       
    62                         }
       
    63                     }
       
    64                     &mut pixels_transposed[..]
       
    65                 } else {
       
    66                     &mut pixels[..]
       
    67                 };
       
    68 
       
    69                 texture.update(rect, mapgen::theme::slice_u32_to_u8_mut(used_pixels), 0, gl::RGBA, gl::UNSIGNED_BYTE);
    56             }
    70             }
    57         }
    71         }
    58 
    72 
    59         Self { atlas }
    73         Self { atlas }
    60     }
    74     }
    62     pub fn render(&mut self, camera: &Camera) {
    76     pub fn render(&mut self, camera: &Camera) {
    63         let projection = camera.projection();
    77         let projection = camera.projection();
    64     }
    78     }
    65 }
    79 }
    66 
    80 
    67 fn load_sprite_pixels(path: &Path, buffer: &mut [u8]) -> io::Result<()> {
    81 fn load_sprite_pixels(path: &Path, buffer: &mut [u8]) -> io::Result<Size> {
    68     let decoder = Decoder::new(BufReader::new(File::open(path)?));
    82     let decoder = Decoder::new(BufReader::new(File::open(path)?));
    69     let (info, mut reader) = decoder.read_info()?;
    83     let (info, mut reader) = decoder.read_info()?;
    70 
    84 
    71     let size = Size::new(info.width as usize, info.height as usize);
    85     let size = Size::new(info.width as usize, info.height as usize);
    72     reader.next_frame(buffer)?;
    86     reader.next_frame(buffer)?;
    73     Ok(())
    87     Ok(size)
    74 }
    88 }
    75 
    89 
    76 fn load_sprite_size(path: &Path) -> io::Result<Size> {
    90 fn load_sprite_size(path: &Path) -> io::Result<Size> {
    77     let decoder = Decoder::new(BufReader::new(File::open(path)?));
    91     let decoder = Decoder::new(BufReader::new(File::open(path)?));
    78     let (info, mut reader) = decoder.read_info()?;
    92     let (info, mut reader) = decoder.read_info()?;