diff options
author | tcmal <me@aria.rip> | 2024-08-25 17:44:23 +0100 |
---|---|---|
committer | tcmal <me@aria.rip> | 2024-08-25 17:44:23 +0100 |
commit | c9fb3ae28fe491bc55243fb80d8c6be93f37ad99 (patch) | |
tree | c8d6519479d3b17f5bd657495c4d7fe2d2830a1a /stockton-render/src/draw/ui/texture.rs | |
parent | c52a05e6d3977efce6bd4479aa312dc90e0452e5 (diff) |
feat(render): ui working
Diffstat (limited to 'stockton-render/src/draw/ui/texture.rs')
-rwxr-xr-x | stockton-render/src/draw/ui/texture.rs | 48 |
1 files changed, 30 insertions, 18 deletions
diff --git a/stockton-render/src/draw/ui/texture.rs b/stockton-render/src/draw/ui/texture.rs index f5ddb3e..7cba1ac 100755 --- a/stockton-render/src/draw/ui/texture.rs +++ b/stockton-render/src/draw/ui/texture.rs @@ -1,24 +1,32 @@ -use crate::draw::texture::{LoadableImage, TextureRepo}; +use crate::draw::texture::{resolver::TextureResolver, LoadableImage, TextureRepo}; use crate::UiState; use anyhow::Result; -use egui::Texture; -use stockton_levels::{prelude::HasTextures, traits::textures::Texture as LTexture}; +use egui::{CtxRef, Texture}; +use log::debug; +use std::{convert::TryInto, sync::Arc}; -pub struct UiTextures; - -impl HasTextures for UiTextures { - type TexturesIter<'a> = std::slice::Iter<'a, LTexture>; +pub struct UiTextures { + ctx: CtxRef, +} - fn textures_iter(&self) -> Self::TexturesIter<'_> { - (&[]).iter() +impl TextureResolver for UiTextures { + type Image = Arc<Texture>; + fn resolve(&mut self, tex: u32) -> Option<Self::Image> { + if tex == 0 { + Some(self.ctx.texture()) + } else { + None + } } +} - fn get_texture(&self, _idx: u32) -> Option<&stockton_levels::prelude::textures::Texture> { - None +impl UiTextures { + pub fn new(ctx: CtxRef) -> Self { + UiTextures { ctx } } } -impl LoadableImage for &Texture { +impl LoadableImage for Arc<Texture> { fn width(&self) -> u32 { self.width as u32 } @@ -31,22 +39,26 @@ impl LoadableImage for &Texture { 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; + *ptr.offset(i as isize * 4) = 255; + *ptr.offset((i as isize * 4) + 1) = 255; + *ptr.offset((i as isize * 4) + 2) = 255; + *ptr.offset((i as isize * 4) + 3) = *x; } } } - unsafe fn copy_into(&self, _ptr: *mut u8, _row_size: usize) { - todo!() + unsafe fn copy_into(&self, ptr: *mut u8, row_size: usize) { + for y in 0..self.height() { + self.copy_row(y, ptr.offset((row_size * y as usize).try_into().unwrap())); + } } } pub fn ensure_textures(tex_repo: &mut TextureRepo, ui: &mut UiState) -> Result<()> { - let tex = ui.ctx.texture(); + let tex = ui.ctx().texture(); if tex.version != ui.last_tex_ver { + debug!("Queueing UI Texture reload"); tex_repo.force_queue_load(0)?; ui.last_tex_ver = tex.version; } |