aboutsummaryrefslogtreecommitdiff
path: root/stockton-render/src/draw/ui/texture.rs
blob: 439c3d7b15ff0ba01b38302dc5a5f5588572b7cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * Copyright (C) Oscar Shrimpton 2020
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
use crate::draw::texture::{LoadableImage, TextureRepo};
use crate::types::*;
use crate::UiState;
use egui::Texture;
use stockton_levels::{prelude::HasTextures, traits::textures::Texture as LTexture};

pub struct UiTextures;

impl HasTextures for UiTextures {
    type TexturesIter<'a> = std::slice::Iter<'a, LTexture>;

    fn textures_iter(&self) -> Self::TexturesIter<'_> {
        (&[]).iter()
    }

    fn get_texture(&self, _idx: u32) -> Option<&stockton_levels::prelude::textures::Texture> {
        None
    }
}

impl LoadableImage for &Texture {
    fn width(&self) -> u32 {
        self.width as u32
    }
    fn height(&self) -> u32 {
        self.height as u32
    }
    fn copy_row(&self, y: u32, ptr: *mut u8) {
        let row_size = self.width();
        let pixels = &self.pixels[(y * row_size) as usize..((y + 1) * row_size) as usize];

        for (i, x) in pixels.iter().enumerate() {
            unsafe {
                *ptr.offset(i as isize * 3) = *x;
                *ptr.offset((i as isize * 3) + 1) = *x;
                *ptr.offset((i as isize * 3) + 2) = *x;
            }
        }
    }

    unsafe fn copy_into(&self, _ptr: *mut u8, _row_size: usize) {
        todo!()
    }
}

pub fn ensure_textures(
    _tex_repo: &mut TextureRepo,
    ui: &mut UiState,
    _device: &mut Device,
    _adapter: &mut Adapter,
    _allocator: &mut DynamicAllocator,
    _command_queue: &mut CommandQueue,
    _command_pool: &mut CommandPool,
) {
    let tex = ui.ctx.texture();

    if tex.version != ui.last_tex_ver {
        // tex_repo.force_queue_load(0).unwrap(); // TODO
        ui.last_tex_ver = tex.version;
    }
}