aboutsummaryrefslogtreecommitdiff
path: root/stockton-render/src/draw/ui/texture.rs
blob: 52580e5b1a35acdc7f682766c9061b32d72c7e1f (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
/*
 * 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 egui::Texture;
use crate::types::*;
use crate::draw::texture::{TextureStore, LoadableImage};
use crate::UIState;

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;
            }
        }     
    }
}

pub fn ensure_textures(texture_store: &mut TextureStore, 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 {
        texture_store.put_texture(0, &*tex, device, adapter, allocator, command_queue, command_pool).unwrap(); // TODO
        ui.last_tex_ver = tex.version;
    } 
}